access to excel vba double quotation issue - excel

am exporting data from access to excel using vba , and i want to excel to receive these lines:-
=COUNTIF('Sheet1'!G2:G68,"1/1")
=COUNTIF('Sheet1'!G2:G68,"1/2")
etc...
my problem is that i can't add these double quotations right,i know that to print this symbol i should put it twice like this "" but still no luck,am getting all kinds of error,can you please modify my code:=
For r = 6 To 16
.Range("B" & r).Value = "=COUNTIF('Sheet1'!C1:C67,1/" & r - 5 & ")"
Next
thanks

If you wish to embed double quotes into the string you are building you can either do it by (as you mentioned) using two pairs of double quotes to 'escape' the double quote you are attempting to add, as follows:
"=COUNTIF('Sheet1'!C1:C67,""1/" & r - 5 & """)"
...which will return:
=COUNTIF('Sheet1'!C1:C67,"1/-5")
An important point to remember is that in order to properly escape the double quote you are inserting, you dont count the double quotes that are defining the string itself as one of the two pairs; That is why in the above example there are 4 pairs at the end:
""")"
The outer two define the string itself; the inner two represent the escaped double quote character.
I always found this form somewhat confusing, and would often opt to instead insert the double quotes by using the chr function, passing in the ascii value for the double quotes character, which is 34;
"=COUNTIF('Sheet1'!C1:C67," & chr(34) & "1/" & r - 5 & chr(34) & ")"
...which will return the same result as the first example:
=COUNTIF('Sheet1'!C1:C67,"1/-5")
Hope this helps,
nim

Related

VB Net Quotes in a string

I need quotes in a string in VB Net.
c = C:\test.doc
How can I realize that the string looks like:
PrintFile /File="C:\test.doc" /OutputFile="C:\test.pdf"
online I read that I have to double the quotes...
but this doesn't work:
" PrintFile /File=""" & c & """ /OutputFile=""" & pfadFertig & pdfName & ".pdf"""
How can I get c and pfadFertig & pdfName & .pdf appear in quotes?
You do indeed have to double the quotes. This is a great example of why you should use String.Format or, even better, string interpolation. Using concatenation operators (&) already makes your code harder to read but with all the extra quotes, it's harder still. Do this:
Dim str = $"PrintFile /File=""{c}"" /OutputFile=""{pfadFertig}{pdfName}.pdf"""
It's obviously much easier to read.
Also I suspect that pfadFertig is a folder path, in which case you should be using Path.Combine to create the file path:
Dim str = $"PrintFile /File=""{c}"" /OutputFile=""{Path.Combine(pfadFertig, pdfName)}.pdf"""
Path.Combine will ensure the correct number of slashes regardless of what trailing or leading slashes are included in the inputs, so you can never make a mistake. In this case, I could probably excuse one concatenation operator as it may seem more natural:
Dim str = $"PrintFile /File=""{c}"" /OutputFile=""{Path.Combine(pfadFertig, pdfName & ".pdf")}"""

VB.NET Compare two string for full or partial match

How do you programmatically Compare two string for full or partial match.
I may have a string called "ItemName" like "003.00.112.0" and a string I am trying to compare it to the string named "ItemNameToFind" like "001...**.*" where " * " is meant to be an unknown blank spot.
What I have figured out so far is that it's some sort of an if statement that needs to correctly compare the strings.
If ItemName = ItemNameToFind Then
MsgBox("Item " & ItemName & " was found based on " & ItemNameToFind)
End If
In the example above it would return that it's not a match because of the first three symbols in the string.
Could, someone, please, help with the code to make that happen correctly as I explained?
In VB.NET you can use the Like operator:
Dim ItemName = "001.00.112.0"
Dim ItemNameToFind = "001.??.???.?"
If ItemName Like ItemNameToFind Then
Console.Write("Item " & ItemName & " was found based on " & ItemNameToFind)
Else
Console.Write("not found")
End If
Note that i have replaced your * with ? since that means "Any single character".
? Any single character
* Zero or more characters
# Any single digit (0–9)
[charlist] Any single character in charlist
[!charlist] Any single character not in charlist

What is the correct syntax for grouping rows

