0

我想使用可以传递给 SUB 的变量来将数组从一张工作表处理到另一个工作簿中的另一张工作表。没有变量,这对我很有效:

Dim RNG1 as Range, RNG2 as Range
Sub CopyRange()
    Set RNG1 = Workbooks("Program.xlsm").Sheets("Input").Range("G9:G19")
    Set RNG2 = Workbooks("Data.xlsx").Sheets("Reformatted").Range("A3:A13")
    RNG1.Copy RNG2
End Sub

我希望能够将“Data.xlsx”作为变量传递给 SUB。我试过使用字符串变量,但没有找到解决方案。

任何帮助,将不胜感激。

4

2 回答 2

1
Sub CopyRange(wbName as string)
    Dim RNG1 as Range, RNG2 as Range 'these need only to be Local variables
    Set RNG1 = Workbooks("Program.xlsm").Sheets("Input").Range("G9:G19")
    Set RNG2 = Workbooks(wbName).Sheets("Reformatted").Range("A3:A13")
    RNG1.Copy RNG2
End Sub

利用:

CopyRange "Data.xlsx"
于 2013-10-24T21:56:56.490 回答
0

这个怎么样?

Dim RNG1 As Range, RNG2 As Range
Sub test()
    Call CopyRange(Workbooks("Data.xlsx"))
End Sub

Sub CopyRange(ByRef wb As Excel.Workbook)
    Set RNG1 = Workbooks("Program.xlsm").Sheets("Input").Range("G9:G19")
    Set RNG2 = wb.Sheets("Reformatted").Range("A3:A13")
    RNG1.Copy RNG2
End Sub
于 2013-10-24T21:32:33.443 回答