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

3

5

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

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 error on the asterisk'd line. Also DataImportColumn has a value of 7 and DataImportRow has a value of 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?

Stratify answered 15/12, 2013 at 23:13 Comment(0)
E
9

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.

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

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

This should fix your problem.

Enzymology answered 15/12, 2013 at 23:20 Comment(0)
E
3

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)

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

ThisWorkbook.Sheets("WhatEver").Cells(DataImportRow,DataImportColumn).Value
Ebneter answered 16/12, 2013 at 0:31 Comment(0)
T
0

I faced this issue when i was trying to reference a cell using the code Range("AB" & row).value and the row was empty, so you can check into that as well

Terris answered 7/8, 2024 at 7:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.