VBA to define a row as a named column

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!

Thanks
Ben
Ben CheethamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MacroShadowCommented:
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.
0
Ben CheethamAuthor Commented:
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.
0
MacroShadowCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ben CheethamAuthor Commented:
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.
Thanks
Ben
0
Ben CheethamAuthor Commented:
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.
Ben
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.