Forum Discussion
Identify characters and split into different sheets
- Jun 14, 2023
Success! I was able to use ChatGPT and it produced the following code. I had to ask it separately for the match code, then paste it into the original code, but it works perfectly now. Thanks:
Thanks for the code. So far it partly works, but it pulls all the cells in the source range to a new sheet instead of splitting them into different sheets by firstThreeChars. For example:
ABC_123456
ABC_123457
ABC_123458
DEF18818
DEF18819
DEF18820
GHI207
GHI208
GHI209
GHI210
GHI211
Here's what I have entered for the code:
Sub SplitCellsToSheets()
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourceRange As Range
Dim cell As Range
Dim firstThreeChars As String
Dim lastRow As Long
' Set the source sheet
Set sourceSheet = ThisWorkbook.Sheets("ABC") ' Replace "Sheet1" with your source sheet name
'Set the source range
lastRow = 19726
Set sourceRange = sourceSheet.Range("F2:F" & lastRow) ' Assuming the data is in column A
' Loop through each cell in the source range
For Each cell In sourceRange
' Get the first three characters of the cell value
firstThreeChars = Left(cell.Value, 3)
' Check if a sheet with the first three characters exists, create one if it doesn't
On Error Resume Next
Set targetSheet = ThisWorkbook.Sheets(firstThreeChars)
On Error GoTo 0
If targetSheet Is Nothing Then
Set targetSheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
targetSheet.Name = firstThreeChars
End If
' Copy the cell value to the target sheet
targetSheet.Cells(targetSheet.Cells(targetSheet.Rows.Count, "F").End(xlUp).Row + 1, "F").Value = cell.Value
Next cell
End Sub
It seems that the code provided is not correctly splitting the cells into different sheets based on the first three characters.
The updated code below should split the cells into different sheets based on the first three characters:
Sub SplitCellsToSheets()
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourceRange As Range
Dim cell As Range
Dim firstThreeChars As String
Dim lastRow As Long
' Set the source sheet
Set sourceSheet = ThisWorkbook.Sheets("ABC") ' Replace "Sheet1" with your source sheet name
' Set the source range
lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "F").End(xlUp).Row
Set sourceRange = sourceSheet.Range("F2:F" & lastRow) ' Assuming the data is in column F
' Loop through each cell in the source range
For Each cell In sourceRange
' Get the first three characters of the cell value
firstThreeChars = Left(cell.Value, 3)
' Check if a sheet with the first three characters exists, create one if it doesn't
On Error Resume Next
Set targetSheet = ThisWorkbook.Sheets(firstThreeChars)
On Error GoTo 0
If targetSheet Is Nothing Then
Set targetSheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
targetSheet.Name = firstThreeChars
End If
' Copy the cell value to the target sheet
targetSheet.Cells(targetSheet.Cells(targetSheet.Rows.Count, "F").End(xlUp).Row + 1, "F").Value = cell.Value
Next cell
End Sub
Make sure to adjust the following parts of the code to match your specific workbook:
- Set sourceSheet = ThisWorkbook.Sheets("ABC"): Replace "ABC" with the name of your source sheet.
- Set sourceRange = sourceSheet.Range("F2:F" & lastRow): Adjust the range to match the column and row range of your source data.
After updating the code, run the SplitCellsToSheets macro, and it should split the cells into different sheets based on the first three characters. Each sheet will be named after the corresponding three characters, and the cells will be copied to their respective sheets.
- lnjohnsonJun 12, 2023Copper Contributor
Maybe I'm missing something. I don't see any differences to the updated code that would split the data into different sheets. Everytime i run the macro it creates a new sheet with the entire range. Have you tried to run it with the example data i submitted, and if so, can you send me a screenshot?
- NikolinoDEJun 13, 2023Gold Contributor
is this in the right direction, does it fit? ...see file.
If not, please explain exactly (step by step, cell by cell, sheet by sheet) exactly what is to be done. If possible, include a file (without sensitive data) or photos. Additional information such as Excel version, operating system, storage medium would be an advantage. Thank you for your understanding.
- lnjohnsonJun 14, 2023Copper Contributor
Success! I was able to use ChatGPT and it produced the following code. I had to ask it separately for the match code, then paste it into the original code, but it works perfectly now. Thanks: