【问题标题】:Pass range into function将范围传递给函数
【发布时间】:2016-06-07 16:38:35
【问题描述】:

我有以下代码用于修改查询的命令文本。 问题在于我试图传递一个范围(RepStartRng)的最后一行。我的代码不是传递范围地址,而是传递范围值。请参阅下文并提前致谢。

Sub UseClass()
    Dim c As Report_
    Set c = New Report_

    Dim RepC As Long
    Dim Repcol As String
    Dim Counter As Long
    Dim RepStartRng As Range

    c.Day = FindDay()
    Repcol = ColLetter(FindDay())
    RepC = CountReports(Repcol)
    c.name = "A"
    Debug.Print "Repcol: " & Repcol
    Debug.Print RepC
    c.RepCount = RepC
    For i = 1 To c.RepCount
        Counter = i
        Report = Rep(Counter, ColLetter(FindDay()))
        Set RepStartRng = Range(Repcol & "3")
        RepStartRng.Select
        Debug.Print RepStartRng.Address
        UpdateQuery Report, RepStartRng, Counter
    Next i
End Sub
Function UpdateQuery(Report As String, RepRng As Range, Counter As Long)
    Dim Rng As Range
    Set RepRng = RepRng
    Dim Dat As String
    Dat = QueryDates(Rng)
    Dim cn As WorkbookConnection
    Dim wb As Workbook
    Dim NewWb As Workbook
    Set wb = ActiveWorkbook

    Dim odbcCn As ODBCConnection, oledbCn As OLEDBConnection
    For Each cn In ThisWorkbook.Connections
        If cn.name = Report Then
            Set oledbCn = cn.OLEDBConnection
            Debug.Print Dat
            oledbCn.CommandText = Replace(oledbCn.CommandText, "?", "'" & Dat & "'")
            Sheets(Rng.Value).Copy
            Set NewWb = ActiveWorkbook
            NewWb.SaveAs ("C:\Users\krishn.patel\Desktop\" & Rng.Value & ".xlsb"), FileFormat:=50
            NewWb.Close
            oledbCn.CommandText = Replace(oledbCn.CommandText, "'" & Dat & "'", "?")
        End If
    Next
End Function

【问题讨论】:

  • 传递范围时,传递的是范围对象,而不是地址。为什么你认为它正在传递价值? Set RepRng = RepRng 行的目的是什么。为什么不使用传入的范围 (RepRng)?

标签: function excel range parameter-passing vba


【解决方案1】:

如果不知道范围的内容或查看您调用的其他函数(例如 QueryDates),就不可能知道这段代码是否完美,但它应该能让您更接近您的需要。我删除了冗余变量并将 Function 更改为 Sub,因为它不返回任何值。

Sub UseClass()
    Dim c As Report_
    Set c = New Report_

    Dim RepC As Long
    Dim Repcol As String
    Dim Counter As Long
    Dim RepStartRng As Range

    c.Day = FindDay()
    Repcol = ColLetter(FindDay())
    RepC = CountReports(Repcol)
    c.name = "A"
    Debug.Print "Repcol: " & Repcol
    Debug.Print RepC
    c.RepCount = RepC
    For Counter = 1 To c.RepCount
        Report = Rep(Counter, ColLetter(FindDay()))
        Set RepStartRng = Range(Repcol & "3")
        UpdateQuery Report, RepStartRng, Counter
    Next Counter
End Sub
Sub UpdateQuery(Report As String, RepRng As Range, Counter As Long)
    Dim Dat As String
    Dat = QueryDates(RepRng)
    Dim cn As WorkbookConnection
    Dim NewWb As Workbook

    Dim odbcCn As ODBCConnection, oledbCn As OLEDBConnection
    For Each cn In ThisWorkbook.Connections
        If cn.name = Report Then
            Set oledbCn = cn.OLEDBConnection
            Debug.Print Dat
            oledbCn.CommandText = Replace(oledbCn.CommandText, "?", "'" & Dat & "'")
            Sheets(RepRng.Value).Copy
            Set NewWb = ActiveWorkbook
            NewWb.SaveAs ("C:\Users\krishn.patel\Desktop\" & RepRng.Value & ".xlsb"), FileFormat:=50
            NewWb.Close
            oledbCn.CommandText = Replace(oledbCn.CommandText, "'" & Dat & "'", "?")
        End If
    Next
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 2015-04-23
    相关资源
    最近更新 更多