reference column by name vs placement

Norske77

New Member
Joined
Dec 7, 2017
Messages
13
I have the following line and I would like to modify it to reference the column by its header name - 'Last Name" vs. the placement - Offset(0, -5). Is this possible?

Code:
.Cells(Row, 6) = Found.Offset(0, -5).Value

Thank you.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Something like...
Code:
Dim i as long
i[COLOR=#222222][FONT=Verdana]=1
[/FONT][/COLOR][LEFT][COLOR=#222222][FONT=Verdana]'(If header is in row 1)[/FONT][/COLOR][/LEFT]
Do until cells(1,i).value ="Last Name"
i=i+1
Next i
then you can refer to the cell as cell(1,i)
 
Last edited:
Upvote 0
So I tried and failed miserably to make the update you suggested. A little more background on the issue. I found and modified the code below and it was working as expected then they made a change to accept the column heading in any order. So now the only constant is the header names. I am trying to modify the section below to reference the header names and not the column placement offset from the found value. Any help you can offer would be greatly appreciated! Thanks in advance.

Code:
Do While StrFile <> ""            Set Wb = Workbooks.Open(Filename:=StrPath & "\" & StrFile, UpdateLinks:=0, ReadOnly:=True, AddToMRU:=False)
            For Each Wk In Wb.Worksheets
            Set Found = Wk.UsedRange.Find(RngSearch)
                If Not Found Is Nothing Then
                    StrAddress = Found.Address
                End If
                Do
                    If Found Is Nothing Then
                        Exit Do
                    Else
                        Count = Count + 1
                        Row = Row + 1
                        .Cells(Row, 1) = Wb.Name
                        .Cells(Row, 2) = Wk.Name
                        .Cells(Row, 3) = Found.Address
                        .Cells(Row, 4) = Found.Value
                        .Cells(Row, 5) = Found.Offset(0, -8).Value
                        .Cells(Row, 6) = Found.Offset(0, -5).Value
                        .Cells(Row, 7) = Found.Offset(0, -6).Value
                        .Cells(Row, 8) = Found.Offset(0, 0).Value
                        .Cells(Row, 9) = Found.Offset(0, 2).Value
                        .Cells(Row, 10) = Found.Offset(0, 3).Value
                        .Cells(Row, 11) = Found.Offset(0, 12).Value
                        .Cells(Row, 12) = Found.Offset(0, 9).Value
                        .Cells(Row, 13) = Found.Offset(0, 11).Value
                        .Cells(Row, 14) = Found.Offset(0, 17).Value
                        .Cells(Row, 15) = Found.Offset(0, 19).Value
                        .Cells(Row, 16) = Found.Offset(0, 14).Value
                        .Cells(Row, 17) = Found.Offset(0, 15).Value
                        .Cells(Row, 18) = Found.Offset(0, 16).Value
                        .Cells(Row, 19) = Found.Offset(0, 18).Value
                    End If
                    Set Found = Wk.Cells.FindNext(After:=Found)
                Loop While StrAddress <> Found.Address
            Next
            Wb.Close (False)
            StrFile = Dir
        Loop
 
Upvote 0
Hi, I am not sure to understand exactly. As I understand it, the macro find a cell (called found) in an array (called RngSearch) and you want to have the header reference of the column of the found cell.

If the header is in line 1, then the range of the header is
Code:
[LEFT][COLOR=#222222][FONT=Verdana]cells(1,found.column)[/FONT][/COLOR][/LEFT]
and its value is
Code:
[COLOR=#222222][FONT=Verdana]cells(1,found.column).value[/FONT][/COLOR]
 
Last edited:
Upvote 0
I will try to explain better. I am looking for a specified value in all files in a folder and then when found return data from the row in which the specified value was found based on the column heading rather then the offset from where the specified value was found. The column headings names are constant but their placement in each file is not. When I originally put this together it was based on knowing the placement of each column, which has since changed. In the example below I would look for value "2" and expect that value "C2" be returned based on the header name "C".

Code:
[TABLE="width: 256"]
 <colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>[TR]
  [TD="class: xl63, width: 64"]A[/TD]
  [TD="class: xl63, width: 64"]B[/TD]
  [TD="class: xl64, width: 64"][B][COLOR=#ff0000]C[/COLOR][/B][/TD]
  [TD="class: xl63, width: 64"]D[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]1[/TD]
  [TD="class: xl63"]B1[/TD]
  [TD="class: xl63"]C1[/TD]
  [TD="class: xl63"]D1[/TD]
 [/TR]
 [TR]
  [TD="class: xl64"][COLOR=#ff0000][B]2[/B][/COLOR][/TD]
  [TD="class: xl63"]B2[/TD]
  [TD="class: xl65"][B][COLOR=#00ff00]C2[/COLOR][/B][/TD]
  [TD="class: xl63"]D2[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]3[/TD]
  [TD="class: xl63"]B3[/TD]
  [TD="class: xl63"]C3[/TD]
  [TD="class: xl63"]C3[/TD]
 [/TR]
 [TR]
  [TD][/TD]
  [TD][/TD]
  [TD][/TD]
  [TD][/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]A[/TD]
  [TD="class: xl64"][B][COLOR=#ff0000]C[/COLOR][/B][/TD]
  [TD="class: xl63"]D[/TD]
  [TD="class: xl63"]B[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]1[/TD]
  [TD="class: xl63"]C1[/TD]
  [TD="class: xl63"]D1[/TD]
  [TD="class: xl63"]B1[/TD]
 [/TR]
 [TR]
  [TD="class: xl64"][B][COLOR=#ff0000]2[/COLOR][/B][/TD]
  [TD="class: xl65"][B][COLOR=#00ff00]C2[/COLOR][/B][/TD]
  [TD="class: xl63"]D2[/TD]
  [TD="class: xl63"]B2[/TD]
 [/TR]
 [TR]
  [TD="class: xl63"]3[/TD]
  [TD="class: xl63"]C3[/TD]
  [TD="class: xl63"]D3[/TD]
  [TD="class: xl63"]B3[/TD]
[/TR]
</tbody>[/TABLE]

Code:
Sub SearchFolders()    Dim Fso As Object
    Dim Fld As Object
    Dim RngSearch As Range
    Dim StrPath As String
    Dim StrFile As String
    Dim Out As Worksheet
    Dim Wb As Workbook
    Dim Wk As Worksheet
    Dim Row As Long
    Dim Found As Range
    Dim Column As Range
    Dim StrAddress As String
    Dim FileDialog As FileDialog
    Dim Update As Boolean
    Dim Count As Long
    Dim sName As Variant
    On Error GoTo ErrHandler
    Set FileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    FileDialog.AllowMultiSelect = False
    FileDialog.Title = "Select a forlder"
    If FileDialog.Show = -1 Then
        StrPath = FileDialog.SelectedItems(1)
    End If
    If StrPath = "" Then Exit Sub
    Set RngSearch = ActiveWorkbook.Worksheets("Search").Range("B2:B2")
    Update = Application.ScreenUpdating
    Application.ScreenUpdating = False
    Set Out = Worksheets.Add
    ActiveSheet.Name = Replace(Format(Now, "yyyy.mm.dd hh:mm:ss"), ":", ".")
    Row = 1
    With Out
        .Cells(Row, 1) = "Workbook"
        .Cells(Row, 2) = "Worksheet"
        .Cells(Row, 3) = "Cell"
        .Cells(Row, 4) = "Text in Cell"
        .Cells(Row, 5) = "Company Name"
        .Cells(Row, 6) = "Last Name"
        .Cells(Row, 7) = "First Name"
        .Cells(Row, 8) = "SSN"
        .Cells(Row, 9) = "Date of Birth"
        .Cells(Row, 10) = "Date of Hire"
        .Cells(Row, 11) = "QLE Date"
        .Cells(Row, 12) = "Product Type"
        .Cells(Row, 13) = "Coverage Effective Date"
        .Cells(Row, 14) = "SP Coverage Effective Date"
        .Cells(Row, 15) = "CH Coverage Effective Date"
        .Cells(Row, 16) = "Coverage Tier"
        .Cells(Row, 17) = "EE CI Approved Coverage Amount"
        .Cells(Row, 18) = "SP CI Approved Coverage Amount"
        .Cells(Row, 19) = "CH CI Approved Coverage Amount"
        Set Fso = CreateObject("Scripting.FileSystemObject")
        Set Fld = Fso.GetFolder(StrPath)
        StrFile = Dir(StrPath & "\*.csv*")
        Do While StrFile <> ""
            Set Wb = Workbooks.Open(Filename:=StrPath & "\" & StrFile, UpdateLinks:=0, ReadOnly:=True, AddToMRU:=False)
            For Each Wk In Wb.Worksheets
            Set Found = Wk.UsedRange.Find(RngSearch)
            Set Column = Wk.UsedRange.Find(RngSearch)
                If Not Found Is Nothing Then
                    StrAddress = Found.Address
                End If
                Do
                    If Found Is Nothing Then
                        Exit Do
                    Else
                        Count = Count + 1
                        Row = Row + 1
                        .Cells(Row, 1) = Wb.Name
                        .Cells(Row, 2) = Wk.Name
                        .Cells(Row, 3) = Found.Address
                        .Cells(Row, 4) = Found.Value
                        .[COLOR=#ff0000]Cells(1, Found.Column) = Cells(1, Found.Column).Value   [/COLOR][COLOR=#00ff00]' Code you suggested pulls back the header name which I am handling above.  What I need is the value from the cell in that column[/COLOR][COLOR=#ff0000][/COLOR]
                        .Cells(Row, 6) = Found.Offset(0, -5).Value
                        .Cells(Row, 7) = Found.Offset(0, -6).Value
                        .Cells(Row, 8) = Found.Offset(0, 0).Value
                        .Cells(Row, 9) = Found.Offset(0, 2).Value
                        .Cells(Row, 10) = Found.Offset(0, 3).Value
                        .Cells(Row, 11) = Found.Offset(0, 12).Value
                        .Cells(Row, 12) = Found.Offset(0, 9).Value
                        .Cells(Row, 13) = Found.Offset(0, 11).Value
                        .Cells(Row, 14) = Found.Offset(0, 17).Value
                        .Cells(Row, 15) = Found.Offset(0, 19).Value
                        .Cells(Row, 16) = Found.Offset(0, 14).Value
                        .Cells(Row, 17) = Found.Offset(0, 15).Value
                        .Cells(Row, 18) = Found.Offset(0, 16).Value
                        .Cells(Row, 19) = Found.Offset(0, 18).Value
                    End If
                    Set Found = Wk.Cells.FindNext(After:=Found)
                Loop While StrAddress <> Found.Address
            Next
            Wb.Close (False)
            StrFile = Dir
        Loop
        .Columns("A:Z").EntireColumn.AutoFit
    End With
    MsgBox Count & "cells have been found"
ExitHandler:
    Set Out = Nothing
    Set Wk = Nothing
    Set Wb = Nothing
    Set Fld = Nothing
    Set Fso = Nothing
    Application.ScreenUpdating = Update
    Exit Sub
ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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