Calling HeaderName to find its respective column with Set = .find but when Column Searched The same column reference is overlapped in a Formula. How t

NimishK

Well-known Member
Joined
Sep 4, 2015
Messages
684
Hello
I’ve created a Procedure to call For Header to find its respective column with Set = .find
Firstly I created the following
Rich (BB code):
Public Sub ColHeaderFindPos()

Dim ws1 As Worksheet, ws2 As Worksheet
Dim AmtStr As String,  cgst12Str As String, sgst12Str As String, cgst14Str As String, Sgst14str As String
Dim AmtColFind As Range, cgst12ColFind As Range, sgst12ColFind As Range, cgst12ColFind As Range, sgst14ColFind As Range

Set ws1 = Worksheets("Sheet3")
Set ws2 = Worksheets("Sheet4")

AmtStr = “Amount”
cgst12Str = ”CGST 12%
sgst12Str =” SGST 12%
cgst14Str = ”CGST 14%
sgst14Str =” SGST 14%

Set amtColFind = .Find(What:= AmtStr, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
Set cgst12ColFind = .Find(What:= cgst12Str, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
Set sgst12ColFind = .Find(What:= sgst12Str, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
Set cgst14ColFind = .Find(What:= cgst14Str, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
Set sgst14ColFind = .Find(What:= cgst14Str, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)


With ws1
      .Range("L2" & ":L" & lastRow).Formula = "=SUMIFS('" & ws2.Name & "'!" & Columns(amtColFind.Column).Address(0, 0) & ",'" & _
ws2.Name & "'!" & Columns("A").Address(0, 0) & "," & ws1.Name & "!" & Columns("A").Address(0, 0) & ",'" & ' ws2.Name & "'!" & Columns("K").Address(0, 0) & "," & ws1.Name & "!" & ws1.Range("L1").Address & ")"

.Range("M3" & ":M" & lastRow).Formula = "=SUMIFS('" & ws2.Name & "'!" & Columns(cgst12ColFind.Column).Address(0, 0) & ",'" & _
ws2.Name & "'!" & Columns("A").Address(0, 0) & "," & ws1.Name & "!" & Columns("A").Address(0, 0) & ",'" &  ws2.Name & "'!" & Columns("K").Address(0, 0) & "," & ws1.Name & "!" & ws1.Range("CL1").Address & ")" &  "+SUMIFS" & "('" & ws2.Name & "'!" & Columns(sgst12ColFind.Column).Address(0, 0) & ",'" & ws2.Name & "'!" & Columns("A").Address(0, 0) & "," &  ws1.Name & "!" & Columns("A").Address(0, 0) & ",'" & _
ws2.Name & "'!" & Columns("K").Address(0, 0) & "," & ws1.Name & "!" & ws1.Range("L1").Address & ")"
End With
End Sub
InOrder to avoid Following Lines Marked in Red
Then I Created following procedure with Array to Call

Rich (BB code):
Sub colNameHdrFindArray(ByVal HeaderName As String, ByRef columnRef As Variant)

    Dim ws1 As Worksheet,  ws2 As Worksheet, HeaderArrayNames As Variant, colfind As Range
    Dim i As Long
    Set wKS = ActiveWorkbook.Worksheets("Sheet1")
    i = 1

    HeaderArrayNames = Array("Amount", "CGST 12%", "SGST 12%", "CGST 14%", "SGST 14%", "Contract ID", "POS")

    With ws1.Range("A1:J1")
     For i = LBound(HeaderArrayNames) To UBound(HeaderArrayNames)

           Set colfind = .Find(What:=HeaderArrayNames(i), LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)

              If Not colfind Is Nothing Then
                   If HeaderName = HeaderArrayNames(i) Then
                      MsgBox HeaderName & vbCrLf & "Is in Col. No " & colfind.Column                        
                      columnRef = colfind.Column
                     Exit For
                 End If
                Else
                     MsgBox "Not Found in Range"
                   End If
         Next
End With
End Sub

Public Sub ColHeaderFindPos()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim AmtStr As String,  cgst12Str As String, sgst12Str As String, cgst14Str As String, Sgst14str As String
Dim AmtColFind As Range, cgst12ColFind As Range, sgst12ColFind As Range, cgst12ColFind As Range, sgst14ColFind As Range

Set ws1 = Worksheets("Sheet3")
Set ws2 = Worksheets("Sheet4")

With ws1

Call colNoSheetNameHdrRow("Amount", columnRef) 

      .Range("L2" & ":L" & lastRow).Formula = "=SUMIFS('" & ws2.Name & "'!" & Columns(columnRef).Address(0, 0) & ",'" & _

ws2.Name & "'!" & Columns("A").Address(0, 0) & "," & ws1.Name & "!" & Columns("A").Address(0, 0) & ",'" & ' ws2.Name & "'!" & Columns("K").Address(0, 0) & "," & ws1.Name & "!" & ws1.Range("L1").Address & ")"

Call colNoSheetNameHdrRow("CGST 12%", columnRef)
Call colNoSheetNameHdrRow("SGST 12%", columnRef)

.Range("M3" & ":M" & lastRow).Formula = "=SUMIFS('" & ws2.Name & "'!" & Columns(columnRef).Address(0, 0) & ",'" & _

ws2.Name & "'!" & Columns("A").Address(0, 0) & "," & ws1.Name & "!" & Columns("A").Address(0, 0) & ",'" &  ws2.Name & "'!" & Columns("K").Address(0, 0) & "," & ws1.Name & "!" & ws1.Range("L1").Address & ")" &  "+SUMIFS" & "('" & ws2.Name & "'!" & Columns(columnRef).Address(0, 0) & ",'" & ws2.Name & "'!" & Columns("A").Address(0, 0) & "," &  ws1.Name & "!" & Columns("A").Address(0, 0) & ",'" & _
ws2.Name & "'!" & Columns("K").Address(0, 0) & "," & ws1.Name & "!" & ws1.Range("L1").Address & ")"

End With
End Sub
So which Columns(columnRef).Address(0, 0) is refered to when
Call colNoSheetNameHdrRow("CGST 12%", columnRef)
Call colNoSheetNameHdrRow("SGST 12%", columnRef)


How can the above be coded for calling a procedure with Headername and its respective column
Call colNoSheetNameHdrRow("CGST 12%", cgst12columnRef)
Call colNoSheetNameHdrRow("SGST 12%", sgst12columnRef)


So when using column reference can use as follows

Columns(cgst12columnRef).Address(0, 0)
Columns(sgst12columnRef).Address(0, 0)


Thanks in Advance ?

NimishK
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,215,430
Messages
6,124,850
Members
449,194
Latest member
HellScout

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