Loop By Header Name and Not Column Number

benntw

Board Regular
Joined
Feb 17, 2014
Messages
222
Office Version
  1. 365
Platform
  1. Windows
Is there a way to loop by header name and not column number ? Below is my code to loop between sheets by column number to compare new to old data. It would make it more dynamic incase columns get moved around and I would not need to go back and redo the column numbers. Appreciate the help.

VBA Code:
Temp_RS.Activate

i = 2 'First Row Of Data On Temp_RS Sheet

        Do Until Cells(i, 1).Value = ""
            NotiNum = Cells(i, 2).Value
            ScopeDesc = Cells(i, 3).Value
            ScopeLoc = Cells(i, 4).Value
            OpsMem = Cells(i, 5).Value
            ScopeMem = Cells(i, 6).Value
            ShortCode = Cells(i, 7).Value
            ScopeCode = Cells(i, 8).Value

    KeepSearching = True
    RowNum = 4 'First Row Of Data On Available In R Systems Sheet

Current_RS.Activate

    Do Until KeepSearching = False

        If Cells(RowNum, 1).Value = ScopeNum Then
            Cells(RowNum, 2).Value = NotiNum
            Cells(RowNum, 3).Value = ScopeDesc
            Cells(RowNum, 4).Value = ScopeLoc
            Cells(RowNum, 5).Value = OpsMem
            Cells(RowNum, 6).Value = ScopeMem
            Cells(RowNum, 7).Value = ShortCode
            Cells(RowNum, 8).Value = ScopeCode

    KeepSearching = False

        ElseIf Cells(RowNum, 1).Value = "" Then

            Cells(RowNum, 1).Value = ScopeNum
            Cells(RowNum, 2).Value = NotiNum
            Cells(RowNum, 3).Value = ScopeDesc
            Cells(RowNum, 4).Value = ScopeLoc
            Cells(RowNum, 5).Value = OpsMem
            Cells(RowNum, 6).Value = ScopeMem
            Cells(RowNum, 7).Value = ShortCode
            Cells(RowNum, 8).Value = ScopeCode

    KeepSearching = False

        Else

        RowNum = RowNum + 1

    End If

Loop

i = i + 1

Temp_RS.Activate

Loop
 
Last edited by a moderator:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
When posting vba code in the forum please use the available code tags. My signature block below has more details. I have added them for you this time. :)
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,028
Members
448,940
Latest member
mdusw

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