VBA to define a row as a named column

Posted on 2014-08-26
Medium Priority
Last Modified: 2014-08-27
I am struggling to write the code to define the data in a given row, in this case row 4, as a named range.  Once I have the named range, I can use it to populate a combobox on a form using: UserForm1.ComboBox1.RowSource = "TheRange"

I've researched trying to transpose it.  I definitely want to avoid copying and pasting the data into a worksheet.

Any help greatly appreciated!

Question by:Ben Cheetham
  • 3
  • 2
LVL 28

Expert Comment

ID: 40286666
No need to define a named range. Try this:
Private Sub UserForm_Initialize()
    Me.ComboBox1.List = WorksheetFunction.Transpose(Range("A4:Z4))
End Sub

Open in new window

Assuming the data is in column A to Z.

Author Comment

by:Ben Cheetham
ID: 40286718
Love the simplicity of the code - thank you.

However, the data goes beyond column z and this will vary.  But that's unimportant as I could set it to a column that it will never exceed.

I am struggling, though, with referencing the row as it is not on the active sheet. You sound like you will know how to help!

Thanks again.
LVL 28

Accepted Solution

MacroShadow earned 2000 total points
ID: 40287167
Just precede the Range with the sheet index or name. i.e.
Me.ComboBox1.List = WorksheetFunction.Transpose(Sheets(1).Range("A4:Z4"))
Me.ComboBox1.List = WorksheetFunction.Transpose(Sheets("FirstSheetName").Range("A4:Z4"))

Open in new window

To dynamically accommodate for changing last last column you can use the following:
Me.ComboBox1.List = WorksheetFunction.Transpose(Sheets(1).Range("A4:" & Replace(Cells(4, Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column).Address(False, False), "1", "")))
Me.ComboBox1.List = WorksheetFunction.Transpose(Sheets("FirstSheetName").Range("A4:" & Replace(Cells(4, Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column).Address(False, False), "1", "")))

Open in new window


Author Comment

by:Ben Cheetham
ID: 40287289
Hi again and thank you for persevering.  I had tried prefixing the range with the sheet name but hadn't been successful.

Unfortunately, it is still not working.  (I haven't yet tried the dynamic version but this relies on the same principle).

This is my code, in the code for the Userform (named "UserForm1").  I tried a bigger range but then reduced it to only three cells in case some of the formatting of later cells was causing an issue.

When the form opens, the combobox doesn't list any values.

Private Sub UserForm1_initialize()
    Me.ComboBox1.List = WorksheetFunction.Transpose(Sheets("Class").Range("A4:C4"))
End Sub

I really hope you can persevere again.

Author Comment

by:Ben Cheetham
ID: 40287339
Hi Again

I've worked out that it wasn't calling the Initialize macro and corrected this and your code works beautifully.

Just fine tuning it so may have a further query.

But thank you for your excellent help so far.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question