简体   繁体   中英

Excel VBA - Pull information into user form to update

I am trying to create a userform that allows the users to update issues stored in a specific sheet (called Issues List). I have built a dropdown list using data validation that allows the user to select the unique issue name from a list. I have created a button next to that dropdown which opens up the userform and correctly imports the issue name identified from the dropdown.

What I need to figure out is, when the user form is initiated how do I have it search column B in my Issues List sheet and identify which row contains the issue selected by the user, and populate the fields of the user form with the information found in rows CX of the Issues List sheet.

What I have been trying to use is an index match function, but have been unsuccessful in getting the code to work. An example of what I have been using is:

Resolved.Value = Application.WorksheetFunction.index
('Issue List'!$X$2:$X$1000,Application.WorksheetFunction.match
('Priority Table'!I35,'Issue List'!$B$2:$B$1000,0))

Any help would be greatly appreciated.

Thanks in advance!

When you use Worksheet Functions in VBA, you still have to pass in the ranges using VBA language:

So instead of:

'Issue List'!$X$2:$X$1000

you would use:

Worksheets("Issue List").Range("X2:X1000")

And instead of:

'Priority Table'!I35

Just use:

Worksheets("Priority Table").Range("I35")

Note that you can also refer to ranges by names, which can make coding easier and also far safer. When you insert rows in spreadsheets, Excel doesn't automatically update ranges in any VBA code. A reference to I35 will always to be I35.

Instead, define a name for cell I35 in Excel as normal, then refer to it in the code.

For example, if you name I35 as "Issue"

You can refer to the cell by:

Range("Issue")

(If it is a global variable, which it is be default as long as it's a unique name in the workbook, you don't need to use the Sheets("Priority Table") qualifier.

Refer to this documentation for more info on how to refer to ranges in Excel from VBA: https://msdn.microsoft.com/en-us/library/office/gg192736(v=office.14).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM