User Contributed Macros > Copy Sheet Data

Copy Sheet Data

Tags:  

When you want to copy any sheet data to the newly created sheet with some condition then Copy the following code and paste into "ThisWorkbook" in the VBA Macro Editor. Change the condition text and Column number with respective to your data.
Private Sub Workbook_NewSheet(ByVal dest_sheet As Object)
Dim source_sheet as String
Dim condition_text as String  
Dim condition_column as Integer
Dim row_index as Integer
Dim end_column as Integer
Dim cell_value as String
Dim end_row as Integer

source_sheet  = "Sheet1"             ' Change the source sheet name if needed.
condition_text = "Condition Text"  ' Change this to your data
condition_column = 3                 ' Change the Column number
row_index = 1
new_row_index = 1
end_row = Worksheets(source_sheet).Range("A1").End(xlDown).Row

For row_index = 1 to end_row
 cell_value = Worksheets(source_sheet).Cells(row_index,condition_column).value
 If cell_value = condition_text Then
    end_column = Worksheets("Sheet1").Cells(row_index,condition_column).End(xlToRight).Column
    For x = 1 to end_column
         dest_sheet .Cells(new_row_index,x) =  Worksheets(source_sheet).Cells(row_index,x)
    Next x
    new_row_index = new_row_index+1
 End If
Next row_index
End Sub


 RSS of this page