Exclude certain banches in Col Y when extracting data

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
Office Version
  1. 2021
Platform
  1. Windows
I have the following macro below


I want have certain branches (Br1, Br2 & Br3) in Col Y which I want to exclude when extracting the data

when the code below was set as

Code:
 If cel3 <> "BR1" Or "BR2" Or "BR3" Then Branches.Add CStr(cel3), CStr(cel3) [

, the code copies the data except BR1


However when trying to add BR2 & BR3 to be excluded, it does not exclude these

It would be appreciated if someone could kindly amend me code to exclude the branches above that is in Col Y



Code:
 Sub Extract_prepayments()
'Clear_Old_Prepayments
'variables and ranges
  Dim ws3 As Worksheet, ws4 As Worksheet, rng3 As Range, cel3 As Range, rng4 As Range, b As Range
    Dim Branches As New Collection, branch As Variant, HowMany As Integer, H As Integer
    Set ws3 = Sheets(3)
    Set ws4 = Sheets(4)
    Set rng3 = ws3.Range("Y2", ws3.Range("Y" & Rows.Count).End(xlUp))
    Set rng4 = ws3.Cells(Rows.Count, "Y")
    HowMany = Application.InputBox("Select number of Prepayments to extract per branch", , 2, , , , , 1)
'create unique list of branches
    On Error Resume Next
        For Each cel3 In rng3
            If cel3 <> "BR1" Or "BR2" Or "BR3" Then Branches.Add CStr(cel3), CStr(cel3)
        Next
    On Error GoTo 0
'find each value and add to range to be copied
    For Each branch In Branches
        Set b = rng3.Find(branch, LookIn:=xlValues)
        Set rng4 = Union(rng4, b)
        For H = 1 To HowMany - 1
            If HowMany = 1 Then Exit For
            If WorksheetFunction.CountIf(rng3, branch) >= H Then
                Set b = rng3.FindNext(b)
                Set rng4 = Union(rng4, b)
            End If
        Next H
        Set b = Nothing
    Next
'copy rows
    rng4.EntireRow.Copy ws4.Range("A2")
    Sheets(4).Select
    finalrow = Range("A65536").End(xlUp).Row + 2
Range("P" & finalrow + 3).Value = "Total Value"
Range("Q" & finalrow + 3).Formula = "=sum(Q2:Q" & finalrow & ")"
 Range("N2:Q" & finalrow + 3).NumberFormat = "#,##0.00"
    Application.CutCopyMode = False
End Sub
 
Last edited:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hsve managed to sort this out by amending my code as follows


Code:
 On Error Resume Next
        For Each cel3 In rng3
            If cel3 <> "BR1" and cel3 <> "BR2" And cel3 <> "BR3" Then Branches.Add CStr(cel3), CStr(cel3)
        Next
    On Error GoTo 0


Is there a better way of doing this possibly using an array ?
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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