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
, 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
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: