使用VBA从第二个工作簿复制和粘贴时,types不匹配

我有两个excel文件,我只需要将原始数据表中的必要行复制到我的新工作簿中。

我到目前为止的代码是:

Sub Pop_Sheet() Dim WB As Workbook ' Declare variables and types Dim WS As Worksheet Dim NumRows As Integer Dim BadCols As Range Dim BadRow As Range Set WB = Get_File() ' Get file from user Set WS = WB.Sheets(1) ' Select first sheet in workbook ' Delete unneeded rows/columns Set BadCols = WS.Range("A:A,B:B,D:D,G:G,H:H,I:I,J:J,K:K,L:L,M:M,P:P,Q:Q,R:R,S:S,V:V,W:W,X:X,AA:AA,AC:AC,AE:AE,AF:AF,AG:AG,AH:AH,AI:AI,AL:AL,AO:AO,AP:AP,AV:AV,AW:AW,AX:AX,AY:AY,AZ:AZ,BA:BA,BB:BB,BC:BC,BD:BD,BE:BE,BF:BF,BG:BG") Set BadRow = WS.Rows("1:1") BadCols.Delete BadRow.Delete WS.Columns("N:N").Select ' Insert empty column at N Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove NumRows = WS.Range("A1048576").End(xlUp).Row ' Count number of rows used in raw data Rows("5:" + (5 + NumRows)).Select ' Insert X rows above...It should at least... Errors out before here ActiveCell.EntireRow.Insert WS.Range("A1:U" + NumRows).Select.Copy ' Copy and paste ??? Rows("4:" + (4 + NumRows)).Select.Paste Close_File (WB) ' Clean up, free memory Set WB = Nothing Set WS = Nothing Set BadCols = Nothing Set BadRow = Nothing End Sub Private Function Get_File() As Workbook Dim FPath As Variant ' Declare variable for filepath ' Get path from file picker FPath = Application.GetOpenFilename(FileFilter:="Excel Files (*.xlsx), *.xlsx", Title:="Select File To Import") If Not FPath = False Then ' If a file was chosen Set Get_File = Workbooks.Open(FPath) ' Open file Else Set Get_File = Nothing ' Otherwise do nothing End If Set FPath = Nothing ' Free Memory End Function Private Sub Close_File(ByRef WB As Workbook, Optional Save As Boolean = False) WB.Close (Save) ' Close raw data workbook End Sub 

当我运行这个时,我得到了“types不匹配”错误。 我相信它与复制和粘贴到不同大小的单元格有关,这就是为什么我试图插入所需的数字,select这些数字并粘贴到它们中。

一般的帮助或指导,真的很感激。 谢谢!

简短的答案是这条线是不正确的:

 Rows("5:" + (5 + NumRows)).Select 

相反,您需要使用“连接”运算符“&”:

 Rows("5:" & (5 + NumRows)).Select 

你需要修复其他行,你做同样的事情。 “&”结合了两个string。

长时间的答案是,你的代码可以清理一下,使其更快,更容易阅读。 我的主要build议是不要“select”事情,然后“做”行动 – 只要将行动附加到范围本身(用**标记的编辑):

 Sub Pop_Sheet() Dim WB As Workbook ' Declare variables and types Dim WS As Worksheet Dim NumRows As Integer Dim BadCols As Range Dim BadRow As Range Set WB = Get_File() ' Get file from user Set WS = WB.Sheets(1) ' Select first sheet in workbook ' Delete unneeded rows/columns Set BadCols = WS.Range("A:B,D:D,G:M,P:S,V:X,AA:AA,AC:AC,AE:AI,AL:AL,AO:AP,AV:BG") 'Can select multiple adjacent columns by using start and end columns Set BadRow = WS.Rows(1) ' ** This function can just take a number rather than the range string BadCols.Delete BadRow.Delete WS.Columns("N:N").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove ' ** Combine two steps NumRows = WS.UsedRange.Rows.Count ' ** Count number of rows used in raw data WS.Range(WS.Rows(5),WS.Rows(5 + NumRows)).Insert ' ** Should work better WS.Range("A1",WS.Cells(NumRows,21)).Copy destination:=Range(Rows(4),Rows(4+NumRows)) ' Copy and paste ??? Close_File (WB) ' Clean up, free memory Set WB = Nothing Set WS = Nothing Set BadCols = Nothing Set BadRow = Nothing End Sub 

replace这个:

 Rows("5:" + (5 + NumRows)).Select ActiveCell.EntireRow.Insert WS.Range("A1:U" + NumRows).Select.Copy Rows("4:" + (4 + NumRows)).Select.Paste 

有了这个:

 Rows("5:" & (5 + NumRows)).EntireRow.Insert WS.Range("A1:U" + NumRows).Copy WS.Paste Destination:=Range("D1") 

让我知道这是如何工作。