0
我有下面的代碼,我用它來修改查詢的命令文本。 問題出在我嘗試傳遞範圍的最後一行(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
當您傳遞一個範圍時,您傳遞的是範圍對象,而不是地址。你爲什麼認爲這是通過價值? 「設置RepRng = RepRng」行的目的是什麼?你爲什麼不使用你傳入的範圍(RepRng)? –