VBA - Collecting all the heading names into an array

poduk

New Member
Joined
Feb 12, 2017
Messages
23
Hi

Please can you help.

I am new to programming and maybe mixing up different languages.
The goal of my code is to collect the names of all the columns currently in my excel sheet.
The excel sheet can change in width (more or less columns) and content (heading names)


Code:
Dim i As Integer
i = 0
Range("A1").Activate  ' row 1 contains column headings
Do While Not IsEmpty(ActiveCell)  ' stop if the column heading is empty 
Heading(i) = ActiveCell.Value ' copy the heading name into array positon 0 
ActiveCell.Offset(, 1).Activate  ' move across to the next heading.
    i = i + 1 ' increase array position by 1
Loop ' loop until the end of the headings

I am hoping get an array with all column headings in it
Heading (A1, B1, C1)

Then use heading(0) to call A1 into my code.

Is it possible to do this?
If so, any idea where I am going wrong?

Thanks

Phil
 
Last edited:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
This seems to work.
I have set Heading to 60 because it should always be lower than that.

Code:
Sub datacollect()
Dim i As Integer
i = 0
Dim heading(60) As Variant
Range("A1").Activate  ' row 1 contains column headings
Do While Not IsEmpty(ActiveCell)
heading(i) = ActiveCell.Value
ActiveCell.Offset(, 1).Activate  ' next cell down
    i = i + 1
Loop
MsgBox heading(0)
End Sub
 
Upvote 0
I would avoid setting the number of elements as a constant, it means that if you go on to use the array later you need to check they hold a value. Also avoid using activate where possible:

Code:
Sub datacollect()
Dim heading() As String
Dim i As Integer
i = -1
For Each x In Rows(1).Cells
    If x.Value = "" Then Exit For
    i = i + 1
    ReDim Preserve heading(i) As String
    heading(i) = x.Value
Next x
End Sub
 
Upvote 0
I would avoid setting the number of elements as a constant, it means that if you go on to use the array later you need to check they hold a value. Also avoid using activate where possible:

Code:
Sub datacollect()
Dim heading() As String
Dim i As Integer
i = -1
For Each x In Rows(1).Cells
    If x.Value = "" Then Exit For
    i = i + 1
    ReDim Preserve heading(i) As String
    heading(i) = x.Value
Next x
End Sub

Thank you, I will give it a go.

I have another fairly related question.

I want to use the array's as part of a recordset filter. I
Array 1 (heading(1)), selecting the column in this case "Serial Number"
Array 2 (vRecord(1)), selects the serial number record. for example "123456"

I know filters should look like this but not sure how to do that with array values.
recordset.Filter = "LastName = 'Jones'

I have tried a few different version but they all fail. my last attempt was
recordset.Filter = "heading(1)='" & vRecord(1) & "'"
 
Last edited:
Upvote 0
The goal of my code is to collect the names of all the columns currently in my excel sheet.
The excel sheet can change in width (more or less columns) and content (heading names)

I am hoping get an array with all column headings in it
Heading (A1, B1, C1)
Assuming your Heading variable is declared as Variant, this single line of code will create a 1-based array from all of the Hearders in Row 1 (assumed to be contiguous with no intermediate blank columns which appears to be what your posted code suggests)...
Code:
[table="width: 500"]
[tr]
	[td]Headers = Application.Index(Range("A1", Cells(1, Columns.Count).End(xlToLeft)).Value, 1, 0)[/td]
[/tr]
[/table]
 
Upvote 0
You are correct, no blanks.

You code works great and clean the VBA up a lot. :)

Any idea on the command needed to recordset filters?
 
Upvote 0
Any idea on the command needed to recordset filters?
Unfortunately, I have almost no experience with databases themselves, so I am not familiar enough with recordsets to be able to advise you. I am sure someone who is familiar with them should come along to answer you question. If that does not happen, you might consider asking your question in a new thread as it is different enough from the purpose of the original thread here to qualify as a new question.
 
Upvote 0
Hi

This code works fine for my heading because it has no spaces.
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Headers = Application.Index(Range("A1", Cells(1, Columns.Count).End(xlToLeft)).Value, 1, 0)[/TD]
[/TR]
</tbody>[/TABLE]

Is there a code similar to this that will collect the whole row including blanks?

I can use UBound(headers) to get the number of columns (items in the variant) but I am struggling to get a command that works without activeCell (I see the drawbacks of activecell now)

Once I have a code, I plan on looping this until I have collected every row of data in the table.

Thank you for all the help so far.

Phil
 
Last edited:
Upvote 0
Including blanks

ABC
1heading 1Heading 2Heading3
212
33

<tbody>
</tbody>

Your command collects all the heading fine because there is no gaps in the headings but the values (row) sometimes have empty cells.
Is there a code that will collect the row regardless of gaps?
Collect row 2 and then a loop to get the following rows.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,195
Members
449,072
Latest member
DW Draft

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