[英]Type mismatch error in VBA
1.Dim destbook As Workbook
2.Dim destsheet As Worksheet
3.Set destbook = Workbooks("Book1")
4.Set destsheet = destbook.Sheets(1)
5.Workbooks("Book1").Sheets("Sheet1").Range("C6").Select
6.ct = Range(Selection, Selection.End(xlDown)).count + 1
7.destbook.Activate
8.Workbooks(destbook).Sheets(destsheet).Range("A" + ct).Select
9.Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Here, when i execute this code, it shows an error saying "type mismatch" on line 8. 在这里,当我执行此代码时,它在第8行显示一个错误,提示“类型不匹配”。
Can u help??... 你能帮忙吗?? ...
You should change + to & 您应将+更改为&
Workbooks(destbook).Sheets(destsheet).Range("A" & ct).Select Workbooks(destbook).Sheets(destsheet).Range(“ A”&ct).Select
您正在使用“ destbook”和“ destsheet”作为“ Workbooks”和“ Sheets”的索引,但实际上,它们在第1行和第2行中已定义为“ Workbook”和“ Worksheet”类型。 8到: destsheet.Range("A" + ct).Select
。
As most folks have already pointed out, you need to change the way you are referencing the desired destination cell. 正如大多数人已经指出的那样,您需要更改引用所需目标单元格的方式。 Either you can switch over to an ampersand (&), or change to just a Cells(row,col) reference as you are only updating a single cell (see code below). 您可以切换到与号(&),也可以仅更改为Cells(row,col)引用,因为您仅更新单个单元格(请参见下面的代码)。 You should also consider slimming down your code to make it a bit more efficient. 您还应该考虑精简代码以使其更有效率。
Dim destbook As Workbook
Dim destsheet As Worksheet
Set destbook = Workbooks("Book1")
Set destsheet = destbook.Sheets(1)
'See my note below
destbook.Activate
destsheet.Range("C6").Select
ct = Range(Selection, Selection.End(xlDown)).Count + 1
destsheet.Cells(ct, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Notes: - Line 5 should be changed to use your variables destbook and destsheet. 注意:-第5行应更改为使用变量destbook和destsheet。 Note that you'll need to move line 7 up to initially Activate your Workbook and then you can reference your Worksheet destsheet. 请注意,您需要将第7行向上移动才能最初激活您的工作簿,然后才能引用您的工作表目标表。 - At the "see my note below", you should probably be copying some value from somewhere, otherwise you'll run into a new error upon your PasteSpecial command. -在“请参阅下面的注释”中,您可能应该从某个位置复制一些值,否则您的PasteSpecial命令会遇到新的错误。 - You should combine line 8 and line 9 together, unless you are planning on reusing the selection from line 8 in some other code (that you have not provided here). -您应该将第8行和第9行结合在一起,除非打算在其他代码中重复使用第8行的选择(此处未提供)。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.