VBA对象必需错误

克雷格23

我已经创建了一个表格来重新格式化我收到的报告,但是我对部分报告的自动化存在疑问。一切正常,直到我定义并设置了最后一个可变代码长度,我想将其设置为已定义范围内的单元格(第一列,第二行)的长度。我收到运行时错误424,“对象必需”。感谢您的帮助!!

这是代码:

Private Sub CommandButton1_Click()


Application.ScreenUpdating = False

Dim rg As Range
Dim rgg As Range
Dim Addr1 As String
Dim Addr2 As String

'Get the address, or reference, from the RefEdit control.
Addr1 = RefEdit1.Value
Addr2 = RefEdit2.Value

'Set the SelRange Range object to the range specified in the
'RefEdit control.
Set rg = Range(Addr1)
Set rgg = Range(Addr2)

ActiveWorkbook.Names.Add Name:="codes", RefersTo:=rgg

'Infill
'Copies the value from the row above into blank cells.
Dim cel As Range, col As Range
Set rg = Range(Addr1).Columns(1).Resize(, 2)
On Error Resume Next
For Each col In rg.Columns
    Set rgg = Nothing
    Set rgg = col.SpecialCells(xlCellTypeBlanks)
    If Not rgg Is Nothing Then
        rgg.FormulaR1C1 = "=R[-1]C"     'Blank cells set equal to value from row above
        rgg.Formula = rgg.Value   'Optional:  Replace the formulas with the values returned by the formulas
    End If
Next
Set rgg = rg.Offset(1, 0).Resize(rg.Rows.Count - 1, rg.Columns.Count)
For Each cel In rgg.Cells
    If cel = "" Then cel.Value = cel.Offset(-1, 0).Value
Next
On Error GoTo 0

'ColCDeleter
Dim i As Long, n As Long
Set rg = Intersect(ActiveSheet.UsedRange, Range(Addr1).Columns(3))
n = rg.Rows.Count
For i = n To 1 Step -1
If rg.Cells(i, 1) = "" Then rg.Cells(i, 1).EntireRow.Delete
Next

'insert corresponding values
Dim codelength As Integer
codelength = Len(codes.Cells(2, 1).Value)
rg.Columns(2).EntireColumn.Insert
rg.Columns(2).EntireColumn.Insert
rg.Columns(2).EntireColumn.Insert
rg.Columns(2).EntireColumn.Insert
If codelength = 6 Then
rg.Columns(2).FormulaR1C1 = "=VLOOKUP((MID(RC1,9,9)),codes,2,FALSE)"
rg.Columns(3).FormulaR1C1 = "=VLOOKUP((MID(RC1,9,9)),codes,3,FALSE)"
Else
rg.Columns(2).FormulaR1C1 = "=VLOOKUP((MID(RC1,8,9)),codes,2,FALSE)"
rg.Columns(3).FormulaR1C1 = "=VLOOKUP((MID(RC1,8,9)),codes,3,FALSE)"
End If
rg.Cells(1, 2).Value = "Plan"
rg.Cells(1, 3).Value = "Status"


'Unload the userform.
Unload Me



End Sub
用户名

当您首先使用以下语法命名范围时

Dim rng as Range
Set rng = Range("A1:A10")

ActiveWorkbook.Names.Add Name:="codes", RefersTo:=rng

然后,这仅是一个名称-它不是一个独立的对象。因此,您得到的错误会告诉您确切的情况->所需的对象。

要引用命名的Range,请将其用双引号引起来并将其粘贴为Range对象的参数。因此,Range("codes")创建一个引用Range的Range对象rng

一种替代,省略名称将是使用的rng范围对象简单地替换Range("codes").rng.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

VBA中的对象必需错误

来自分类Dev

424对象必需错误与VBA Vlookup

来自分类Dev

Excel VBA中的对象必需错误

来自分类Dev

Excel VBA中的对象必需错误

来自分类Dev

VBA上的对象必需错误(使用VBA提交IE表单)

来自分类Dev

VBA检查表是否存在-对象必需错误

来自分类Dev

在Dictionary的excel内部引用Dictionary的字典-VBA对象必需的错误

来自分类Dev

VBA语法:运行时错误424-必需对象

来自分类Dev

访问VBA / SQL“运行时错误'424':必需对象”

来自分类Dev

VBS错误:必需的对象

来自分类Dev

Excel VBA-必需的对象

来自分类Dev

VBA脚本上的Excel VBA对象必需错误在计数器执行重复行

来自分类Dev

Excel VLookup“必需的对象”错误

来自分类Dev

Set =中的对象必需错误

来自分类Dev

vbscript-对象必需错误

来自分类Dev

For循环给出对象必需错误

来自分类Dev

用vba清除文本框-获取424对象必需错误

来自分类Dev

VBA Excel 2010 msoFileDialogOpen路径。打开运行时错误'424':必需的对象

来自分类Dev

删除项目“必需的对象” excel VBA

来自分类Dev

运行时错误“ 424”:必需对象

来自分类Dev

通过DIR()循环时对象必需错误

来自分类Dev

Access 2013编译错误-必需对象

来自分类Dev

VBA对象定义的错误

来自分类Dev

对象必需错误-为什么这超出范围?

来自分类Dev

重命名文件时出现VBScript对象必需错误

来自分类Dev

为什么在我的数组变体上出现对象必需错误?

来自分类Dev

尝试计算已用行数时出现错误424“对象必需”

来自分类Dev

带For循环的Workbook_Open中的对象必需错误

来自分类Dev

Excel运行时错误424-必需对象