Select Different Sheets Based on Criteria VBA

austinandreikurt

Board Regular
Joined
Aug 25, 2020
Messages
91
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I have a list of specific Sheet names in Column B of Sheets("MAIN") which starts in B3 and is dynamic to the last non-blank row and the "YES" and "NO" in Column A. So basically, what I need is to find the vba script to select all the sheet names with "YES" as values in Column A. Then move those selected sheets to another workbook and save it will filename "Summary_" & Date & ".xlsx" then select the previous workbook again. I can do the other codes, I just really need a simple code for selecting the sheetnames with "YES". Thanks!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi there,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim strMySheets() As String
    Dim lngArrayCount As Long
    Dim lngLastRow As Long
    Dim wbThisWB As Workbook
    Dim wbNewWB As Workbook
    Dim rngMyCell As Range
   
    Application.ScreenUpdating = False
   
    Set wbThisWB = ThisWorkbook
   
    With wbThisWB.Sheets("MAIN")
        lngLastRow = .Range("A:B").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        For Each rngMyCell In .Range("A3:A" & lngLastRow)
            If StrConv(rngMyCell.Value, vbUpperCase) = "YES" And Len(rngMyCell.Offset(0, 1).Value) > 0 Then
                lngArrayCount = lngArrayCount + 1
                ReDim Preserve strMySheets(1 To lngArrayCount)
                strMySheets(lngArrayCount) = CStr(rngMyCell.Offset(0, 1))
            End If
        Next rngMyCell
    End With
   
    wbThisWB.Sheets(strMySheets).Copy
    Set wbNewWB = ActiveWorkbook
    wbNewWB.SaveAs "D:\Robert\Summary_" & Format(Now(), "YYYYMMDD") & ".xlsx", FileFormat:=51 'Change the directory where the file is to be saved to suit.
   
    wbThisWB.Activate
   
    Application.ScreenUpdating = True
   
End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,454
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top