我一直在第一行收到VBA错误“编译错误:无效或不合格引用”?
为什么宏的第一行出现错误?”亚宏观5’?我以前从未遇到过这样的问题。我觉得这一定很简单我一直在第一行收到VBA错误“编译错误:无效或不合格引用”?,vba,excel,Vba,Excel,为什么宏的第一行出现错误?”亚宏观5’?我以前从未遇到过这样的问题。我觉得这一定很简单 Sub Macro5() ' ' MacroNew Macro ' Application.ScreenUpdating = False Dim j As Integer Dim k As Integer Worksheets("Resumen").Activate Columns("Q:V").EntireColumn.Delete j = 3 Do While Not IsEmpty(Resum
Sub Macro5()
'
' MacroNew Macro
'
Application.ScreenUpdating = False
Dim j As Integer
Dim k As Integer
Worksheets("Resumen").Activate
Columns("Q:V").EntireColumn.Delete
j = 3
Do While Not IsEmpty(Resumen.Cells(j, "A"))
If Not Resumen.Cells(j, 1).Interior.ColorIndex = xlNone Then
Resumen.Range(.Cells(j, 1), Cells(j, 2)).Delete Shift:=xlToUp
End If
Loop
j = j + 1
Application.ScreenUpdating = True
End Sub
下面几行
Resumen.Range(.Cells(j, 1), Cells(j, 2)).Delete Shift:=xlToUp
应该是
.单元格与with和End with块一起使用除非'Resumen'是一个代码名,否则必须告诉VBE什么是'Resumen'。在本例中,我将其声明为工作表对象,并将工作表对象设置为指向运行代码的工作簿中的“Resumen”工作表。现在应该运行良好了,我也修复了无限While循环
Sub SomeMacro()
' Name your macros with some kind of informative name
Application.ScreenUpdating = False
' 'k' is never used, delete this.
' Dim k As Integer
Dim Resumen As Worksheet
Set Resumen = ThisWorkbook.Worksheets("Resumen")
' You should avoid activate and select like the plague. Qualify and directly modify instead.
' Worksheets("Resumen").Activate
'
' This reference is unqualified, and operates on the active sheet. Use a qualification instead.
' Columns("Q:V").EntireColumn.Delete
Resumen.Columns("Q:V").EntireColumn.Delete
' Declared j as Long instead of Integer. Otherwise you will eventually hit an overflow error. Always use Long over Integer.
Dim j As Long
j = 3
' If Resumen is a codename (you named the sheet directly using the VBE) then this would work fine
' without first declaring 'Resumen' as a variable, and then setting it properly.
Do While Not IsEmpty(Resumen.Cells(j, "A"))
If Not Resumen.Cells(j, 1).Interior.ColorIndex = xlNone Then
Resumen.Range(Resumen.Cells(j, 1), Resumen.Cells(j, 2)).Delete Shift:=xlToUp
End If
' Moved this within the loop since otherwise you will have an infinite loop
j = j + 1
Loop
' You could also use a with block here instead:
' With Resumen
' Do While Not IsEmpty(.Cells(j, "A"))
' If Not .Cells(j, 1).Interior.ColorIndex = xlNone Then
' .Range(.Cells(j, 1), .Cells(j, 2)).Delete Shift:=xlToUp
' End If
'
' j = j + 1
' Loop
' End With
Application.ScreenUpdating = True
End Sub
您引用的简历表不正确。看看这里:为什么这个问题被否决了?对于编程新手来说,这是一个完全合理的问题。@BrandonBarney我认为这是因为这个问题远远不是一个好问题。寻求调试帮助的问题此代码为什么不起作用?必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题说明的问题对其他读者来说是没有用处的。公平地说,这个问题肯定会更好,但当他解释错误信息时,问题已经非常清楚地说明了。我肯定可以先通过一些研究来解决他的问题,但这也可以作为一个很好的例子,说明什么是不应该做的,以及如何纠正可能导致更大问题的错误。我想,否决票是有道理的,但这个问题最终还是值得一提的。它实际上应该是“Resumen.Cells”,而不仅仅是“Cells”。虽然它在不限定单元格引用的情况下可以工作,但毫无疑问,他会遇到一个问题,即一旦错误的工作表激活,就会抓取错误的值。除了@BrandonBarney的评论外:您可以读入。很好,抓到Brandon!要完全限定范围,单元格还应与图纸参考一起限定。完全同意你的意见
Sub SomeMacro()
' Name your macros with some kind of informative name
Application.ScreenUpdating = False
' 'k' is never used, delete this.
' Dim k As Integer
Dim Resumen As Worksheet
Set Resumen = ThisWorkbook.Worksheets("Resumen")
' You should avoid activate and select like the plague. Qualify and directly modify instead.
' Worksheets("Resumen").Activate
'
' This reference is unqualified, and operates on the active sheet. Use a qualification instead.
' Columns("Q:V").EntireColumn.Delete
Resumen.Columns("Q:V").EntireColumn.Delete
' Declared j as Long instead of Integer. Otherwise you will eventually hit an overflow error. Always use Long over Integer.
Dim j As Long
j = 3
' If Resumen is a codename (you named the sheet directly using the VBE) then this would work fine
' without first declaring 'Resumen' as a variable, and then setting it properly.
Do While Not IsEmpty(Resumen.Cells(j, "A"))
If Not Resumen.Cells(j, 1).Interior.ColorIndex = xlNone Then
Resumen.Range(Resumen.Cells(j, 1), Resumen.Cells(j, 2)).Delete Shift:=xlToUp
End If
' Moved this within the loop since otherwise you will have an infinite loop
j = j + 1
Loop
' You could also use a with block here instead:
' With Resumen
' Do While Not IsEmpty(.Cells(j, "A"))
' If Not .Cells(j, 1).Interior.ColorIndex = xlNone Then
' .Range(.Cells(j, 1), .Cells(j, 2)).Delete Shift:=xlToUp
' End If
'
' j = j + 1
' Loop
' End With
Application.ScreenUpdating = True
End Sub