Require Syntax for Worksheet and column reference after the Column Name Searched by Header

SamDsouza

Board Regular
Joined
Apr 16, 2016
Messages
205
Hello
What would be the correct Syntax for Column reference when searched by header

VBA Code:
Public Sub FormulaWorking()

Dim wsInv as Worksheet
Dim wsMis As Worksheet
Dim SrchtxtStr As String

Set wsMis = Worksheets("MIS")

Set wsInv = Worksheets("InvSheet")

With wsInv.Range("A1:Z1")

SrchtxtStr = "Riveria"

Set cfind = .Find(What:=SrchtxtStr, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not cfind Is Nothing Then
        MsgBox SrchtxtStr & vbCrLf & "Is in Col. No " & cfind.Column & "   Row No. " & cfind.Row
     Else
        MsgBox "Not Found in Range"
      
End If
End With

With wsMis
    .Range("CL3" & ":CL" & lastRow).Formula = "=SUMIFS('InvSheet'!S:S,'InvSheet'!A:A,MIS!A:A,'InvSheet'!K:K,MIS!$CL$1)"
End With
End Sub

'After searching the column name by header name ie i searched the column as "S" as per above formula
'So as per above formula How to have correct syntax for
'' .Range("CL3" & ":CL" & lastRow).Formula = "=SUMIFS('InvSheet'!cfind.Column , 'InvSheet'!A:A,......)
'' And
'' .Range("CL3" & ":CL" & lastRow).Formula = "=SUMIFS(wsInv.cfind.Column , wsInv.column ("A:A").....


Your Help will be highly appreciated
SamD
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi, you could try something like this.

VBA Code:
With wsMis
    .Range("CL3" & ":CL" & lastrow).Formula = "=SUMIFS('InvSheet'!" & Columns(cfind.Column).Address & ",'InvSheet'!A:A,MIS!A:A,'InvSheet'!K:K,MIS!$CL$1)"
End With
 
Upvote 0
Thank you FormR. Indeed it was great input

VBA Code:
With wsMis
    .Range("CL3" & ":CL" & lastrow).Formula = "=SUMIFS('InvSheet'!" & Columns(cfind.Column).Address & ",'InvSheet'!A:A,MIS!A:A,'InvSheet'!K:K,MIS!$CL$1)"
End With

As this is related to syntax
and worksheet is already defined or SET
Can I
.Formula = "=SUMIFS(wsInv.Columns(cfind.Column).Address), (wsInv.Columns(cfind.Column).Address), (wsMIS. Columns("A:A").Address) .......


Thanks Once again
SamD
 
Upvote 0
One way to get the worksheet name from your variable would be like this.

VBA Code:
With wsMis
   .Range("CL3" & ":CL" & lastrow).Formula = "=SUMIFS('" & wsInv.Name & "'!" & Columns(cfind.Column).Address & ",'InvSheet'!A:A,MIS!A:A,'InvSheet'!K:K,MIS!$CL$1)"
End With

And the same sort of thing for the other parts of the formula.
 
Upvote 0
Thanks FormR but am really sorry to bother you for petty lssues

I get error 1004 Application defined.... Error
because i wanted to use in the below manner though above posts are with correct syntax if not used with the Columns property
Any ideas how can i use correctly with Columns("A:A").Address or have i gone wrong using & wsMis.Range("CL1").Value & "$)" at end of formula

.Range("CL3" & ":CL" & lastRow).Formula = "=SUMIFS('" & wsInv.Name & "'!" & Columns(colFind.Column).Address & ",'" & _
wsInv.Name & "'!" & Columns("A:A").Address & wsMis.Name & "'!" & Columns("A:A").Address & ",'" & _
wsInv.Name & "'!" & Columns("K:K").Address & wsMis.Name & "'!$" & wsMis.Range("CL1").Value & "$)"

SamD
 
Upvote 0
Guys Corrected
Corrections in bold
.Range("CL3" & ":CL" & lastRow).Formula = "=SUMIFS('" & wsInv.Name & "'!" & Columns(colFind.Column).Address(0,0) & ",'" & _
wsInv.Name & "'!" & Columns("A").Address (0,0) & ","& wsMis.Name & "'!" & Columns("A").Address (0,0) & ",'" & _
wsInv.Name & "'!" & Columns("K").Address (0,0) & "," & wsMis.Name & "'!" & wsMis.Range("CL1").Address & ")"

SamD
 
Upvote 0
& Columns("A").Address (0,0) &

Hi, this doesn't make sense to me, it will always return "A:A" so why not just use that directly?

Same for column K and cell CL1.

Not tested, but for example.
VBA Code:
"=SUMIFS('" & wsInv.Name & "'!" & Columns(colFind.Column).Address(0, 0) & ",'" & _
wsInv.Name & "'!A:A,'" & wsMis.Name & "'!A:A,'" & wsInv.Name & "'!K:K,'" & wsMis.Name & "'!CL1)"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,331
Members
452,636
Latest member
laura12345

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