如何修改VBA函数以在不打开工作簿的情况下访问另一个工作簿中的数据?

huangapple go评论114阅读模式
英文:

How can I modify VBA function to access data from another workbook without opening it?

问题

从另一个工作簿访问数据的VBA函数。

你好,我有一个VBA函数,用于遍历打开的工作簿的工作表,并添加A1单元格中的值,以便举例说明。

我的问题是,我只能指定已打开工作簿的名称。
我希望能够通过路径来操作值,而无需打开工作簿。

  1. Function test(wbName As String, ByVal wsName As String, ByVal wsName2 As String) As Long
  2. start = Workbooks(wbName).Worksheets(wsName).Index
  3. end = Workbooks(wbName).Worksheets(wsName2).Index
  4. If start <= end Then
  5. For i = start To end
  6. test = Workbooks(wbName).Worksheets(i).Range("A1").Value + test
  7. Next i
  8. ElseIf start > end Then
  9. MsgBox "开始日期不能晚于结束日期!"
  10. End If
  11. End Function

我希望你能理解我的问题,不幸的是,英语不是我的母语。
提前感谢你的帮助!

英文:

Access data from another workbook VBA function.

Hello, I have a VBA function that goes through the worksheets of open workbooks and adds the values in cell A1 for the sake of example.

My problem is that I can only specify the names of opened workbooks.
I want to make it so that I don't have to open the workbooks, but can work with the values based on the path.

  1. Function test(wbName As String, ByVal wsName As String, ByVal wsName2 As String) As Long
  2. start = Workbooks(wbName).Worksheets(wsName).Index
  3. end = Workbooks(wbName).Worksheets(wsName2).Index
  4. If start &lt;= end Then
  5. For i = start To end
  6. test = Workbooks(wbName).Worksheets(i).Range(&quot;A1&quot;).Value + test
  7. Next i
  8. ElseIf start &gt; end Then
  9. MsgBox (&quot;The start date cannot be later than the end date.!&quot;)
  10. End If
  11. End Function

I hope you understand my problem, unfortunately English is not my native language.
Thanks in advance for your help!

答案1

得分: 3

  1. Option Explicit
  2. Function SumOfA1s( _
  3. ByVal WorkbookPath As String, _
  4. ByVal FirstWorksheetName As String, _
  5. ByVal LastWorksheetName As String) _
  6. As Double
  7. Const PROC_TITLE As String = "Sum of A1s"
  8. On Error GoTo ClearError
  9. Dim wb As Workbook: Set wb = Workbooks.Open(WorkbookPath)
  10. Dim First As Long: First = wb.Worksheets(FirstWorksheetName).Index
  11. Dim Last As Long: Last = wb.Worksheets(LastWorksheetName).Index
  12. If First > Last Then
  13. MsgBox "The start date cannot be later than the end date!", _
  14. vbExclamation, PROC_TITLE
  15. Goto ProcExit
  16. End If
  17. Dim Value, i As Long, Result As Double
  18. For i = First To Last
  19. Value = wb.Worksheets(i).Range("A1").Value
  20. If IsNumeric(Value) Then
  21. Result = Result + Value
  22. End If
  23. Next i
  24. SumOfA1s = Result
  25. ProcExit:
  26. On Error Resume Next
  27. If Not wb Is Nothing Then
  28. wb.Close SaveChanges:=False
  29. End If
  30. On Error GoTo 0
  31. Exit Function
  32. ClearError:
  33. MsgBox "Run-time error '" & Err.Number & "':" _
  34. & vbLf & vbLf & Err.Description, vbCritical, PROC_TITLE
  35. Resume ProcExit
  36. End Function
英文:

Sum Up Cells

<!-- language: lang-vb -->

  1. Option Explicit
  2. Function SumOfA1s( _
  3. ByVal WorkbookPath As String, _
  4. ByVal FirstWorksheetName As String, _
  5. ByVal LastWorksheetName As String) _
  6. As Double
  7. Const PROC_TITLE As String = &quot;Sum of A1s&quot;
  8. On Error GoTo ClearError
  9. Dim wb As Workbook: Set wb = Workbooks.Open(WorkbookPath)
  10. Dim First As Long: First = wb.Worksheets(FirstWorksheetName).Index
  11. Dim Last As Long: Last = wb.Worksheets(LastWorksheetName).Index
  12. If First &gt; Last Then
  13. MsgBox &quot;The start date cannot be later than the end date!&quot;, _
  14. vbExclamation, PROC_TITLE
  15. Goto ProcExit
  16. End If
  17. Dim Value, i As Long, Result As Double
  18. For i = First To Last
  19. Value = wb.Worksheets(i).Range(&quot;A1&quot;).Value
  20. If IsNumeric(Value) Then
  21. Result = Result + Value
  22. End If
  23. Next i
  24. SumOfA1s = Result
  25. ProcExit:
  26. On Error Resume Next
  27. If Not wb Is Nothing Then
  28. wb.Close SaveChanges:=False
  29. End If
  30. On Error GoTo 0
  31. Exit Function
  32. ClearError:
  33. MsgBox &quot;Run-time error &#39;&quot; &amp; Err.Number &amp; &quot;&#39;:&quot; _
  34. &amp; vbLf &amp; vbLf &amp; Err.Description, vbCritical, PROC_TITLE
  35. Resume ProcExit
  36. End Function

huangapple
  • 本文由 发表于 2023年5月30日 04:02:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定