Forum Discussion
Move cells to a different sheet when a specific text is found
it's possible to automate the movement of data when specific texts are found in one cell in Excel. You can achieve this with VBA (Visual Basic for Applications). Here's a step-by-step guide to create a VBA macro that moves data to another worksheet based on specific text:
Important: Before using VBA, make sure you save your Excel workbook as a macro-enabled file with the extension ".xlsm."
- Press Alt + F11 to open the Visual Basic for Applications editor.
- In the editor, click Insert > Module to insert a new module.
- Copy and paste the following VBA code into the module:
vba code not tested:
Sub MoveDataBasedOnText()
Dim wsSource As Worksheet
Dim wsDestination As Worksheet
Dim cell As Range
Dim searchText As String
Dim lastRow As Long
' Set your source and destination worksheets
Set wsSource = ThisWorkbook.Sheets("SourceSheet")
Set wsDestination = ThisWorkbook.Sheets("DestinationSheet")
' Set the text you want to search for
searchText = "SpecificText"
' Find the last row in the source sheet
lastRow = wsSource.Cells(Rows.Count, 1).End(xlUp).Row
' Loop through cells in a specific column (e.g., column A)
For Each cell In wsSource.Range("A1:A" & lastRow)
If InStr(1, cell.Value, searchText, vbTextCompare) > 0 Then
' If the specific text is found in the cell, move the entire row
cell.EntireRow.Copy wsDestination.Cells(wsDestination.Cells(Rows.Count, 1).End(xlUp).Row + 1, 1)
cell.EntireRow.Delete
End If
Next cell
End Sub- Modify the code as needed. Replace "SourceSheet" and "DestinationSheet" with your source and destination sheet names, and "SpecificText" with the text you want to search for.
- Close the VBA editor.
- To run the macro, press Alt + F8, select MoveDataBasedOnText, and click "Run."
The macro will search for the specific text in the source sheet (e.g., "SourceSheet") and move the entire row to the destination sheet (e.g., "DestinationSheet") when the text is found. You can run the macro whenever you need to perform this task. The text, steps and Code was created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and like it!
This will help all forum participants.