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)? –