Link to home
Start Free TrialLog in
Avatar of ssblue
ssblueFlag for United States of America

asked on

Run-time error '1004'

Why am I getting an error on this line?

I get a Run-time error '10040:
Application-defined or object-defined error

When I run the debugger it highlights this line

  ws.Range("A" & i + 1).Resize(UBound(str), 1).Insert shift:=xlDown
Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

What is str, if you've declared it as a String then it probably causes the error

Declare the variable as an Integer or Long
Avatar of ssblue

ASKER

I received help with this code for use on another file and it worked as intended however I am trying to use it on another sheet but when I do I get the error.

Sub SplitData()
Dim ws As Worksheet
Dim lr As Long, i As Long
Dim str() As String
Application.ScreenUpdating = False
Set ws = Sheets("Sheet2")
lr = ws.Cells(Rows.Count, 1).End(xlUp).Row
For i = lr To 2 Step -1
    If InStr(ws.Cells(i, "C").Value, ",") > 0 Then
        str = Split(ws.Range("C" & i).Value, ", ")
        ws.Range("A" & i & ":C" & i).Copy
        ws.Range("A" & i + 1).Resize(UBound(str), 1).Insert shift:=xlDown
        ws.Range("C" & i).Resize(UBound(str) + 1, 1).Value = Application.Transpose(str)
        Application.CutCopyMode = 0
    End If
Next i
Application.ScreenUpdating = True
End Sub
As per your code str should be an array and it seems either str is not populated or it has only one element in it so ubound would be 0 and resize will throw an error when 0 is passed as row size.
I saw your code after I posted my reply and my guess seems to be right.
Can you upload a sample file?
Replace the line
If InStr(ws.Cells(i, "C").Value, ",") > 0 Then

Open in new window

WITH
If InStr(ws.Cells(i, "C").Value, ", ") > 0 Then

Open in new window

That's because the inStr function is checking for a comma in the column C cell while Split is being done using the delimiter ", " i.e. comma followed by a space. And if the string in column C cell has a comma in it but not a space after the comma, as I said, UBound(str) would be 0 and will throw an error.
Avatar of ssblue

ASKER

I modified the files to send you samples.

The attachment has 2 sheets. I tried testing the code on Sheet REV3 but I really need it to work on sheet REV2 which has over 5000 lines.
SplitColumnsIntoRowsTEST.xlsx
Avatar of ssblue

ASKER

Additional info.

Sheet REV2 the column to split is   AG

Sheet REV3 the column to split is   C
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ssblue

ASKER

Thanks everyone. :)
You're welcome!