Good day, I am a new member and need help with a VBA code. The code works except I will get an error when it encounters a value in the specified column “T” that does not have a matching worksheet name.
I would like to modify the code so if no tab exists with a matching value found in the column “T” then it should create a new sheet with the name, copies header and then the row to the new sheet.
Below is the macro that I am currently trying to modify, any help would be appreciated. I have also added a sample excel file.
Sub CopyDataToSheets()
Dim copyfromws As Worksheet
Dim copytows As Worksheet
Dim cfrng As Range
Dim ctrng As Range
Dim cflr As Long
Dim ctlr As Long
Dim i As Long
Dim currval As String
Set copyfromws = Sheets("Report")
cflr = copyfromws.Cells(Rows.Count, "A").End(xlUp).Row
' Copy Row of Data to Specific Worksheet based on value in Column T
' Existing Formulas in Columns F through H or J are automatically extended to the new row of data
For i = 2 To cflr
currval = copyfromws.Cells(i, 20).Value
Set copytows = Sheets(currval)
ctlr = copytows.Cells(Rows.Count, "A").End(xlUp).Row + 1
Set cfrng = copyfromws.Range("A" & i & ":Y" & i)
Set ctrng = copytows.Range("A" & ctlr & ":Y" & ctlr)
ctrng.Value = cfrng.Value
Next
End Sub
I would like to modify the code so if no tab exists with a matching value found in the column “T” then it should create a new sheet with the name, copies header and then the row to the new sheet.
Below is the macro that I am currently trying to modify, any help would be appreciated. I have also added a sample excel file.
Sub CopyDataToSheets()
Dim copyfromws As Worksheet
Dim copytows As Worksheet
Dim cfrng As Range
Dim ctrng As Range
Dim cflr As Long
Dim ctlr As Long
Dim i As Long
Dim currval As String
Set copyfromws = Sheets("Report")
cflr = copyfromws.Cells(Rows.Count, "A").End(xlUp).Row
' Copy Row of Data to Specific Worksheet based on value in Column T
' Existing Formulas in Columns F through H or J are automatically extended to the new row of data
For i = 2 To cflr
currval = copyfromws.Cells(i, 20).Value
Set copytows = Sheets(currval)
ctlr = copytows.Cells(Rows.Count, "A").End(xlUp).Row + 1
Set cfrng = copyfromws.Range("A" & i & ":Y" & i)
Set ctrng = copytows.Range("A" & ctlr & ":Y" & ctlr)
ctrng.Value = cfrng.Value
Next
End Sub