首页 文章

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

提问于
浏览
1

我一直坚持使用excel visual basic宏的特定问题 . 我编写了一个脚本来简单地将动态范围的数据从一个工作表粘贴到另一个工作表中 . 正在粘贴的工作表是一个运行工作表,每次向其发送新数据时都会更新到下一个空行 . 对于某些数据范围,我的代码会中断并给出1004错误 . 我看过多个类似的问题,他们似乎没有给我任何解决方案 .

我将首先发布我的代码然后发布一个有效的数据集和一个没有的数据集 .

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

任何帮助将不胜感激,谢谢你花时间

1 回答

  • 0

    这是不应该工作的错误语法 . 您无法将范围对象连接到字符串并期望有效的范围对象 . 我怀疑它适用于数字,因为 copyWS.Range("B3").End(xlDown) 默认返回其单元格值 . 因此,如果B列底部的值为99,您将被解析为 Range("B3:S99") .

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

    它应该是这样的,

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

相关问题