Excel VBA Run-Time Error '1004'

Excel Jason

New Member
Joined
Oct 17, 2022
Messages
26
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
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
When asking about a runtime error always indicate which line of code is highlighted when you click the Debug button on the error dialog. Technically you cannot have an error occur after code "completes its run" because if it completed, it is no longer running. I don't see anything glaringly obvious in your code that would cause this. Can you paste in a table of your data, instead of a picture of it, so we can try your code on the same data?
 
Upvote 0
Ah ofcourse, thank you for that feedback. Here is the portion that is highlighted as in error.

1666089413137.png


Here is the raw data that the macro is manipulating (i.e. repeat '10221463' 104 times and then move to '10224680' and repeat that 104 times etc...):

ItemRepeat
10221463104
10224680104
10224681104
10184291104
10239929104
10239930104
10242478104
10245568104
10249906104
10250138104
10267670104
10233093104
10233103104
10238629104
10239792104
10251121104
10268674104
10157523104
10178248104
10178250104
10178251104
10203907104
10204228104
10220178104
10220179104
10239720104
10245739104
10250055104
10250677104
10252103104
10252473104
10252994104
10253079104
10253644104
10255800104
10267339104
10267916104
10267917104
10268675104
10269374104
10136166104
10137507104
10145132104
10147420104
10147594104
10165152104
10205583104
10208423104
10215100104
10215802104
10220138104
10220825104
10231312104
10231335104
10231375104
10231523104
10232585104
10232587104
10232591104
10232601104
10234753104
10234810104
10234960104
10235157104
10235939104
10236343104
10236353104
10236355104
10237145104
10238155104
10238251104
10238277104
10238857104
10240621104
10240839104
10240855104
10244020104
10246318104
10249908104
10250018104
10250559104
10252362104
10203997104
10235031104
10246425104
10246916104
10248201104
10248357104
10249732104
10250838104
10254741104
 
Upvote 0
Your code sets your input range to B5:C120. However, your data ends at row 95. Therefore on row 96, XNum will be 0 and your Resize fails.

You need to use the actual amount of data instead of hardcoding a range. Here is a quick fix that ends the For loop when blank data is encountered.

Also you have undeclared variables. I strongly recommend to everyone that they use Option Explicit and declare variables. Doing so prevents a lot of bugs and runtime errors.
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
   
   '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

      If XRg.Range("A1") = "" Then Exit For
      
      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
 
Upvote 0
Hello,

Thank you for all your help - clearly, I'm quite the VBA beginner. Your suggestions helped!

However, I now have a new, different error. Hoping to pick your brains!

1667263276606.png


1667263354817.png


Data paste above is still what I'm using this script on. Apologies if I leftg out any needed information. Thank you for the help in advance!~
 

Attachments

  • 1667263259523.png
    1667263259523.png
    35.4 KB · Views: 3
Upvote 0
The line Dim xValue as Long, assumes that A1 contains a number. If this cell is always going to contain a text value change it to Dim xValue As String.
If it really is going to be a mixture then you might just have to use Dim xValue as Variant.
 
Upvote 0
I see no advantage to even using the variable xValue. It is only assigned once and only referenced once and not used except as a straight assignment. I would avoid using xValue altogether and make your assignment
VBA Code:
OutXRg.Resize(xNum, 1).Value = XRg.Range("A1").Value
Then data type doesn't matter.
 
Upvote 0
Thank you, where would I incorporate that into my current script?

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

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

Next

End Sub
 
Upvote 0
I recommend using code tags when you post code. That way your original spacing is preserved. Select the code then click the VBA button in the edit controls.

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

Forum statistics

Threads
1,214,593
Messages
6,120,434
Members
448,961
Latest member
nzskater

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