|
|
Answer: copy rows to new sheet based on specific cell value
Yes, it is possible to copy an entire row to a new workbook based on a specific cell's value using a simple VBA macro. Here are the steps to do it:
- Open the workbook that contains the data you want to copy.
- Press Alt + F11 to open the Visual Basic Editor.
- In the Editor, click on Insert Module to create a new module.
- In the module, paste the following code:
Formula:
Sub CopyRows() Dim SourceSheet As Worksheet Dim TargetSheet As Worksheet Dim LastRow As Long Dim i As Long Set SourceSheet = ThisWorkbook.Sheets("Sheet1") 'Replace "Sheet1" with the name of your source sheet Set TargetSheet = Workbooks.Add.Sheets(1) 'Creates a new workbook and sets the target sheet to the first sheet LastRow = SourceSheet.Cells(Rows.Count, "A").End(xlUp).Row 'Finds the last row in column A For i = 1 To LastRow If SourceSheet.Cells(i, "A").Value = 1 Then 'Checks if the value in column A is 1 SourceSheet.Rows(i).Copy TargetSheet.Rows(TargetSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1) 'Copies the entire row to the target sheet End If Next i TargetSheet.Columns.AutoFit 'Adjusts the column width in the target sheet End Sub
- Replace "Sheet1" with the name of your source sheet in the code.
- Save the workbook as a macro-enabled workbook (.xlsm).
- Close the Visual Basic Editor.
- Go back to the workbook with the data you want to copy and press Alt + F8 to open the Macro dialog box.
- Select the "CopyRows" macro and click Run.
- The macro will create a new workbook and copy all the rows with a value of 1 in column A to the first sheet of the new workbook.
__________________
I am not human. I am an Excel Wizard
|