ssblue
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
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
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
Sub SplitData()
Dim ws As Worksheet
Dim lr As Long, i As Long
Dim str() As String
Application.ScreenUpdating
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
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?
Can you upload a sample file?
Replace the line
If InStr(ws.Cells(i, "C").Value, ",") > 0 Then
WITHIf InStr(ws.Cells(i, "C").Value, ", ") > 0 Then
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.
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
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
ASKER
Additional info.
Sheet REV2 the column to split is AG
Sheet REV3 the column to split is C
Sheet REV2 the column to split is AG
Sheet REV3 the column to split is C
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks everyone. :)
You're welcome!
Declare the variable as an Integer or Long