variable not define is vba formula - excel

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 & ")"

Related

Type mismatch in VBA FormulaR1C1 that removes values past "-" sign [duplicate]

This question already has answers here:
How do I put double quotes in a string in vba?
(5 answers)
Closed 11 months ago.
I have a simple macro that moves down to the last filled cell, then moves to the right and is supposed to put the following formula in there: "=LEFT(RC[-2], SEARCH(" - ",RC[-2])-1)", that takes the value from two cells to the left and removes all characters after the "-".
Range("B1").End(xlDown).Select
Selection.Offset(0, 1).Select
Selection.FormulaR1C1 = "=LEFT(RC[-2], SEARCH(" - ",RC[-2])-1)"
The problem is that when I run the code I get Type Mismatch error. The cell that the code goes into is blank before the code runs.
VBA uses double quotes (") to demark string literals. For example:
const saying = "you look nice today"
The problem arises when you want to have a double quote inside a string literal as follows:
const saying = "you look "nice" today"
In this case, the VBA interpreter understants the value of the saying constant to be "you look " as if you had written:
const saying = "you look "
But then it gets to the word 'nice' and it is confused because it is not syntactically valid. There are a couple of ways to get a double quote into a string literal in VBA. The first way is to use string concatenation and the chr() function. The chr() function returns a character based on it numeric value from the ASCII table. Every character has a numeric value that underlies it. "A" is number 65, "B" is number 66 and so on, The ASCII number the double quote character is 34. So a statement of:
debug.print chr(65) & chr(66) & chr(34)
would display the following
AB"
So you could wirte:
debug.print "you look " & chr(34) & "nice" & chr(34) & " today"
and it would display:
you look "nice" today
But I think you will agree with me that this approach seems very onerous, so there is a shortcut when trying to put a double quote into a string literal. It seems a bit strange, but you need to put two double quotes instead of one. So the following code:
debug.print "you look ""nice"" today"
will print:
you look "nice" today
I hope this explanation helps.

CStr does not handle 3 decimals figures with comma

I have the following issue.
When I try to do :
cell.value=CStr(cell.value)
it works with numbers like 6,91.
But when I try with numbers like 6,911 I get 6911 in return when I just want 6,911 instead.
I'm using commas because I'm in Europe, I guess maybe VBA mixes it up with the American way of writing thousands with a comma.
Indeed, here I only want a decimal with 3 figures after the comma
This does not what you expect it to do
cell.value=CStr(cell.value)
Here CStr(cell.value) will turn it into a String but if you write a string into a cell that looks like a number Excel "thinks" and turns it back into a number. Here comes the confusion.
If you want to format that cell as text use
Cell.NumberFormat = "#"
Cell.Value = Format$(cell.Value, "0.000")
or use Cell.Value = "'" & Format$(cell.Value, "0.000")

How to use the match Function with two criteria in VBA

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)")

Extract text from a long string of text

I need to extract the following portion CDA-CUP-PF from the following string of
MECH~CDA-CUP-PF~1 - CUP0915.2XL - Copper Reducer (P)
text
AddFormula TopLeft.Offset(1, 3).Resize(RowCount, 1), "=IFERROR(RIGHT(AA" & Row & ",FIND(""~"",AA" & Row & ")-1,FIND(""^"",AA" & Row & ")+1-FIND(""-"",AA" & Row & ")),"""")"
This is what I see right now: MECH^CHU
I need to see this: CDA-CUP-PF
I need to use something like the VBA code above.
Assuming your pattern is isolating the text in between ~ a formula solution is:
=MID(A1,FIND("~",A1)+2,FIND("~",A1,FIND("~",A1)+1)-FIND("~",A1)-3)
A VBA - UDF solution would look something like this
Public Function Isolate(x As Range)
Dim xString: xString = Split(x, "~")
Isolate = xString(1)
End Function
This formula will do what you want: =TRIM(MID(SUBSTITUTE(A1,"~",REPT(" ",255)),255,255))
It works by replacing ~ with 255 spaces, then it carves out 255 characters from 255 characters in (Which guarantees we get what you want) then it trims off the spare spaces.
If you want the other parts, use left or right instead of mid.
The UDF is a much better option though especially as you are doing this from code already.

Excel Formula with concatenate

I am trying to generate a customer number using the first three letters of the customers last name, the first name initial and middle initial, followed by the last four of their phone number. How would I do this? All I need is the formula.
First_Name Middle_Initial Last_Name Street_Address City State Zip Phone
Nathaniel E. Conn 6196 View Ct Lancing TN 37770 567-273-3956
Something like this (assuming a table with [structured-references], fill in the actual cell names if not):
=LEFT([LastName] & "---", 3)
& LEFT([FirstName] & "-", 1)
& LEFT([MiddleInitial] & "-", 1)
& RIGHT([PhoneNumber] & "----", 4)
I have used dashes ("-") to fill in any spaces where the field might be smaller than the number of characters you need from it. You can change them to any fill character that suits you.
Well, it depends on if each piece of data has its own column, looks like it does.
You can use the left/right functions to parse the data out of your columns.
=CONCATENATE(RIGHT(C1,3) & LEFT(A1,1) & LEFT(B3,1) & RIGHT(H1,4))
I would do:
=MID(CELL_LAST_NAME;1;3)&MID(CELL_FIRST_NAME;1;1)&MID(CELL_MIDDLE_NAME;1;1)&MID(CELL_PHONE;LEN(CELL_PHONE)-3;4)

Resources