How to use column header in macro

Melzebu

New Member
Joined
Jan 24, 2022
Messages
8
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
Hi everyone,

I'm quite new with macros in excel so I'm relying on your expertise.
Let's say I have a table with headers and I'm using the count formula in a macro and this is the code:
Sub Test()
'
' Test Macro
'
'
ActiveCell.FormulaR1C1 = "=COUNT(Sheet1!C)"
End Sub

The header of the column would be "Apples".
How can I change the code to have the count performed in the column called "Apples"? And where exactly within the initial code should I put it?

I hope my question is clear.
Thank you for your help
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If your is a structured table then the formula would be, simply:
VBA Code:
=COUNT(YourTableName[Apples])
Use ".Formula" (and not .FormulaR1C1) to insert it into a cell

Bye
 
Upvote 0
VBA Code:
Sub Test()

Dim foundCol As Variant

foundCol = Application.Match("Apples", Range("1:1"), 0)
If Not IsError(foundCol) Then
    ActiveCell.Formula = "=COUNT(Sheet1!" & Cells(1, foundCol).EntireColumn.Address & ")"
End If

End Sub

WBD
 
Upvote 0
Solution
Thank you for your replies.
WBD's advice worked but I have an additional question now.
I'm trying to calculate an average and I'm using a macro like this:
Sub Average()
'
' Average Macro
'
'
ActiveCell.FormulaR1C1 = _
"=IFERROR(AVERAGEIF('Report'!C[-2],""Severity 1"",'Report'!C[3]),""-"")"
End Sub

I would like to change 'Report'!C[-2] with the column header "Severity" and 'Report'!C[3] with the column header "THT".
How can I change it?

Thank you again for your help.
Cheers
 
Upvote 0
VBA Code:
Sub Test()

Dim foundCol As Variant

foundCol = Application.Match("Apples", Range("1:1"), 0)
If Not IsError(foundCol) Then
    ActiveCell.Formula = "=COUNT(Sheet1!" & Cells(1, foundCol).EntireColumn.Address & ")"
End If

End Sub

WBD
Hi WBD,
do you think you could help me also with the averageif formula in my last comment?

Thank you a lot
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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