excel - VBA - runtime error 438 object doesn't support this property using set
问题描述
I have a simple script that declares a few variables as a range but I am getting the error runtime error 438 object doesn't support this property
- anything I'm missing?
Sub copyId()
Dim numberID As Range, column As Range, pasteId As Range
Set column = Workbooks("Book4.xlsm").Worksheets(1).Columns("C")
Set pasteId = Workbooks("Book5.xlsx").Worksheets(1).Columns("A").xlUp
For Each numberID In column
If numberID Like "########" Or numberID Like "#########" Then numberID.Copy Destination:=pasteId
Next numberID
End Sub
解决方案
xlUp
is used with Range.End
. Then, you need to determine the last cell inside the loop:
For Each numberID In column
If numberID Like "########" Or numberID Like "#########" Then
With Workbooks("Book5.xlsx").Worksheets(1)
numberID.Copy Destination:=.Cells(.Rows.Count, "A").End(xlUp).Offset(1)
End With
End If
Next
EDIT:
You (presumably) don't want to loop through every single cell in the column... find the last row instead:
Dim sourceBook As Workbook
Set sourceBook = Workbooks("Book4.xlsm")
Dim destBook As Workbook
Set destBook = Workbooks("Book5.xlsx")
Dim sourceSheet As Worksheet
Set sourceSheet = sourceBook.Worksheets(1)
Dim destSheet As Worksheet
Set destSheet = destBook.Worksheets(1)
With sourceSheet
Dim lastRow As Long
lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
End With
Dim sourceCells As Range
Set sourceCells = sourceSheet.Range("C1:C" & lastRow)
For Each numberID in sourceCells
If numberID Like "########" Or numberID Like "#########" Then
With destSheet
numberID.Copy Destination:=.Cells(.Rows.Count, "A").End(xlUp).Offset(1)
End With
End If
Next
推荐阅读
- javascript - Javascript 不会输出到 HTML
- r - tabItem 不显示 R 闪亮
- date - 如何将日期时间转换为erlang中的日期名称和月份名称?
- google-apps-script - 带有谷歌电子表格的 OnEdit 函数脚本
- python-3.x - GUI中的基本计算器
- powerquery - 自定义列电源查询编辑器 - 过滤器
- java - Swing JTextField 文本更改侦听器 DocumentListener 无限循环
- reactjs - react - setState 的简写形式
- html - 在两个文本对象上使用位置
- c# - 使用 C# 的 HP ALM REST API - 更新测试集中的数据