vba 对象“_Global”的方法“Range”失败。错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7747728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:02:55  来源:igfitidea点击:

Method 'Range' of object '_Global' failed. error

excel-vbavbaexcel

提问by franklin

I'm trying to get Excel to figure out which columns of a worksheet are blank. Eventually the idea is to get it to delete the completely blank columns. Here's the code I have so far:

我试图让 Excel 找出工作表的哪些列是空白的。最终的想法是让它删除完全空白的列。这是我到目前为止的代码:

Sub Macro2()
'
' Macro2 Macro
'
Dim totalCols As Integer
Dim totalRows As Integer

totalCols = ActiveSheet.UsedRange.Columns.Count
totalRows = ActiveSheet.UsedRange.Rows.Count

Dim i As Integer
Dim j As Integer
Dim numNull As Integer

For i = 1 To totalCols
    For j = 2 To totalRows
        Dim location As String
        location = "R" & i & ":" & "C" & j
        If Range(location).Select = "" Then
            numNull = numNull + 1
        End If
    Next j
    If numNull = totalRows - 1 Then
        MsgBox ("Column " & i & "is null")
    End If
Next i

End Sub

At the end it checks to see if numNull(number of null entries in the row) = totalRows minus the header. I had it working up until the statement If Range(location).Select = "". Now the compiler says:

最后,它检查是否numNull(行中的空条目数)= totalRows 减去标题。我一直在努力直到声明If Range(location).Select = ""。现在编译器说:

Method 'Range' of object '_Global' failed

对象“_Global”的方法“范围”失败

Does anyone know what this means or how I can fix it?

有谁知道这意味着什么或我如何解决它?

回答by Tim Williams

Your code is using .Selectwhen you should be using .Value

.Select当您应该使用时,您的代码正在使用 .Value

This might be faster though:

不过,这可能会更快:

Sub Tester()

    Dim col As Range, ur As Range
    Dim numRows As Long, numCols As Long, i As Long, awf

    Set awf = Application.WorksheetFunction
    Set ur = ActiveSheet.UsedRange

    numRows = ur.Rows.Count
    numCols = ur.Columns.Count

    'edit: account for header row...
    Set ur = ur.Offset(1,0).Resize(numRows-1, numCols)
    numRows = numRows - 1

    For i = numCols To 1 Step -1
        Set col = ur.Columns(i)
        If awf.CountBlank(col) = numRows Then
            MsgBox "Column #" & col.Column & " is empty"
            'col.EntireColumn.Delete
        End If
    Next i

End Sub

回答by dmogle

At this point 'Range' doesn't refer to an instantiated object. The Range class can't be used statically this way - you have to instantiate a Range object, usually by calling an appropriate object's factory method. There are probably other ways to do it, but I generally call ActiveSheet.Range.

在这一点上,'Range' 不是指实例化的对象。Range 类不能以这种方式静态使用 - 您必须实例化 Range 对象,通常是通过调用适当对象的工厂方法。可能还有其他方法可以做到,但我通常调用 ActiveSheet.Range。

As noted by Tim Williams above, you should probably be using .Value rather than .Select; I'm not at all sure what .Select returns, the MSDN reference just says data type variant.

正如上面 Tim Williams 所指出的,您可能应该使用 .Value 而不是 .Select;我完全不确定 .Select 返回什么,MSDN 参考只是说数据类型变体。

I'm not at a Windows machine to test, but try:

我不是在 Windows 机器上进行测试,但请尝试:

If ActiveSheet.Range(location).Value = "" Then

MSDN Range Object Reference

MSDN 范围对象参考