英文:
How can I modify VBA function to access data from another workbook without opening it?
问题
从另一个工作簿访问数据的VBA函数。
你好,我有一个VBA函数,用于遍历打开的工作簿的工作表,并添加A1单元格中的值,以便举例说明。
我的问题是,我只能指定已打开工作簿的名称。
我希望能够通过路径来操作值,而无需打开工作簿。
Function test(wbName As String, ByVal wsName As String, ByVal wsName2 As String) As Long
start = Workbooks(wbName).Worksheets(wsName).Index
end = Workbooks(wbName).Worksheets(wsName2).Index
If start <= end Then
For i = start To end
test = Workbooks(wbName).Worksheets(i).Range("A1").Value + test
Next i
ElseIf start > end Then
MsgBox "开始日期不能晚于结束日期!"
End If
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.
Function test(wbName As String, ByVal wsName As String, ByVal wsName2 As String) As Long
start = Workbooks(wbName).Worksheets(wsName).Index
end = Workbooks(wbName).Worksheets(wsName2).Index
If start <= end Then
For i = start To end
test = Workbooks(wbName).Worksheets(i).Range("A1").Value + test
Next i
ElseIf start > end Then
MsgBox ("The start date cannot be later than the end date.!")
End If
End Function
I hope you understand my problem, unfortunately English is not my native language.
Thanks in advance for your help!
答案1
得分: 3
Option Explicit
Function SumOfA1s( _
ByVal WorkbookPath As String, _
ByVal FirstWorksheetName As String, _
ByVal LastWorksheetName As String) _
As Double
Const PROC_TITLE As String = "Sum of A1s"
On Error GoTo ClearError
Dim wb As Workbook: Set wb = Workbooks.Open(WorkbookPath)
Dim First As Long: First = wb.Worksheets(FirstWorksheetName).Index
Dim Last As Long: Last = wb.Worksheets(LastWorksheetName).Index
If First > Last Then
MsgBox "The start date cannot be later than the end date!", _
vbExclamation, PROC_TITLE
Goto ProcExit
End If
Dim Value, i As Long, Result As Double
For i = First To Last
Value = wb.Worksheets(i).Range("A1").Value
If IsNumeric(Value) Then
Result = Result + Value
End If
Next i
SumOfA1s = Result
ProcExit:
On Error Resume Next
If Not wb Is Nothing Then
wb.Close SaveChanges:=False
End If
On Error GoTo 0
Exit Function
ClearError:
MsgBox "Run-time error '" & Err.Number & "':" _
& vbLf & vbLf & Err.Description, vbCritical, PROC_TITLE
Resume ProcExit
End Function
英文:
Sum Up Cells
<!-- language: lang-vb -->
Option Explicit
Function SumOfA1s( _
ByVal WorkbookPath As String, _
ByVal FirstWorksheetName As String, _
ByVal LastWorksheetName As String) _
As Double
Const PROC_TITLE As String = "Sum of A1s"
On Error GoTo ClearError
Dim wb As Workbook: Set wb = Workbooks.Open(WorkbookPath)
Dim First As Long: First = wb.Worksheets(FirstWorksheetName).Index
Dim Last As Long: Last = wb.Worksheets(LastWorksheetName).Index
If First > Last Then
MsgBox "The start date cannot be later than the end date!", _
vbExclamation, PROC_TITLE
Goto ProcExit
End If
Dim Value, i As Long, Result As Double
For i = First To Last
Value = wb.Worksheets(i).Range("A1").Value
If IsNumeric(Value) Then
Result = Result + Value
End If
Next i
SumOfA1s = Result
ProcExit:
On Error Resume Next
If Not wb Is Nothing Then
wb.Close SaveChanges:=False
End If
On Error GoTo 0
Exit Function
ClearError:
MsgBox "Run-time error '" & Err.Number & "':" _
& vbLf & vbLf & Err.Description, vbCritical, PROC_TITLE
Resume ProcExit
End Function
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论