Performing multiple vlookup operation in VBA - vba

I have a logical doubt. Is it possible to return a value to a cell by performing Vlookup operation in 3 sheets, i.e the value to be returned might be in one of the three sheets. If so how can I approach this? I am trying to create a macro using VBA.

if there are no real differences between the worksheets you can use formulas instead of VBA using =IFERROR() and =VLOOKUP()
An example would be:
=IFERROR(VLOOKUP(valueToLookFor,sheet1Range,columnNumber,FALSE),IFERROR(VLOOKUP(valueToLookFor,sheet2Range,columnNumber,FALSE),VLOOKUP(valueToLookFor,sheet3Range,columnNumber,FALSE)))
This would just perform the search on the first sheet, if the value isn't there it will return an error, hence looking in the following sheet etc.
Bear in mind I've written the formula given your Excel is in English with commas (",") as separators, you might need to translate it in you default language and separators.

Related

Find and correct all inconsistent column formulas with VBA

I have an excel workbook with 5 worksheets in each of this worksheets there is a table with values.
I then delete some of the rows (using VBA) depending on user selection (using dropdown list).
After my VBA code deleted all the unnecessary rows Excel states that I have "inconsistent column formulas" which I'd like to resolve with VBA before the user sees it.
Is there any way to do this with VBA?
I've searched Google the whole day now and still found nothing usefull and the only thing I'd have in my mind would be iterating through all Rows and Columns with formulas in it, checking, if the formula contains an error, which would definitely be super slow...
Note: If this counts as duplicate of Find inconsistent formulas in Excel through VBA I'm sorry, but the only answer there doesn't work with tables as data range
If you are trying to reset a formula in a Table, you can use the DataBodyRange.Formula property to reset the formula for the entire column. This addresses one way to get the Inconsistent Calculated Column Formula.
Example of the error was obtained by setting the formula for the column, changing one cell, and then telling Excel not to change the formula column after that edit.
To revert this back to a column formula (and remove the error), you can run VBA that changes the formula for the DataBodyRange.
Code to change back
Sub ResetTableColumnFormula()
Dim listObj As ListObject
For Each listObj In ActiveSheet.ListObjects
listObj.ListColumns("b").DataBodyRange.Formula = "=[#a]"
Next
End Sub
Note that I am being a bit lazy by iterating through all ListObjects instead of using the name of the table to get a reference. This works since there is only a single Table on the Worksheet.
Formulas after that code runs:
Note that this answer is very similar to the answer here.

Trying to use excel formula or VBA

I have two different workbooks. Book 1 and Book 2 both have the same number of columns A through M.
I want to do match the records between two workbook, for example: I have a column A name Birthday, Column B City, Column C Passport Number......., in worksheet 1 & 2. I want to match worksheet 1 Cell A1 from the Range A:A worksheet 2, If the record in column A cell 13 not matching it shows Birthdate not match in N13 Workbook 2, If it does not match with worksheet 1 Cell 13 from the Range B:B worksheet 2 it shows city not MATCH in Column N 13 in workbook 2, and so on till column M.
I am using the formula below but it's not working properly, I don't know what I am missing and what formula should I add in. I have no idea about VBA. But I want to see is it easier to do by using excel formula or vba?
IF(COUNTIF(Target!$A$2:$A$5964,Source!A8)=0,"Birthday",IF(COUNTIF(Target!$B$2:$B$5964,Source!B8)=0,"City",IF(COUNTIF(Target!$C$2:$C$5964,Source!C8)=0,"Country",IF(COUNTIF(Target!$E$2:$E$5964,Source!D8)=0,"Passport Number Mismatch in Target",IF(COUNTIF(Target!$F$2:$F$5964,Source!E8)=0," Travel Date Mismatch in Target",IF(COUNTIF(Target!$G$2:$G$5964,Source!F8)=0,"First Name Mistmatch in Target",IF(COUNTIF(Target!$H$2:$H$5964,Source!G8)=0,"Full Name Mismatch in Target","Match in Target")))))))
Thanks in Advance.
VBA has access to these same worksheet formula functions (e.g. COUNTIF): there really aren't column or matrix functions that VBA has that formulas don't have.
However, VBA lets you write loops (e.g. while, for), it allows if-statements, procedure calls, and many lines of code so your calculations can have more steps and hence be more complex. VBA also lets you have temporary space in the form of arrays (and strings and objects, too) (so you don't necessarily need to use columns for temporary space as one might do with formulas). VBA also allows recursion, which makes some calculations easier (to some definition).
VBA provides an imperative programming model. VBA procedures can read and write any cell of the spreadsheet. Imperative programming, on the other hand, needs to be triggered somehow such as by using a button.
By contrast, the data-flow programming model with formulas will automatically recalculate whenever their input sources change, which is good. But there are some cases it doesn't handle naturally (e.g. recursion).
Another option is to combine VBA with formulas, by writing new formulas that are then implemented in VBA. If you are doing that, the VBA can only return information thru function return values; it cannot otherwise modify the spreadsheet.
So, if you can think of how to do this easier using loops (and arrays) or recursion and maybe with a button to trigger the computation (or by using custom formulas) then VBA might be interesting.

Excel VBA alternative for VLOOKUP when searching the cell value with special characters

What is VLOOKUP alternative when searching the cell value with special characters? Or any macro I could use for alternative.
The formula is in Column E and the look up value is Column D. Apparently the VLOOKUP is only searching the numbers and text. If the value has special characters it does not display the correct info I need in Column E.
Well, I have tested that now, and it seems that every special character works for me.
You should always include 0 (or FALSE) as the fourth argument in the VLOOKUP formula if you need to find the exact match. My guess is that you forgot the zero this time. Even though not VBA, but an alternative for VLOOKUP is =INDEX(MATCH(...,...,0)). You can google it.

Make a List of numbers formatted with commas from multiple cells to one cell

For starters I have limited experience with VBA and was hoping to accomplish this task with built in excel functions, unfortunately without success. Here is the problem:
I have multiple cells with number values in them (lets say 3 cells each with a value of 1000) and I need that converted to one cell with those same multiple values (on top of each other like a list) and be able to have them formatted with commas at thousands places.
I have been successful at putting them in one cell by using an ampersand and the char(10) function along with formatting to wrap text.
However, now I cannot format with commas. Can this be done without VBA? If not some direction for writing a function would be helpful.
Without VBA
=TEXT(A1,"#,##0")&CHAR(10)&TEXT(B1,"#,##0")&CHAR(10)&TEXT(C1,"#,##0")
For example:

How do I select specific cells in VBA?

Basically I need to select specific cells on an excel sheet in vba. For example the code should be able to select a5, a10, a15. I do not want the cells in between them, just the ones I listed. Is there a specific function that will do this? It seems like .Range could only take start cell and ending cell.
You'd use this: Range("A5,A10,A15").Select
To add additional cells, just use more commas.
Alternately, you can utilize the Union method to join multiple range objects into a single Range object.
Note that it is generally not a good idea to select cells in VBA as nearly everything can be done without selection. This is a frequent mistake made by people new to VBA due to generated macros (Record Macro) recreating what you do rather than the result you want.