I have a 3 sheet Excel spreadsheet that I want to paste data onto the top sheet each week and based on student IDs in column A and the criteria ("Yes" or "No") in column M, have a range of data (M-AQ) returned to the other 2 sheets. Sheet Met ("Yes") and Sheet Unmet (No"). This is the VBA code so far, and it returns an error of "Block if without End if". It worked at first but did not return the range I wanted. Any help is appreciated!
Sub Copy_BasedOnValue()
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
Dim lr1 As Long, lr2 As Long, lr3 As Long
Set sh1 = Sheets("Data")
Set sh2 = Sheets("Yes")
Set sh3 = Sheets("No")
lr1 = sh1.Range("M" & Rows.Count).End(3).Row
lr2 = sh2.UsedRange.Rows.Count
lr3 = sh3.UsedRange.Rows.Count
If Application.WorksheetFunction.CountA(sh2.UsedRange) = 0 Then lr2 = 0
If Application.WorksheetFunction.CountA(sh3.UsedRange) = 0 Then lr3 = 0
If WorksheetFunction.CountIf(sh1.Range("M3:M" & lr1), "Yes") > 0 Then
sh1.Range("A2:M" & lr1).AutoFilter 1, "No"
sh1.AutoFilter.Range.Range("A2:AE" & lr1).Copy sh2.Range("A" & lr2 + 1)
sh1.ShowAllData
If WorksheetFunction.CountIf(sh1.Range("M3:M" & lr1), "No") > 0 Then
sh1.Range("A2:M" & lr1).AutoFilter 1, "No"
sh1.AutoFilter.Range.Range("A2:AE" & lr1).Copy sh3.Range("A" & lr3 + 1)
sh1.ShowAllData
End If
End Sub
Sub Copy_BasedOnValue()
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
Dim lr1 As Long, lr2 As Long, lr3 As Long
Set sh1 = Sheets("Data")
Set sh2 = Sheets("Yes")
Set sh3 = Sheets("No")
lr1 = sh1.Range("M" & Rows.Count).End(3).Row
lr2 = sh2.UsedRange.Rows.Count
lr3 = sh3.UsedRange.Rows.Count
If Application.WorksheetFunction.CountA(sh2.UsedRange) = 0 Then lr2 = 0
If Application.WorksheetFunction.CountA(sh3.UsedRange) = 0 Then lr3 = 0
If WorksheetFunction.CountIf(sh1.Range("M3:M" & lr1), "Yes") > 0 Then
sh1.Range("A2:M" & lr1).AutoFilter 1, "No"
sh1.AutoFilter.Range.Range("A2:AE" & lr1).Copy sh2.Range("A" & lr2 + 1)
sh1.ShowAllData
If WorksheetFunction.CountIf(sh1.Range("M3:M" & lr1), "No") > 0 Then
sh1.Range("A2:M" & lr1).AutoFilter 1, "No"
sh1.AutoFilter.Range.Range("A2:AE" & lr1).Copy sh3.Range("A" & lr3 + 1)
sh1.ShowAllData
End If
End Sub