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
ssblueAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roy CoxGroup Finance ManagerCommented:
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
0
ssblueAuthor Commented:
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
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
I saw your code after I posted my reply and my guess seems to be right.
Can you upload a sample file?
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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.
0
ssblueAuthor Commented:
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
0
ssblueAuthor Commented:
Additional info.

Sheet REV2 the column to split is   AG

Sheet REV3 the column to split is   C
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Please try this...

Sub SplitRev2Data()
Dim ws As Worksheet
Dim lr As Long, i As Long
Dim str() As String
Application.ScreenUpdating = False
Set ws = Sheets("REV2")
lr = ws.Cells(Rows.Count, "AG").End(xlUp).Row
For i = lr To 2 Step -1
    If InStr(ws.Cells(i, "AG").Value, ",") > 0 Then
        str = Split(ws.Range("AG" & i).Value, ", ")
        ws.Range("A" & i & ":AV" & i).Copy
        ws.Range("A" & i + 1).Resize(UBound(str), 1).Insert shift:=xlDown
        ws.Range("AG" & i).Resize(UBound(str) + 1, 1).Value = Application.Transpose(str)
        Application.CutCopyMode = 0
    End If
Next i
Application.ScreenUpdating = True
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ssblueAuthor Commented:
Thanks everyone. :)
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.