I've coded myself into a corner with this one
The original code works beautifully as it searches my worksheet, finds predefined header name via Array("headername1", etc) and uses it to reference the Column letter("A") it's found at. It makes my sheet dynamic in that I can make changes with ease.
So this works and used as a working reference for my new bit of code below this
So what I'm doing now is referencing and converting Header name that I find into a Column letter. (lets say Header "Mr Excel" is a header on K1. I would refer to the Mr Excel column found on K as iColLtr. So on and so forth for each Header I need to reference.)
The problem is the code to reference multiple column headers is lengthy if I have to do more than 10. So I am trying to condense the process only I'm running into a problem.
The ColLetterArray(ndx1) in my code is not doing what I'd like it to do which is to refer to iColLtr = replace(Cells...etc) then jColLtr = replace(Cells...etc) and so on and so forth.
I know I'm close but I'm missing a key piece of the puzzle.
The original code works beautifully as it searches my worksheet, finds predefined header name via Array("headername1", etc) and uses it to reference the Column letter("A") it's found at. It makes my sheet dynamic in that I can make changes with ease.
So this works and used as a working reference for my new bit of code below this
Code:
Dim ColIndex As Integer
Dim counter As Integer
Dim HeaderFound As Range
Dim ReArrangeCol As Variant
ReArrangeCol = Array("HeaderName1", "HeaderName2", "HeaderName3", "HeaderName4", "Ect")
counter = 1
For ColIndex = LBound(ReArrangeCol) To UBound(ReArrangeCol)
Set HeaderFound = Rows("1:1").find(ReArrangeCol(ColIndex), LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
If Not HeaderFound Is Nothing Then
If HeaderFound.Column <> counter Then
HeaderFound.EntireColumn.Cut
Columns(counter).Insert Shift:=xlToRight
Application.CutCopyMode = False
End If
counter = counter + 1
End If
Next ColIndex
So what I'm doing now is referencing and converting Header name that I find into a Column letter. (lets say Header "Mr Excel" is a header on K1. I would refer to the Mr Excel column found on K as iColLtr. So on and so forth for each Header I need to reference.)
The problem is the code to reference multiple column headers is lengthy if I have to do more than 10. So I am trying to condense the process only I'm running into a problem.
Code:
Dim counter As Integer
Dim HeaderFound As Range
Dim ColHeaderArray As Variant
Dim ColLetterArray As Variant
Dim ndx1 As Integer
ColHeaderArray = Array("HeaderName1", "HeaderName2", "HeaderName3", "HeaderName4", "Ect")
ColLetterArray = Array(iColLtr, jColLtr, kColLtr, lColLtr, lColLtr)
counter = 1
For ndx1 = LBound(ColHeaderArray) To UBound(ColHeaderArray)
Set HeaderFound = Rows("1:1").find(ColHeaderArray(ndx1), LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
If Not HeaderFound Is Nothing Then
[COLOR=#ff0000][B][U]ColLetterArray(ndx1)[/U][/B][/COLOR] = replace(Cells(1, HeaderFound.Column).address(0, 0), 1, "")
End If
Next ndx1
The ColLetterArray(ndx1) in my code is not doing what I'd like it to do which is to refer to iColLtr = replace(Cells...etc) then jColLtr = replace(Cells...etc) and so on and so forth.
I know I'm close but I'm missing a key piece of the puzzle.
Last edited: