0

I'm a novice at VBA (just starting to learn). In my userform I have a combobox with values from 1 to 12(not string) representing the months. I want the user to pick a month and based on that, the multiple listboxes and labels I have placed should get filled by the relevant values in one of the 12 sheets representing each month. as I am a novice I have a lot of problems here but for starters the following lines do not seem to work on userform_initiate()

For j = 0 To 1
    arr_trh(0, j) = Sheets("Sheet6").Cells(4, j + 1)
Next j

I can get it to work for a single sheet by using

arr_trh(0, j) = Sheet6.Cells(4, j + 1)

However, what I'm trying to do later on is to create a string and somehow concatenate "Sheet" and combobox value to pass on to Sheets() function.

Any help would be appreciated Thanks

3
  • "the following lines do not seem to work" -- that isn't a very helpful problem statement. Are you getting an error message? If so -- what? On what line? Commented May 20, 2018 at 13:33
  • A userform and a worksheet are not the same thing.I think you're working with worksheets, so avoid any instruction that has to do with userforms for now. The worksheet is the spreadsheet, with tabs at the bottom; user forms are custom form elements designed in the Visual Basic Editor. A Google search will explain better. Commented May 20, 2018 at 13:33
  • What about Sheets("Sheet" & x).Cells()? Where x is string type variable and x gets value from combobox. Commented May 20, 2018 at 14:35

2 Answers 2

1

Rather than referring to the Sheet object like:

v = Sheet1.Range("A1")

use:

v = Sheets(1).Range("A1")

which you can index like:

v = Sheets(i).Range("A1")

where i is a variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! that seems to have worked, I had no idea you could use it like that. Thanks John and Ashlee for the answers as well. Now to see if I can assign combobox selection to it.
0

I guess, my this answer will help you to figure out how to refer to sheets. Also, it tells about caveats of Index property.

1 Comment

That was an excellent post and it helped me figurd out what's going on a lot. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.