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

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

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 &lt;= end Then
        For i = start To end
            test = Workbooks(wbName).Worksheets(i).Range(&quot;A1&quot;).Value + test
        Next i
    ElseIf start &gt; end Then
        MsgBox (&quot;The start date cannot be later than the end date.!&quot;)
    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 = &quot;Sum of A1s&quot;
    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 &gt; Last Then
        MsgBox &quot;The start date cannot be later than the end date!&quot;, _
            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(&quot;A1&quot;).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 &quot;Run-time error &#39;&quot; &amp; Err.Number &amp; &quot;&#39;:&quot; _
        &amp; vbLf &amp; vbLf &amp; Err.Description, vbCritical, PROC_TITLE
    Resume ProcExit
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:

确定