How to specifically pick entire few different columns from a huge set of data

asddsaasddas

Board Regular
Joined
Mar 23, 2020
Messages
60
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
I have a huge set of raw data from column A to AM with a non fixed number of rows. I want to be able to pick which column to use by their specific name and every row under the column and delete the rest of the columns. For example, in the range from column A to AM, I want it to search for "Employee ID", "Name" and "Salary" and take all the rows under these 3 columns and delete the rest of the columns. Thus i will only be left with 3 different columns and their rows. In addition, I want to be able to BOLD the header of each column, in this case "Employee ID", "Name" and "Salary" would be boded. Any help will be appreciated.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
To make it clearer, this is a better explanation

I have a huge chunk of raw data ranging from column A to AM. The number of rows are not fixed and are based on the data retrieved from the system. Within columns A to AM, the first row will always be the header. With this being said, I want to be able to choose which columns and their rows below them based on the header's value and delete the rest of the columns. For example, in column A to AM there are 3 columns that i want to record. I want to be able to record the 3 columns based on the header's name and the rows below these 3 columns. Lets say the first row (header) in columns B, F and G are named "Employee ID ", "Name" and "Salary" respectively. I want to be able to record these 3 columns and their rows and delete the all the other columns. All in all, I want to be able to choose which columns to be recorded based on the values of the first row (header) and delete all the other rows. Thus, in my example, I would be left with column A, B and C populated with "Employee ID", "Name" and "Salary" as the values for the first row. After sorting the columns based on their names, I want to be able to bold the header (first row) for these 3 columns left. Any help will be appreciated.
 
Upvote 0
Here is another way to look at it

I have 10 columns of data ranging from A to J. The number of rows in each columns are not fixed and can change from time to time. The first row however will always be consistent. Thus, I want to be able to choose column B, E, J. But I want to do so in a way that i choose these columns based on the first row's value. Since the first row will always be the same, the values are always fixed. I currently have a macro that chooses the specific columns that i want, However i want to upgrade this macro such that it will choose the columns with specific values in row 1 rather than just the columns itself. Here is an example, Since i want to take column B, E and J and delete the other columns. Is there a way to do so such that I take the columns based on the values in the first row. For instance, column B first row's value is "BEE", Column E first row's value is "ELEPHANT" and column J first row's value is "JAGUAR". Is there a way to search for the specific "JAGUAR", "BEE" and "ELEPHANT" in the first row and take the whole columns and the rows below it and delete the rest? Any help will be appreciated.
 
Upvote 0
Here is the first half

1587901884254.png


VBA Code:
Private Sub CommandButton1_Click()
Dim currentColumn As Integer
Dim columnHeading As String

For currentColumn = ActiveSheet.UsedRange.Columns.Count To 1 Step -1
    columnHeading = ActiveSheet.UsedRange.Cells(2, currentColumn).Value 'Looks for the value in Row 2

    'Search this criteria in row2 and keep those columns
    Select Case columnHeading
        Case "Hat", "Bat", "Cat"
            'Do nothing
        Case Else
            'Delete if the cell doesn't contain "string-"
             If InStr(columnHeading, "string1-") = 0 And _
                      InStr(columnHeading, "string2-") = 0 And _
                           InStr(columnHeading, "string3-") = 0 Then
            
           ActiveSheet.Columns(currentColumn).Delete 
            End IF
    End Select
Next
End Sub

After
1587902003981.png


Found on the web, made minor changes to it.
 
Upvote 0
Thank you very much for your help. Not exactly what i wanted but it will be of great help. Instead of the "Hat", "Bat" and "Cat" being in row 2, it should be the name of the header. Is there any way to change that? Once again thank you very much for your help :).
 
Upvote 0
How about
VBA Code:
Sub asddsaasddas()
   Dim Cl As Range, Rng As Range
   
   For Each Cl In Range("A1", Cells(1, Columns.Count).End(xlToLeft))
      Select Case LCase(Cl.Value)
         Case "employee id", "name", "salary"
         Case Else
            If Rng Is Nothing Then Set Rng = Cl Else Set Rng = Union(Rng, Cl)
      End Select
   Next Cl
   If Not Rng Is Nothing Then Rng.EntireColumn.Delete
End Sub
 
Upvote 0
Look at the bits of the code in green, they are the comments only. The comments do not run when the code does, so change the code

VBA Code:
For currentColumn = ActiveSheet.UsedRange.Columns.Count To 1 Step -1
    columnHeading = ActiveSheet.UsedRange.Cells(2, currentColumn).Value       'Looks for the value in Row 2

    'Search this criteria in row2 and keep those columns
    Select Case columnHeading
        Case "Hat", "Bat", "Cat"

Change the 2 in the Cells(2, currentColumn).Value to what row you want.
Change "Hat", "Bat", "Cat" to what you want in the code I gave you. It might be case senstive, I did not check that bit.

hope that helps
 
Upvote 0
Super, go with Fluff he knows what he is doing. Mine is a stab in the dark most of the time
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,419
Messages
6,124,796
Members
449,189
Latest member
kristinh

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