[英]Why do I get an VBA Run-time error '424' Object Required Error from this code?
[英]Why do I get object required error for the below VBA code?
我是 VBA 新手,我需要帮助将以下 VBA 代码按区域和模型应用于两个特定的工作表。 该代码仅查找具有年份名称总计的最后一列,并将前几个月的值复制到新列中。 但是我在 ws 处得到了一个错误对象。
Sub Insert_New_Col()
Dim Found As Range, BeforeR As Long
Dim xSheets As Variant
Dim ws As Worksheet
Dim i As Long
xSheets = Array("By Region", "By Model") '
For i = LBound(xSheets) To UBound(xSheets)
Set ws = xSheets(i)
Set Found = ws.Rows(3).Find(What:="Total for the Year", Lookat:=xlWhole)
BeforeR = R.Column - 1
If Found Is Nothing Then
MsgBox ("The word 'Totals' was not found in Row 5 on Sheet: " & ws.Name)
Else
Columns(BeforeR).Copy
ws.Columns(R.Column).Insert Shift:=xlRight
End If
Next i
End Sub
您需要Worksheets
集合的实际成员:
Set ws = ThisWorkbook.Worksheets(xSheets(i))
BeforeR = R.Column - 1
什么是R
? 在模块顶部添加Option Explicit
并声明所有变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.