I am new to macros and VBA, so this might be easy.
I need to filter a list of companies, such that companies not matching branchcode are removed from the excel-tab.
More precisely:
- company.xls contains the full list of companies, where column N, is containing branchcodes
- branch.xls, column A contains the branchcodes of relevance, which has to be used to filter the comapnies in company.xls
- The companies in company.xls that do not have a matching
branchcode should be removed from tab1 to tab2 of company.xls.
I hope it make sense!?
Thanks in advance for your responses.
Function Search_String(x As String) As Boolean
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim Contained As Boolean
Contained = True
'We use the ActiveSheet but you can replace this with
'Sheets("MySheet")if you want
With Sheets("codes")
'We select the sheet so we can change the window view
.Select
'Set the first and last row to loop through
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
'We loop from Lastrow to Firstrow (bottom to top)
For Lrow = Lastrow To Firstrow Step -1
'We check the values in the A column in this example
With .Cells(Lrow, "A") 'Column letter for codes sheet
If Not IsError(.Value) Then
If InStr(x, .Value) Then Contained = False
End If
End With
Next Lrow
End With
Search_String = Contained
End Function
Sub Filtrer()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
With Sheets("search (14)") 'Sheet name with rows to be deleted
'We select the sheet so we can change the window view
.Select
'If you are in Page Break Preview Or Page Layout view go
'back to normal view, we do this for speed
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
'Turn off Page Breaks, we do this for speed
.DisplayPageBreaks = False
'Set the first and last row to loop through
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
'We loop from Lastrow to Firstrow (bottom to top)
For Lrow = Lastrow To Firstrow Step -1
With .Cells(Lrow, "N") 'Change this to the correct Sheets column that needs deleting
If Not IsError(.Value) Then
If Search_String(.Value) Then .EntireRow.Delete
End If
End With
Next Lrow
End With
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub