• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Passing column number to VBA code

DE_Tx

Member
I've tried searching for a solution using various tags but haven't quite found something that puts it all together. If there is something that addresses this, please point me in the right direction.

Scenario - a system generated file received daily, columns are not always in the same order. Need to extract specific columns, data filtered.

Proposed solution
Identify column numbers of required columns based on column header
Identify LastColumn
Hide all active columns (header data)
Unhide required columns
Copy/Paste the required, filtered data to a summary file (this part I have figured out).

Problem
How to pass the column number of the required columns to the code?
I know how to do a Find in the header row for specific header labels.
Once I have found the column (header label), how do I pass that column number to the code so that when I unhide the required columns, I can unhide columns 1, 3, 4, 97, 156, 257 (just as an example).

Any assistance would be greatly appreciated.
 
Not sure I understand what specific information you need. Are you looking for a line of code to unhide the target column?

Sheets(1).Columns(insert_column_number_here).Hidden = False
 
Last edited:
Just an example
Code:
Sub test()
    Columns.Hidden = True
    UnHideColumns Array(1, 3, 4, 97, 156, 257)
End Sub

Private Sub UnHideColumns(arry)
    Dim e
    For Each e In arry
        Columns(e).Hidden = False
    Next
End Sub
 
No, I know how to unhide the columns. How do I dimension the identified columns so that I can unhide them.
For example, find "OrderNumber" in row 1 (header row), identify the column number and dimension it so I can use it later when I unhide the needed columns.
 
Like this? But most of the time you don't need to store column number for such operation.

Code:
Rows(1).Find("OrderNumber").Column
 
Code:
Sub test()
    Const HeaderRow As Long = 1
    Dim myHeaders
    Columns.Hidden = True
    myHeaders = Application.Match(Array("OrderNumber", "xyz", "blablah"), Rows(HeaderRow), 0)
    UnHideColumns myHeaders
End Sub

Private Sub UnHideColumns(arry)
    Dim e
    For Each e In arry
        Columns(e).Hidden = False
    Next
End Sub
Elements in the array MUST match, otherwise error.
 
You would typically search for a header value in the header row of the data eg: Name
Then extract the column from that search
Then use that

Something like:

Code:
Dim mySearch As Variant
Dim myColumn As Integer

' Header Range is B2:J2
'
  Range("B2").Select
  mySearch = "Name"
  myColumn = Range("B2:J2").Find( _
  What:=mySearch, _
  After:=ActiveCell, _
  LookIn:=xlFormulas, _
  LookAt:=xlPart, _
  SearchOrder:=xlByRows, _
  SearchDirection:=xlNext, _
  MatchCase:=False, _
  SearchFormat:=False).Column
   
  Columns(myColumn).EntireColumn.Hidden = False
 
Back
Top