Copy filtered Columns Based on Header matching a variable to another sheet in VBA

Dark tyranus

New Member
Joined
Feb 19, 2020
Messages
1
Office Version
  1. 2016
Platform
  1. MacOS
Hi guys,

I need help with my VBA, there is my problem :

We have a rather large file that exports lot of columns of data. We want to use VBA to copy some of the columns to another sheet with the same name than the column. The columns are scattered through out the entire workbook. I have the code created to copy the entire column to the new sheet. Problem is :

- I would like to rename the new sheet created with an automatic name by taking the name manually (filled inside the VBA).
- I need to filter the column "Status Code" with only results in "200" and copy after the different column (e.g : filter by Status Code in 200 -> copy URL column and Title column -> create new sheet with these informations).

Some of the columns we are trying to copy are : Title, Status Code, Indexability, etc

The code i'm using for now is :

VBA Code:
Sub weasel()
'The sheet with all the imported data columns must be active when this macro is run
Dim newSht As Worksheet, sSht As Worksheet, Hdrs As Variant, i As Long, Fnd As Range
Set sSht = ActiveSheet
'Expand the array below to include all relevant column headers
Hdrs = Array("Address", "Status Code")
Application.ScreenUpdating = False
Set newSht = Worksheets.Add(after:=sSht)
With sSht.UsedRange.Rows(1)
    For i = LBound(Hdrs) To UBound(Hdrs)
        Set Fnd = .Find(Hdrs(i), lookat:=xlWhole)
        If Not Fnd Is Nothing Then
            Intersect(Fnd.EntireColumn, sSht.UsedRange).Copy Destination:=newSht.Cells(1, i + 1)
        End If
    Next i
    Application.CutCopyMode = False
End With
Hdrs = Array("Address", "Indexability")
Application.ScreenUpdating = False
Set newSht = Worksheets.Add(after:=sSht)
With sSht.UsedRange.Rows(1)
    For i = LBound(Hdrs) To UBound(Hdrs)
        Set Fnd = .Find(Hdrs(i), lookat:=xlWhole)
        If Not Fnd Is Nothing Then
            Intersect(Fnd.EntireColumn, sSht.UsedRange).Copy Destination:=newSht.Cells(1, i + 1)
        End If
    Next i
    Application.CutCopyMode = False
End With
Application.ScreenUpdating = True
End Sub

Any suggestions?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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