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:
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.
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:
- NikolinoDEJun 15, 2023Gold ContributorI'm glad you found a solution that helps you.
I wish you continued success with Excel!
- lnjohnsonJun 13, 2023Copper ContributorThe formula should work without me telling it look for a specific string of data; something like the first three characters should work fine. I have over 19,000 lines with over 500 different variables. I can't spend time with a formula that i have manually tell it to look for a specific string of data. I hope that makes sense.
- lnjohnsonJun 13, 2023Copper Contributor
Thank you for responding. The idea is that the cell information starts the way you have listed in your document. Then the formula should divide the cell information into different sheets based on the first three characters of each cell. See attachment.