2016-06-07 116 views
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 
+0

当您传递一个范围时,您传递的是范围对象,而不是地址。你为什么认为这是通过价值? “设置RepRng = RepRng”行的目的是什么?你为什么不使用你传入的范围(RepRng)? –

回答

0

不知道你的范围的内容,或者看到你的其它函数(例如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