我有一个8页的excel文件,我想将范围与从“ adws”,“ chws”,“ homelines”,“ ariel”,“ kit”,“ philip”和“ timmy”到工作表“ ws”,但标题除外。
这是我的代码:
Private Sub combine_btn_Click()
Dim ws As Worksheet
Dim adws As Worksheet
Dim chws As Worksheet
Dim homelines As Worksheet
Dim ariel As Worksheet
Dim kit As Worksheet
Dim philip As Worksheet
Dim timmy As Worksheet
Dim adws_co As Long
Dim chws_co As Long
Dim homelines_co As Long
Dim ariel_co As Long
Dim kit_co As Long
Dim philip_co As Long
Dim timmy_co As Long
Dim ws_co As Long
Set ws = ThisWorkbook.Sheets("Data")
Set adws = ThisWorkbook.Sheets("SL - Adult")
Set chws = ThisWorkbook.Sheets("SL - Children")
Set homelines = ThisWorkbook.Sheets("Homelines & Acc")
Set ariel = ThisWorkbook.Sheets("Hardlines - Ariel")
Set kit = ThisWorkbook.Sheets("Hardlines - Kit")
Set philip = ThisWorkbook.Sheets("Hardlines - Philip")
Set timmy = ThisWorkbook.Sheets("Hardlines - Timmy")
ws_co = ws.Range("A2:F" & Rows.Count).End(xlDown).Row + 1
adws_co = adws.Range("D2:F" & Rows.Count).End(xlDown).Row + 1
chws_co = chws.Range("D2:F" & Rows.Count).End(xlDown).Row + 1
homelines_co = homelines.Range("D2:F" & Rows.Count).End(xlDown).Row + 1
ariel_co = ariel.Range("D2:F" & Rows.Count).End(xlDown).Row + 1
kit_co = kit.Range("D2:F" & Rows.Count).End(xlDown).Row + 1
philip_co = philip.Range("D2:F" & Rows.Count).End(xlDown).Row + 1
timmy_co = timmy.Range("D2:F" & Rows.Count).End(xlDown).Row + 1
adws.Range("D2:F" & adws_co).SpecialCells(xlCellTypeVisible).Copy
ws.Select
Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
chws.Range("D2:F" & chws_co).SpecialCells(xlCellTypeVisible).Copy
ws.Select
Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
homelines.Range("D2:F" &homelines_co).SpecialCells(xlCellTypeVisible).Copy
ws.Select
Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
ariel.Range("D2:F" & ariel_co).SpecialCells(xlCellTypeVisible).Copy
ws.Select
Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
kit.Range("D2:F" & kit_co).SpecialCells(xlCellTypeVisible).Copy
ws.Select
Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
philip.Range("D2:F" & philip_co).SpecialCells(xlCellTypeVisible).Copy
ws.Select
Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
timmy.Range("D2:F" & timmy_co).SpecialCells(xlCellTypeVisible).Copy
ws.Select
Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
但是,当工作表“ philip”为空时,我收到以下错误:对象“ _Worksheet”的方法“ Range”在此行失败:
philip.Range("D2:F" & philip_co).SpecialCells(xlCellTypeVisible).Copy
我可以知道有什么办法解决吗?预先感谢!
答案 0 :(得分:2)
Excel中的最大行是1048576。如果“ philip”表为空,则意味着philip_co的值为1048577(在函数中加1)。现在,如果您尝试选择/复制D2:F1048577的范围,则Excel将抛出错误,因为该范围根本不存在。
答案 1 :(得分:1)
即使这不是解决问题的答案,但您已经参考了图纸,因此您无需进行任何选择。就是这样:
Private Sub combine_btn_Click()
Dim ws As Worksheet
Dim adws As Worksheet
Dim chws As Worksheet
Dim homelines As Worksheet
Dim ariel As Worksheet
Dim kit As Worksheet
Dim philip As Worksheet
Dim timmy As Worksheet
Dim adws_co As Long
Dim chws_co As Long
Dim homelines_co As Long
Dim ariel_co As Long
Dim kit_co As Long
Dim philip_co As Long
Dim timmy_co As Long
Dim ws_co As Long
With ThisWorkbook
Set ws = .Sheets("Data")
Set adws = .Sheets("SL - Adult")
Set chws = .Sheets("SL - Children")
Set homelines = .Sheets("Homelines & Acc")
Set ariel = .Sheets("Hardlines - Ariel")
Set kit = .Sheets("Hardlines - Kit")
Set philip = .Sheets("Hardlines - Philip")
Set timmy = .Sheets("Hardlines - Timmy")
End With
ws_co = ws.Cells(ws.Rows.Count, 6).End(xlUp).Row + 1
adws_co = adws.Cells(adws.Rows.Count, 6).End(xlUp).Row + 1
chws_co = chws.Cells(chws.Rows.Count, 6).End(xlUp).Row + 1
homelines_co = homelines.Cells(homelines.Rows.Count, 6).End(xlUp).Row + 1
ariel_co = ariel.Cells(ariel.Rows.Count, 6).End(xlUp).Row + 1
kit_co = kit.Cells(kit.Rows.Count, 6).End(xlUp).Row + 1
philip_co = philip.Cells(philip.Rows.Count, 6).End(xlUp).Row + 1
timmy_co = timmy.Cells(timmy.Rows.Count, 6).End(xlUp).Row + 1
With ws
adws.Range("D2:F" & adws_co).SpecialCells(xlCellTypeVisible).Copy
.Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
chws.Range("D2:F" & chws_co).SpecialCells(xlCellTypeVisible).Copy
.Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
homelines.Range("D2:F" & homelines_co).SpecialCells(xlCellTypeVisible).Copy
.Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
ariel.Range("D2:F" & ariel_co).SpecialCells(xlCellTypeVisible).Copy
.Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
kit.Range("D2:F" & kit_co).SpecialCells(xlCellTypeVisible).Copy
.Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
philip.Range("D2:F" & philip_co).SpecialCells(xlCellTypeVisible).Copy
.Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
timmy.Range("D2:F" & timmy_co).SpecialCells(xlCellTypeVisible).Copy
.Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
End With
End Sub
此外,在没有进一步信息的情况下,您的代码对我来说还不错。
编辑:与Justyna答案排序,没看到那个!