【发布时间】: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