vba 运行时错误 '1004' - 对象 '_Global' 的方法 'Range' 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20601110/
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 17:24:08  来源:igfitidea点击:

Run-time error '1004' - Method 'Range' of object'_Global' failed

excelexcel-vbaexcel-2007vba

提问by s0up2up

I have a problem in VBA with a line throwing back an error.

我在 VBA 中遇到了一个问题,一行抛出错误。

What the macro is intended to do is find a particular cell then paste data into it.

宏的目的是找到一个特定的单元格,然后将数据粘贴到其中。

The code is as following:

代码如下:

'To find Column of Customer imput
For Each cell In Range("B4:M4")

        If cell.Value = strLeftMonth Then
            DataImportColumn = cell.Column

        End If

Next


For Each cell In Worksheets("data customer monthly 2013").Range("A3:A9999")

'First Customer
If cell.Value = strFirstCustomer Then
        DataImportRow = cell.Row

    Range(DataImportColumn & DataImportRow).Offset(0, 2).Value = iFirstCustomerSales ****
End If

After running the above code; The code crashes giving the 1004 run-time erroron the asterisk'dline. Also DataImportColumnhas a value of 7and DataImportRowhas a value of 5.

运行上述代码后;代码崩溃1004 run-time errorasterisk'd。也DataImportColumn具有7DataImportRow的值5

Now my concern is that Columns aren't referenced as numbers but letters, so it must be that my code can never work because its a terrible reference.

现在我担心的是,列不是作为数字而是以字母形式引用的,所以一定是我的代码永远无法工作,因为它是一个糟糕的引用。

Does anyone have any suggestions how I can make the above work?

有没有人对我如何使上述工作有任何建议?

回答by ApplePie

Your range value is incorrect. You are referencing cell "75" which does not exist. You might want to use the R1C1 notation to use numeric columns easily without needing to convert to letters.

您的范围值不正确。您正在引用不存在的单元格“75”。您可能希望使用 R1C1 表示法轻松使用数字列,而无需转换为字母。

http://www.bettersolutions.com/excel/EED883/YI416010881.htm

http://www.bettersolutions.com/excel/EED883/YI416010881.htm

Range("R" & DataImportRow & "C" & DataImportColumn).Offset(0, 2).Value = iFirstCustomerSales

This should fix your problem.

这应该可以解决您的问题。

回答by Siddharth Rout

Change

改变

Range(DataImportColumn & DataImportRow).Offset(0, 2).Value

to

Cells(DataImportRow,DataImportColumn).Value

When you just have the row and the column then you can use the cells()object. The syntax is Cells(Row,Column)

当您只有行和列时,您就可以使用该cells()对象。语法是Cells(Row,Column)

Also one more tip. You might want to fully qualify your Cellsobject. for example

还有一个提示。您可能希望完全限定您的Cells对象。例如

ThisWorkbook.Sheets("WhatEver").Cells(DataImportRow,DataImportColumn).Value