运行时错误“1004” – object'_Global的方法“范围”失败
我在VBA中遇到了一个返回错误的问题。
macros想要做的是find一个特定的单元格,然后将数据粘贴到其中。
代码如下:
'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
运行上面的代码后, 代码崩溃,在asterisk'd
行上给出1004 run-time error
。 此外, DataImportColumn
的值为7
, DataImportRow
的值为5
。
现在我担心的是列不被引用为数字,但字母,所以它必须是我的代码不能工作,因为它是一个可怕的参考。
有没有人有任何build议,我可以做出上述工作?
您的范围值不正确。 您正在引用不存在的单元格“75”。 您可能需要使用R1C1表示法轻松使用数字列,而无需转换为字母。
http://www.bettersolutions.com/excel/EED883/YI416010881.htm
Range("R" & DataImportRow & "C" & DataImportColumn).Offset(0, 2).Value = iFirstCustomerSales
这应该解决您的问题。
更改
Range(DataImportColumn & DataImportRow).Offset(0, 2).Value
至
Cells(DataImportRow,DataImportColumn).Value
当你只有行和列时,你可以使用cells()
对象。 语法是Cells(Row,Column)
还有一个小费。 您可能想完全限定您的Cells
对象。 例如
ThisWorkbook.Sheets("WhatEver").Cells(DataImportRow,DataImportColumn).Value