Run-time error '1004'

ssblue
ssblue used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Roy CoxGroup Finance Manager

Commented:
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
ssblueCoordinator

Author

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
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
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.
Ensure you’re charging the right price for your IT

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

Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
I saw your code after I posted my reply and my guess seems to be right.
Can you upload a sample file?
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
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.
ssblueCoordinator

Author

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
ssblueCoordinator

Author

Commented:
Additional info.

Sheet REV2 the column to split is   AG

Sheet REV3 the column to split is   C
Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
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

ssblueCoordinator

Author

Commented:
Thanks everyone. :)
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial