VBA设置表错误

时间:2018-02-22 17:18:09

标签: excel-vba vba excel

我试图通过将工作表1上的所有数据粘贴在工作表2的底部,然后将工作表2中的所有数据粘贴到工作表2的底部并将其粘贴到工作表3的底部,从而使3个电子表格组合数据。是宏开始设置' sh'变量,我得到一个"运行时错误' 9':下标超出范围"

'   Combines all 3 spreadsheets into one

Workbooks.Open "C:\excel\BRCO.CSV"
Workbooks.Open "C:\excel\BRC2.CSV"
Workbooks.Open "C:\excel\BRC3.CSV"

Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, lr As Long, rng As 
Range

Set sh1 = Sheets("BRCO.csv")
Set sh2 = Sheets("BRC2.csv")
Set sh3 = Sheets("BRC3.csv")
lr = sh1.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh1.Range("A1:A" & lr)
rng.EntireRow.Copy sh2.Cells(Rows.Count, 1).End(xlUp)(2)

lr = sh2.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh2.Range("A1:A" & lr)
rng.EntireRow.Copy sh3.Cells(Rows.Count, 1).End(xlUp)(2)

真的很感激任何帮助!

1 个答案:

答案 0 :(得分:0)

替换:

Set sh1 = Sheets("BRCO.csv")

使用:

Set sh1 = Sheets("BRCO")

并在适当的Open

之后立即

其他人一样。