2017-06-15 228 views
0

我正在使用此宏將數據從工作簿'x'中的3列複製到工作簿'y',而不復制隱藏行。excel vba宏:將列信息複製到另一個工作簿

Sub GetDataDemo() 
Const FileName As String = "EHS.xlsx" 
Const SheetName As String = "PO" 
FilePath = "C:\Users\DD\Desktop\" 
Dim wb As Workbook 
Dim this As Worksheet 
Dim i As Long, ii As Long 

Application.ScreenUpdating = False 

If IsEmpty(Dir(FilePath & FileName)) Then 

    MsgBox "The file " & FileName & " was not found", , "File Doesn't Exist" 
Else 

    Set this = ActiveSheet 

    Set wb = Workbooks.Open(FilePath & FileName) 

    With wb.Worksheets(SheetName).Range("Y:AA") 

     ii = 3 
     For i = 3 To 500 

      If Not .Rows(i).Hidden Then 

       .Cells(i).Copy 
       this.Range("P:R").Cells(ii).Paste 
       ii = ii + 1 
      End If 
     Next i 
    End With 
End If 

ActiveWindow.ScreenUpdating = True 
End Sub 

我不斷收到自動化錯誤(錯誤440)附近的行:

this.Range("P:R").Cells(ii).Paste 

感謝您的幫助提前!

回答

2

您應該可以複製&批量粘貼可見的單元格/行。

With wb.Worksheets(SheetName).Range("Y3:AA500") 
    on error resume next 
    .SpecialCells(xlcelltypevisible).Copy this.Range("P3") 
    on error goto 0 
End With 

。粘貼是工作表的成員,而不是範圍或單元格的成員。

this是保留名稱;將保留名稱重用爲變量不被認爲是「最佳做法」。

+2

不應該是'.SpecialCells(xlcelltypevisible).Copy this.Range(「P3」)'? – SJR

+0

@SJR - 這不是做同樣的事情嗎? – BruceWayne

+1

@BruceWayne - 'Paste'不是'Range'對象的方法。 (它是'Worksheet.Paste'或'Range.PasteSpecial'。) – YowE3K

相關問題