Home » excel » vba – If/And statements and lookup in Excel

vba – If/And statements and lookup in Excel

Posted by: admin May 14, 2020 Leave a comment

Questions:

My question is about VBA excel code.

I want to maybe write a macro but i’m not very familiar with coding so I was looking for some guidance.

Basically I have an excel spreadsheet with a table with some fields but no values filled in and then I have a seperate spreadhsheet with a table of values that I’d like to get into the first spreadsheet

For Example this would be a brief breakdown of what I have:

Height: *blank cell to be populated*

length: *blank cell to be populated*

Width: *blank cell to be populated*

I want to set up a macro so that when I type 10 into the first row (note that there are two columns, one for the noun e.g “Height” and one for the actual value.), that it will look up my second worksheet with the table of values and automatically populate the field for the length and the width based on the adjacent values in the calculations table.

I hope this is making sense!

I’ve tried creating a new macro and writing a basic if/and function but the macro won’t run. I’ve enable macro’s but it’s telling me that my code has no defined object. Because I couldn’t get that running, I didn’t look to much into incorporating the lookup command.

I’ve done some research online into basic if/and statements and they don’t seem that difficult, however my table of values has 126 different rows, each one with a height. lenght and width value, so it would be 3 horizontal columns by 126 vertical rows. It seems excessive to me to have to do that many if/and statements.

I’m also finding it difficult to do a lookup of a different worksheet. This table is very big and i’d rather have it on a seperate sheet rather than put it to the far side of the first sheet and run the lookup there (it seems to messy to me)

Sorry If I haven’t explained this very well. Any help is greatly appreciated!

How to&Answers:

I suggest that you do not use VBA. You can achieve this with the VLOOKUP function.

Here are some examples that I have in Sheet2.

H   L   W
1   2   3
5   10  12
10  11  12
15  15  15

For convenience, I define a name, myHLW, which is defined as sheet2!$a$1 to sheet2!$c$5.

In Sheet1, I type Height Width Length in a1, b1, and c1.

I type 10 in a2. I then entered this formula in b2: =VLOOKUP($A2,myHLW,2). This tells Excel to (a) look up the value in a2 (currently 10). (b) Look in the named region, myHLW. (c) Put whatever is in column 2 in the found row. Excel fills in 11 for me.

I then entered this formula in c2: =VLOOKUP($A2,myHLW,3). This tells Excel to look up the value in a2 (currently 10), using the named region, myHLW, and place whatever is in column 3 in the found row. Excel fills in 12 for me.

I copied the formulas down, entering different heights. It looks like this:

Height  Length  Width
10          11  12
5           10  12
1           2   3
3.14159     2   3

Note that the heights in Sheet2 must be in ascending order. Note also that for a height that is not found (3.14159), it uses the height for 1 (the largest value that was not exceeded.)

You may also add a fourth argument, for example =VLOOKUP($A2,myHLW,3,FALSE). If the fourth argument is false, Excel will require an exact match. If you had pdone this for VLOOKUP, Excel would have placed #N/A if there were no match.