Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Run-time error '1004' (VBA code cell range error)

0

I have been using this report with no issues until the supplier changed their source report format.

i now get this error that appears to be in relation to selecting the copy range. 

attached is

1 screen shot of debug error message

2 suppliers new report (raw data) called "training matrix"

3 "report" - the raw data is copied to "sheet 1" then in sheet "VOC" you click the green "import columns" and the script runs. 

but now there is a run-time error !!! grrrr

Answer
Discuss

Discussion

Hi again Sue.

I vaguely remember working in this some time ago. I don't get the error you mentioned above- if I launch Training Matrix.xlsx and - in Report.xlsm - click the Import Columns button, I get the MsgBox stating:
Imported from source 09/04/2024 11:54:28 but didn't find matches for report headers:
 
HLTAID001 - CPR - Refresher due every 12 months
SP029 - Vehicles and Driving
SP030 - Heavy Vehicle National Law
 
SP017 - Articulated Haul and Haul Truck Operations
SP033 - Haul Truck
SP017 - Articulated Haul and Haul Truck Operations
 
SP021 - Vibrating Plant and Equipment
 
SP011 - Trenching and Excavations
VOC IT DOOSAN DL300
RIISAM211 - Remove, repair and refit tyres and tubes
SP016 - Plant Service and Refuelling
SP012 - Crane and Lifting Operations
HRWL - CB Bridge and Gantry Crane
SP012 - Crane and Lifting Operations
HRWL - RI Rigging Intermediate

which I had coded. Not sure if that's what you expect (I didn't look in the columns). Are you sure you attached the supplier's new Report file?

Comments please.
John_Ru (rep: 6142) Apr 9, '24 at 6:59 am
@Sue
I too tried your macro by clicking the "import columns" button. I was hoping to get the runtime error to get more specific details/description about the error. Your file with the screen shot shows the line which throws the error code but you didn't include the error description (Runtime 1004 can be caused for several reasons).
I didn't get any runtime error. I got the same result as John.
WillieD24 (rep: 557) Apr 9, '24 at 11:58 am
@willieD24
@John_Ru yes you did help me with this a long time ago, and it has been working perfectly.... until now.

if you use the report excel file as it is uploaded and click "import columns" the report will work as it should, reading the current data on sheet1

but when you copy and paste the new data from the "training matrix" excel into sheet1 and then "import columns" that is when you get the runtime error

i use  control A control C to copy and paste the data into sheet 1 

the programme that generates the "training matrix" excel has changed how the report is generated and it must be something in the new style "training matrix" excel report that is causing the error.

i have tried different ways to copy and paste the data to eliminate the error, but nothing works
sue (rep: 6) Apr 9, '24 at 5:18 pm
@John_Ru

thanks for your assistance again. your answer below worked. Have run the real reports with no issues using your update / answer
sue (rep: 6) Apr 9, '24 at 6:38 pm
Great! Thanks for the confirmation Sue. 
John_Ru (rep: 6142) Apr 9, '24 at 6:51 pm
Add to Discussion

Answers

0
Selected Answer

Sue

From the Discussion above, it seems your question .xlsm file didn't include the new Report format (so Willie and I found no error).

In the first attached file below, I deleted the existing Sheet0 and copied Sheet0 from the Report.xlsx file to that file.

I then found that a runtime error 1004 occurred because -when writing the first column (with n=1) - the array element ColXRef(1, 2) was emptyso .Cells(2, ColXRef(n, 2) would evaluate to .Cells(1,0) which doesn't exist in Excel since there is no column 0.

I think I fixed it using the simple expedient of setting it to 1 when n=1 and the array element is empty, as in bold in the code extract below:

' #4. Copy data from source to report using cross refs
' go across headers
For n = 1 To RepLstCl
    ' if first column or there's a cross ref to source
    If n = 1 Or Not IsEmpty(ColXRef(n, 2)) Then
        ' when n=1 set value to 1
        If IsEmpty(ColXRef(n, 2)) Then ColXRef(n, 2) = 1
        ' copy source rows 2 to end
        With Sheets(2)
            Set CpyRng = .Range(.Cells(2, ColXRef(n, 2)), .Cells(SrcLstRw, ColXRef(n, 2)))
            CpyRng.Copy Destination:=Sheets(1).Cells(3, n)
        End With
        Else
        'otherwise collect headings not imported
        NoX = NoX & ColXRef(n, 1) & vbCr
    End If

    ' write borders and fill background like header
    With Sheets(1).Range(Cells(3, n), Cells(SrcLstRw + 1, n))
        .Borders.LineStyle = xlContinuous
        .Interior.Color = Sheets(1).Cells(1, n).Interior.Color
    End With
Next n

The button now runs without problem (and the missing refs are reported as before).

Hope this fixes your problem- if so, please remember to mark this Answer as Selected.

Discuss

Discussion

Glad that helped Sue and thanks for selecting my Answer. It was a bit rushed (it's fairly late here) so please check that this doesn't create new problems.
John_Ru (rep: 6142) Apr 9, '24 at 6:37 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login