How to use the match Function with two criteria in VBA - excel

I am trying to use the „MATCH“ function in order to get the Row (named RICRow) number of a name which is contained in the “RIC“ column (A). Because there are always two names in the RIC column (e.g.”Tom”) it is necessary to take the column(L) “RIC_FID” into consideration which contains a unique value corresponding to the names (e.g. 295). The VBA code below keeps yielding the error msg: type mismatch
RICRow = Application.WorksheetFunction.Match(RIC & RIC_FID, protokoll.Range("A1:A500") & protokoll.Range("L1:L500"), 0)

this one solved the issue:
RICRow = protokoll.Evaluate("match(""" & RIC & "" & RIC_FID & """, A:A&L:L, 0)")

Related

Add Cell value into URL to find Data vba

I am trying to replace 558899 this Id number from below URL with cell value that is ID_NO = ws.Range("A1").Value.
"15.156.352.352/api/Book/GetOrBookID?Id=558899&ColumnName=PrimaryId"
and i changed it to
"15.156.352.352/api/Book/GetOrBookID?Id=" & ID_NO & "ColumnName=PrimaryId"
But it does not work nay help will be appreciated.
Your question doesn't mention what the bad result is, but I can see one issue in your code:
"15.156.352.352/api/Book/GetOrBookID?Id=" & ID_NO & "ColumnName=PrimaryId"
When this is parsed, it will result in:
"15.156.352.352/api/Book/GetOrBookID?Id=xxxxColumnName=PrimaryId"
But I think you want:
"15.156.352.352/api/Book/GetOrBookID?Id=xxxx&ColumnName=PrimaryId"
So the code you need is:
"15.156.352.352/api/Book/GetOrBookID?Id=" & ID_NO & "&ColumnName=PrimaryId"
Im guessing that the cell value is formatted as a number(long). therefore you could try replacing ID_NO with str(ID_NO) to convert it to a string

Possible faster VBA lookup with wildcards?

I need to perform a quite a lot of lookups with wildcards on the worksheet using a macro (mainly lookup for value & returning the value from another column though with proper adjustment it can be also just looking for a value with wildcard, and some lookups only as checks if the value exists in the dataset). My data can't be sorted and all the lookups are within a loop A or loops within loop A; wildcards are included mostly for condition "string begins with...". I often have to find a value in one row and find corresponding value in row N rows below or above.
I have a working code, but I wonder if it can be done faster. #response to comment about posting it on Code Review (sorry, I cannot comment yet :)) - preparation the whole code to posting would take a bit too much time for me, confidentiality etc, so I prefer to treat it as a general question to be worked on this example.
Example data (I can add more columns, if I need any helper column):
Example Data picture at Imgur
Assume 100 000 rows (max xPagesCount = 1000, typically around 400; all values for certain xPage is in one block). Due to a lot of possible rows with additional data I can't simply find one value and add numbers to the found row to find the other values by their position.
Example lookups to perform while looping through consecutive xPages (so, for each given xPage):
value in row just below row with "RESTRICTIONS:" text
find name (which is always given with height (column C) = 35)
find RSW number (which can be in several rows depending on page content, but always below name)
find all rows starting with the same four digits as RSW, in two formats: DDDD.LLL.DD and DDDD.DDDDD.DD (L letter, D digit) (I use internal loop here)
check if there is a text "MASTER" (or "MASTER " etc.)
find all values between values "DOCUMENTS:" and "OPTIONS:", which quantity can be from 1 to 50 (I use internal loop here)
I was wondering, what is the fastest way to do such lookups?
What I tried:
using a dictionary on all dataset (keys in column A or C with, values
col.D) but as dictionary can't work on wildcards, I had to add ifs
for not finding a key to perform additional Application.Match
lookup... and then realized it mostly worked on these Match lookups
and not sure I even need a dictionary. I also have duplicate values
within a page and dictionary was getting only first value, regardless
their position (for example, several attachments could have value 1).
The main use remained dict.exists("MASTER") but when I removed
dictionary and changed it to IsError(Application.Match(...)) the code
worked slightly faster.
Application.Match in whole range, typical example: Application.Match(xPage & "4???.*", sh.Range("A1:A" & LastRow), 0)
in few places I use If xValue Like "????.???.??" Then construction
I have dictionary lookups with ifs redirecting to Application.Match:
xValue = dict(xPage & "ATH.416")
If dict(xPage & "ATH.416") = "" Then xValue = Application.Match("ATH.*", Sheets(1).Range("D:D"), 0)
What I consider, but not sure it's worth the effort:
altering the code that at the beginning of the iteration I find the first and the last row for xPage, and then each later check is performed in this range
xStartPage = sh.Range("D" & Application.Match(xPage, sh.Range("A1:A" & LastRow), 0))
'or, I guess better:
xStartPage = xEndPage + 1
If xPage = xPagesCount Then
xEndPage = LastRow
Else
xEndPage = sh.Range("D" & Application.Match(xPage + 1, sh.Range("A1:A" & LastRow), 0) - 1)
End If
xValue = sh.Range("D" & Application.Match("4???.*", sh.Range("D" & xStartPage & ":D" & xEndPage), 0)).Value

variable not define is vba formula

my variable uuid does not pick up in the the number.
in the excel it still shows =COUNTIF(AB11:AL11,uuid) but it should be
=COUNTIF(AB11:AL11,1234567)
uuid = Worksheets("Search").Cells(2, 4)
Range("AV2:AV" & lastrows).Formula = "=COUNTIF(AB2:AL2,uuid)"
You cannot use a variable inside a text string. You need to concatenate the text with the variable.
In your case, the text string is a formula. Split that formula text string where the variable needs to go and use the ampersand & sign to put the pieces together.
Range("AV2:AV" & lastrows).Formula = "=COUNTIF(AB2:AL2," & uuid & ")"

If else statement with three conditions using Application.Match to find row number

I'm writing a macro which imports a lot of .csv files from a folder with many subfolders. This is working so far.
Now I want to implement that if a file was already imported it should not be imported a second time. There I get into an issue.
I really tried to find a solution with the search function, but I only could find "If else statement with two conditions" but it didn't get my code working.
I need to write an if else statement with three conditions.
Conditions: Only if the file name (column C) the folder name (column B) and the parent folder name (Column A) are an exact match, the file is NOT to be imported.
For one Condition it is working fine.
This is my code snippet for finding the file name in column C:
' Check if file was already imported should return Row Number --> SearchColumn
If Not IsError(Application.Match(fso.GetBaseName(oFile.name), Columns("C:C"), 0)) Then
SearchColumn = Application.Match(fso.GetBaseName(oFile.name), Columns("C:C"), 0)
End If
The variable SearchColumn tells my where it found the file name, if it is in the list. I want to use this variable to check if in the same row in column B and column A are matching to the names of the file.
I thought it would be something like this:
' Check if file was already imported should return Row Number --> SearchColumn
If Not IsError(Application.Match(fso.GetBaseName(oFile.name), Columns("C:C"), 0)) Then
SearchColumn = Application.Match(fso.GetBaseName(oFile.name), Columns("C:C"), 0)
End If
If Not IsError(Application.Match(fso.GetBaseName(oFile.name), Columns("C:C"), 0)) _
And Not IsError(Application.Match(fso.GetBaseName(oFolder), "B1:B" & SearchColumn, 0)) _
And Not IsError(Application.Match(fso.GetBaseName(folderName), "A1:A" & SearchColumn, 0)) Then
' Do nothing because file was already imported
Else
' import file / copy content
Set wbkCS = Workbooks.Open(oFile.path)
'some more macro code
But somehow it ignores both of the other conditions. Where am I mistaken?
Best TMC
Okay I figured it out by myself. Actually it was pretty simple.
Since the code was working with one condition very well, I just concatenated the conditions to one. I concatenated the string in columns C, B and A in a new column E (--> C & B & A).
Range("E" & LastRow).FormulaR1C1 = fso.GetBaseName(Left(oFolder, InStrRev(oFolder, "\"))) _
& fso.GetBaseName(oFolder) _
& fso.GetBaseName(FileName)
I used this column to look for my concatenated criteria like this:
If Not IsError(Application.Match(criteria, Columns("E:E"), 0)) Then
' Do nothing because file was already imported
Else
'import code
That was it.
Best TMC

Match doesn't work on Specific String name

So I have a spreadsheet like this:
I'm trying to run this formula to get the value from "Total Income" under the -Call Centre header
These are the values from the NAME_INPUT sheet:
B71 = A1:AJ39 D71 = E1:E39
This is the formula which is finding result 0:
=ROUND(INDEX(INDIRECT("'QB IS by class'!" & NAME_INPUT!B71),MATCH("Total Income",INDIRECT("'QB IS by class'!" & NAME_INPUT!D71)),MATCH("-Call Centre",'QB IS by class'!1:1,0))/1000,0)
I know the formula is written correctly, because if I change "Total Income" to "PIEFACE", it'll match and give me the correct value.
Why does the string, "Total Income" cause it to have zero results. I don't see any other fields under E column with the name, "Total Income".
Even if I use, "Total Expense" that will get the correct value under -Call Centre for that row... So why does "Total Income" fail to give me a result?
The field types are all the same.
Thanks guys
Although your data isn't clear, but I think you are doing an Exact Match so you forgot the third parameter 0 for the first MATCH in the formula:
MATCH("Total Income",INDIRECT("'QB IS by class'!" & NAME_INPUT!D71))
The above term should be
MATCH("Total Income",INDIRECT("'QB IS by class'!" & NAME_INPUT!D71), 0)
^^^^
and the whole formula:
=ROUND(INDEX(INDIRECT("'QB IS by class'!" & NAME_INPUT!B71),MATCH("Total Income",INDIRECT("'QB IS by class'!" & NAME_INPUT!D71), 0),MATCH("-Call Centre",'QB IS by class'!1:1,0))/1000,0)
^^^
Try substituting MATCH(CONCATENATE("*"&"Total Income"&"*")) in for MATCH("Total Income"

Resources