Excel VBA Run-Time Error '1004'

Excel Jason

New Member
Joined
Oct 17, 2022
Messages
30
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a macro that takes an item number and duplicates it 'x' amount of times based on a number is an adjacent cell. The macro works fine but I still receive this error once it completes it's run. "Run-time error '1004': Application-defined or object-defined error"

VBA Code:
Sub RepeatData()
'Repeat Rows
Dim XRg As Range
Dim InRg As Range, OutXRg As Range
'A Box Named Repeat Rows will Show
Set InRg = Range("B5:C120")
Set OutXRg = Range("H5")
Set OutXRg = OutXRg.Range("A1")
For Each XRg In InRg.Rows
xValue = XRg.Range("A1").Value
xNum = XRg.Range("B1").Value
OutXRg.Resize(xNum, 1).Value = xValue
Set OutXRg = OutXRg.Offset(xNum, 0)
Next
End Sub

Thank You!

1666047504776.png

1666047722237.png

1666047758360.png
 
Thank You for the response; Unfortunately, I received another error.

1667350713105.png


1667350723746.png


VBA Code:
Sub RepeatData()

'Repeat Rows
Dim XRg As Range
Dim InRg As Range, OutXRg As Range
Dim xValue As Long
Dim xNum As Long


Set InRg = Range("D5:E200")
Set OutXRg = Range("J5")
Set OutXRg = OutXRg.Range("A1")

For Each XRg In InRg.Rows

xNum = XRg.Range("B1").Value
OutXRg.Resize(xNum, 1).Value = XRg.Range("A1").Value
Set OutXRg = OutXRg.Offset(xNum, 0)

Next

End Sub

Also - Thank you for the Code Paste recommendation! I gave it a try just now, let me know if the visual is better.
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
What value are you expecting to get into xNum in that line of code? Your range references are a little convoluted and I want to review what you are doing.

VBA Code:
Set InRg = Range("D5:E200")


For Each XRg In InRg.Rows
XRg will be the section of one row of the range, so D5:E5, then D6:E6, and so on.
VBA Code:
xNum = XRg.Range("B1").Value
Range B1 of that section is relative to where the range starts, so will be the second column, first row. So the cells will be E5, E6, E7, and so on. Is that what you were expecting?

Last of all, what is the value that is being attempted to assign to xNum on the line of code that raises the error? You are getting an error because it is not a number.
 
Upvote 0
Ok, so I had some time to review and I'll just come out and say that I'm not sure what I need that variable to be, others on this site were helping me; However, I will outline what end result I'm trying to achieve and flesh out some details hoping it will help and/or allow us to achieve this with formulas over macros?

Goal: I want to take an item number and duplicates it 'x' amount of times based on a number is an adjacent cell.

So essentially, take Item 10221463 and duplicate 104 times, then move on to item 10224680 and duplicate it 104 times and so on....so essentially 17,000+ lines of Item numbers.

Sorry for the ramble; Hopefully restating my desired results help clearify any confusion.

1667529711815.png
 
Upvote 0
Your original code set InRg to Range("B5:C120"). This matched the data you showed. Your latest code refers columns D:E instead of B:C which does not match the data you are showing. Columns D and E are blank. I am not sure why you took my original working solution and changed it.
 
Upvote 0
Hey String,

I shifted a few columns so that's why my referenced columns are now D:E instead of B:C. My understanding is that it shouldn't affect the VBA it just pasted the data in a new location. Is there anything else in the below script that I can adjust to get rid of the error?

Thanks!

VBA Code:
Sub RepeatData()

'Repeat Rows
Dim XRg As Range
Dim InRg As Range, OutXRg As Range
Dim xValue As Long
Dim xNum As Long


Set InRg = Range("D5:E200")
Set OutXRg = Range("J5")
Set OutXRg = OutXRg.Range("A1")

For Each XRg In InRg.Rows

xNum = XRg.Range("B1").Value
OutXRg.Resize(xNum, 1).Value = XRg.Range("A1").Value
Set OutXRg = OutXRg.Offset(xNum, 0)

Next

End Sub
 
Upvote 0
I shifted a few columns
Your code is very specific to your worksheet layout and the error is most likely related to that. If you are still getting an error, the only possible way to diagnose this is to review the code and also the exact data you are using as it appears on the worksheet. If you show new code but an old version of the worksheet then we'll just be spinning wheels.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,161
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top