VBA & Macro Problems

How to Retrieve Commodity Prices by ID in Excel VBA Using WPS Spreadsheets

Natalie TaylorNatalie Taylor Nov 13, 2026 869 views

Question details

The user needs a VBA user form in WPS Spreadsheets that returns a commodity price by ID and also handles cases where multiple commodity records match or no price exists.

How to Retrieve Commodity Prices by ID in Excel VBA Using WPS Spreadsheets
Product
WPS Spreadsheets / Excel VBA
Device & OS
not provided
Scenario
Creating a spreadsheet VBA user form that accepts an ID, package code, and supplier code to retrieve the matching commodity price.
Observed behavior
The form should find matching commodity rows and either display a valid price or clearly handle duplicate matches and missing prices.
Before you start

Prepare a sample workbook with the commodity worksheet layout, user-form controls, lookup fields, and expected output before writing or testing the VBA code. Confirm that macros are enabled in WPS Spreadsheets so the VBA project can run.

Solution 1Recommended

Create a VBA User Form That Looks Up Commodity Prices by ID, Package Code, and Supplier Code

Use this approach when you need a form to return a price after a user enters an ID, package code, and supplier code, and to handle multiple matching records or missing prices.

A VBA user form is the most flexible way to capture the three lookup keys and return the matching commodity price. Keep the lookup data in a dedicated worksheet table so the code can identify columns by name and handle duplicate matches.

1
Prepare the commodity price worksheet

Open WPS Spreadsheets and create a data sheet with columns such as CommodityID, PackageCode, SupplierCode, and Price, then enter representative records including duplicate commodity IDs and one row with no price.

2
Insert a UserForm and lookup controls

Go to the Developer tab, click Visual Basic or Macro to open the VBA editor, choose Insert > UserForm, and add text boxes for ID, package code, and supplier code, a command button for lookup, and a label or list box for the result.

3
Write the matching and missing-price routine

Double-click the lookup button, then in the click event read the three input values, loop through the data range with Do While or For Each, compare all three fields, collect matches, and show a 'No price found' message when the price cell is empty.

4
Run and validate the user form

Press F5 in the VBA editor to launch the form, enter a known commodity ID, package code, and supplier code, confirm the price appears, then test a missing price and a duplicate-record case before saving as an .xlsm workbook.

Create a VBA User Form That Looks Up Commodity Prices by ID, Package Code, and Supplier Code
User form is ready: Use the same lookup routine on a copy of your production workbook and re-test whenever the commodity data columns change.
WPS Spreadsheets VBA

Build a Commodity Price Lookup VBA Form in WPS Spreadsheets

WPS Spreadsheets can run VBA user forms and macros, so you can build a price lookup by commodity ID, package code, and supplier code without changing your existing Excel workflow.

  1. 1. Open the macro-enabled workbook in WPS Spreadsheets: Click File > Open and select your .xlsm or .xlsx workbook with the commodity data and user form.
  2. 2. Enable the Developer tab: Go to File > Options > Customize Ribbon and turn on Developer if it is not already visible.
  3. 3. Create or edit the UserForm: Press Alt+F11 to open the VBA editor, then insert or open the UserForm for commodity price lookup.
  4. 4. Add matched-row lookup code: In the button click event, read the ID, package code, and supplier code, then loop through the data range to find matching commodity records.
  5. 5. Run the form and save: Press F5 to test the user form, confirm missing-price and multiple-match handling, then save as an Excel Macro-Enabled Workbook (.xlsm).
Use VBA controls and lookup logic for commodity pricing in a familiar spreadsheet interface.Maintains compatibility with Excel Macro-Enabled Workbook (.xlsm) files and common VBA project structures.Free and lightweight Office suite that reduces license costs for business VBA tools.Supports worksheet tables and identifiers for reliable price matching and duplicate handling.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA lookup return only the first match instead of multiple matching commodity records?

Use a loop or a filtered range in WPS Spreadsheets VBA instead of a single Find call. Initialize a Collection or list to collect every row where ID, package code, and supplier code match, then display all matches in a list box or select the first active price.

How do I handle missing prices in the VBA user form?

Before assigning the price to the label or cell, check whether the price cell is empty or contains a placeholder value. If it is missing, show a message such as 'No price found' and stop the lookup instead of returning a zero-length string.

Which file format should I save the WPS VBA workbook in?

Save as Excel Macro-Enabled Workbook (.xlsm) so the user form and VBA project are retained when the file is reopened.

How do I open the VBA editor in WPS Spreadsheets?

Enable the Developer tab, then click Visual Basic or press Alt+F11. The VBA editor opens in a separate window where you can insert a UserForm and write the lookup code.

Can I use existing Excel VBA code in WPS Office?

WPS Office can run many Excel VBA macros and user forms, but you should test form controls and references on a copy of your workbook because feature support may vary.