Syntax error using the following code.
Both variables LR1 and LR2 were declared as integers:
sh.Rows("& LR1+1 &":"& LR2-1 &").Group 'Syntax error
No syntax error if hardcoded numbers are used:
sh.Rows("27:29").Group
If your question is how strings and concatenating work:
The & forces string concatenation which means it will take strings or non-string types (but not all types like arrays) like your integer/long variables and combine them into a string.
In your code you have two strings and a character outside the string. "& LR1+1 &":"& LR2-1 &"
String 1: "& LR1+1 &" This is a valid string but Rows can't parse it.
Character: : This is causing the syntax error
String 2: "& LR2-1 &" This is a valid string but Rows can't parse it.
This is throwing a syntax error because : is not a valid character and isn't in a string.
an & needs to be outside the string otherwise it is just a part of the string.
"&" will not concatenate.
What BigBen did in his comment is take your variables (which you have inside a string and as such are not variables they are just characters in a string that happen to look like your variables) and concatenate them into a string that Rows can work with.
So LR1 + 1 & ":" & LR2 - 1 Is taking the Value in LR1 and adding 1, then combining that number with ":", then subtracting 1 from LR2 and combining that into the string as well.
if LR1 = 10 and LR2 = 20 your final output will be:
"11:19"
You can check what your string will look like with debug.print
Variables don't belong inside quotes.
sh.Rows(LR + 1 & ":" & LR2 - 1)
Also, use Long instead of Integer.

How to use .numberformat in VBA with variable of type double as comperator

Dear all i want to format my cell based on the Cell value. There are 3 possible Conditions:
Cell is larger than 0.05 -> the Value should stay the same but be fromated to 0.0
Cell is smaller than a variable (called Threshold) -> the Value should be replaced with a String "a.C."
Cell is Zero -> the Value should be replaced with a Dash"
I figured out how to do this without a vavriable in here. How to add a text string .numberformat in vba is discussed here!
This:
.NumberFormat = "[=0]---;[<0.05] ""a.C."";0.0 "
Does work, while this:
Dim threshold as Double
threshold = 0.05
.NumberFormat = "[=0]---;[<threshold] ""a.C."";0.0 "
Does not work. I guess i nedd to either pass a special variable typoe to the "<" comperator or i need to escape the variable somehow?
A hint would be really appreciated!
Thank you in advance
That's because threshold is part of your number format string. Basically everything that is within quotes is not touched by VBA - except for the double double quotes that tells VBA to use a quote character within the string rather than end the string.
To put the content of the variable into a string, use string concatenation. This is done in VBA using the "&" operator:
.NumberFormat = "[=0]---;[<" & threshold & "] ""a.C."";0.0 "
However, as Double can be a little bit nasty because of rounding, it's maybe better to use the format command to output it in the wanted form (eg 2 digits after the decimal). And it's usually a good idea to use an intermediate variable that you can easily check with the debugger:
Dim myNumberFormat as string
myNumberFormat = "[=0]---;[<" _
& format(threshold, "0.00") _
& "] ""a.C."";0.0 "
.NumberFormat = myNumberFormat

VBA - surround data with double quotes

I'm trying to prepare my data to be inserted in a SQL database, so certain pieces need to be surrounded by quotes.
For Each r In Range("A2", last_data)
r.Value2 = """ & r.Value2 & """
Next r
This just populates everything with 'r.value2'. Using a single quote makes it only appear on the right side of the word.
Essentially, the sheet needs to be modified so every word is surrounded by double quotes. I had a custom code in Excel that surrounded everything with quotes but when VBA reads the cells it ignores the custom formatting.
I think you need to escape the single, double quote within the ... er... quotes:
For Each r In Range("A2", last_data)
r.Value2 = """" & r.Value2 & """"
Next r
The fact that the escape character is, unfortunately, yet another double quote character, makes things a bit confusing.
So for each double quote that you want to add, you have an escape character ("), followed by the actual double quote that you want to add. These are then surrounded by two more double quotes, denoting that it is a string value.
I'm sorry if I haven't explained this well, but I hope you get the idea.
Although, as #Comintern suggests in comments, there is most likely a better way of doing what you want.

Resources