Copy Rows to existing Multiple worksheet based on condition and create new worksheet if one does not exist

RPE

New Member
Joined
Apr 26, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Good day, I am a new member and have a similar situation but would like to for the code to account for when it finds a value and there is no worksheet. So if no tab exists with the value found in the column 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 was provided above. which works until it hits a value that does not have a matching tab.

Code:

Option Explicit

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("Data")

cflr = copyfromws.Cells(Rows.Count, "B").End(xlUp).Row

' Copy Row of Data to Specific Worksheet based on value in Column E

' 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, 5).Value

Set copytows = Sheets(currval)

ctlr = copytows.Cells(Rows.Count, "B").End(xlUp).Row + 1

Set cfrng = copyfromws.Range("A" & i & ":E" & i)

Set ctrng = copytows.Range("A" & ctlr & ":E" & ctlr)

ctrng.Value = cfrng.Value

Next



End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Apologies, below is the code i am using now. I would also like to modify so it will adjust if a column is added to the Sheets("Report")


CODE:

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
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top