Jun 07 2023 08:32 AM
How do i create a formula that scans a column, auto-identify similar characters(perhaps by first 3 characters) in a cell and then splits(extract) the cells with similar characters into different sheets?
Jun 07 2023 11:04 PM
You can use VBA code in Excel.
Here is an example of how you can achieve this:
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("Sheet1") ' Replace "Sheet1" with your source sheet name
' Set the source range
lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row
Set sourceRange = sourceSheet.Range("A1:A" & 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, "A").End(xlUp).Row + 1, "A").Value = cell.Value
Next cell
End Sub
4. Update the code with the appropriate sheet names and column references as necessary.
5. Close the Visual Basic Editor.
6. Press "Alt + F8" to open the Macro dialog box.
7. Select the "SplitCellsToSheets" macro and click "Run".
This macro will scan the specified column (column A in this example) in the source sheet, identify cells with similar first three characters, and split them into different sheets based on those characters. Each sheet will be named after the first three characters, and the corresponding cells will be copied to their respective sheets.
Make sure to adjust the code to fit your specific needs, such as the column to scan and the sheet names. Also, remember to save your workbook as a macro-enabled (.xlsm) file format.
If you prefer to use formulas instead of VBA, you can achieve a similar result using formulas and filters. Here is an example:
Assuming you have your data in column A starting from cell A1, and you want to split the cells with similar first three characters into different sheets:
=LEFT(A1,3)
By applying filters and copying the visible cells for each value, you can split the cells with similar first three characters into different sheets manually using formulas and filtering in Excel.
Jun 09 2023 10:53 AM
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
Jun 09 2023 03:10 PM
As a functional programming environment, Excel formulas cannot 'put' extracts anywhere except the cell in which the formula is written. VBA, being an imperative programming language, can change the state of other cells.
So, a formula solution must have a formula that reads the 'sourceRange' on every sheet you wish to write to.
= LET(
tail, TEXTAFTER(sourceRange, string),
FILTER(tail, ISTEXT(tail))
)
[The image shows the ranges on the same sheet for convenience]
Jun 09 2023 11:17 PM
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:
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.
Jun 12 2023 09:08 AM
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?
Jun 12 2023 09:10 AM
Jun 13 2023 12:27 AM
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.
Jun 13 2023 06:35 AM
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.
Jun 13 2023 06:39 AM
Jun 14 2023 10:17 AM
SolutionSuccess! 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:
Jun 15 2023 12:43 AM
Jun 14 2023 10:17 AM
SolutionSuccess! 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: