简体   繁体   English

对象工作表的方法范围失败excel VBA

[英]Method Range of Object Worksheet failed excel VBA

I have been stuck on a particular problem I am having with an excel visual basic macro. 我一直在使用excel视觉基本宏遇到一个特定的问题。 I have written a script to simply paste a dynamic range of data from one worksheet to the other, The sheet that is being pasted to is a running sheet that updates to the next empty row each time new data is sent to it. 我已经编写了一个脚本,将一个动态范围的数据从一个工作表简单地粘贴到另一个工作表。粘贴到的工作表是一个正在运行的工作表,每当向其发送新数据时,该工作表就会更新到下一个空行。 For certain ranges of data my code breaks and gives me the 1004 error. 对于某些数据范围,我的代码中断并给我1004错误。 I have looked at multiple similar questions and they don't seem to have given me any solutions. 我看过多个类似的问题,但它们似乎并没有给我任何解决方案。

I will first post my code then a data set that works and a data set that does not. 我将首先发布我的代码,然后发布一个有效的数据集和一个无效的数据集。

Private Sub RunningSheetClick_Click()

Dim lastrow As Long

Dim Vals As Range

Dim copyWS As Worksheet
Dim pasteWS As Worksheet

Set pasteWS = Sheets("Full List")
Set copyWS = Sheets("Macro Test")
Setlastrow = Sheets("Full List").Range("A" & Rows.count).End(xlUp).Row + 1

copyWS.Activate

' the following line is where my error appears
Set Vals = copyWS.Range("B3:S" & copyWS.Range("B3").End(xlDown))

Vals.Select

Vals.Copy _
    Destination:=pasteWS.Range("A" & lastrow)

End Sub

This is a data set that works and exports correctly to the sheet "Full List" 这是一个数据集,可以正常工作并将其正确导出到工作表“完整列表”

This is a data set that does not, I believe it has to do with column B and the strings that are in those cells as compared to the integers in the first data set 这是一个数据集,与第一个数据集的整数相比,我认为它与B列和那些单元格中的字符串有关

Any help would be appreciated, thank you for taking the time 任何帮助,将不胜感激,谢谢您抽出宝贵的时间

This is bad syntax that should never work. 这是语法错误,永远不会起作用。 You just cannot concatenate a range object onto a string and expect a valid range object. 您只是不能将范围对象连接到字符串上,而期望有效的范围对象。 I suspect it works with numbers since copyWS.Range("B3").End(xlDown) is returning its cell value by default. 我怀疑它可以与数字一起使用,因为copyWS.Range("B3").End(xlDown)默认返回其单元格值。 So if the value at the bottom of column B was 99, you would be resolved to Range("B3:S99") . 因此,如果B列底部的值为99,则将解析为Range("B3:S99")

Set Vals = copyWS.Range("B3:S" & copyWS.Range("B3").End(xlDown))

It should be something like, 应该是这样的

with copyWS
    Set Vals = .Range(.cells(3, "B"),  .cells(rows.count, "B").end(xlup).offset(0, 17))
end with

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM