billandrew
Well-known Member
- Joined
- Mar 9, 2014
- Messages
- 743
Hello All
Attempting to expand my VBA knowledge. I am trying to copy a specific value (This case Maine & Virginia) from Sheet 1 to the newly added Maine & Virginia worksheets. I am using the below code which was provided in part by this Forum.
I would also like to copy the header row & in the future add additional state named sheets and the data from Sheet 1 to those sheets.
Sub copyif()
Dim lr As Long, ws As Worksheets, lr2 As Long
Dim wsa As Worksheet
Application.ScreenUpdating = False
Set wsa = ActiveSheet
ThisWorkbook.Worksheets.Add(after:=Sheets(Sheets.Count)).Name = "Maine"
ThisWorkbook.Worksheets.Add(after:=Sheets(Sheets.Count)).Name = "Virginia"
wsa.Activate
lr = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lr2 = 1
For i = 2 To lr
If Cells(i, 3).Value = "Maine" Then
Worksheets("Maine").Rows(lr2).Value = Worksheets("Sheet1").Rows(i).Value
ElseIf Cells(i, 3).Value = "Virginia" Then
Worksheets("Virginia").Rows(lr2).Value = Worksheets("Sheet1").Rows(i).Value
lr2 = lr2 + 1
End If
Next i
Application.ScreenUpdating = True
End Sub
Attempting to expand my VBA knowledge. I am trying to copy a specific value (This case Maine & Virginia) from Sheet 1 to the newly added Maine & Virginia worksheets. I am using the below code which was provided in part by this Forum.
I would also like to copy the header row & in the future add additional state named sheets and the data from Sheet 1 to those sheets.
Sub copyif()
Dim lr As Long, ws As Worksheets, lr2 As Long
Dim wsa As Worksheet
Application.ScreenUpdating = False
Set wsa = ActiveSheet
ThisWorkbook.Worksheets.Add(after:=Sheets(Sheets.Count)).Name = "Maine"
ThisWorkbook.Worksheets.Add(after:=Sheets(Sheets.Count)).Name = "Virginia"
wsa.Activate
lr = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lr2 = 1
For i = 2 To lr
If Cells(i, 3).Value = "Maine" Then
Worksheets("Maine").Rows(lr2).Value = Worksheets("Sheet1").Rows(i).Value
ElseIf Cells(i, 3).Value = "Virginia" Then
Worksheets("Virginia").Rows(lr2).Value = Worksheets("Sheet1").Rows(i).Value
lr2 = lr2 + 1
End If
Next i
Application.ScreenUpdating = True
End Sub