Excel copy contents from multiple cells adding quotations - excel

I'm trying to combine contents from multiple cells in excel. Here is an example of what i'm wanting to do:
(Cell1): John Doe
(Cell2): 5950
(Cell3): Autofill with "John Doe" XXXXXX5950
I know i can combine the cells using the formula =&[Cell1]&" XXXXXX"&[Cell2]
But I want it to put the " " on each side of the name, but the code won't except it since part of using the formula is using the " symbol. Any help on this?

Use two double-quotes inside a string to put a double-quote:
=""""&[Cell1]&""" XXXXXX"&[Cell2]

You can escape the " character in VBA by using double quotes.
Range("A1").Formula = """" & Range("B1").Value & """ XXXXXX" & Range("C1").Value
Or you can also use ASCII character 34 as well.
Range("A1").Formula = Chr(34) & Range("B1").Value & Chr(34) & " XXXXX" & Range("C2").Value

Related

Inserting semicolon between two quotation marks in VBA

I am trying to insert a formula automatically within a certain range but somehow I am getting a runtime error 1004. Can someone please assist ?:
The Original in excel:
=#IF(ISNA(MATCH($B6;StepList;0));"";IF(#INDEX(StepIdTable;MATCH($B6;StepList;0);COLUMN())="";"";INDEX(StepIdTable;MATCH($B6;StepList;0);COLUMN())))
My Code:
Dim sMyString As String
sMyString = Chr(34) & Chr(34) & Chr(59) & Chr(34) & Chr(34)
.Range("C6:C25").Formula = "=#IF(ISNA(MATCH($B6,StepList,0)),"""",IF(#INDEX(StepIdTable,MATCH($B6,StepList,0),COLUMN())=" & sMyString & ",INDEX(StepIdTable,MATCH($B6,StepList,0),COLUMN())))"
The problem is "";"". I have tried everything to make this work
Does anyone have an idea ?
Not sure if this is the answer because I'm in a different locale and can't test it directly, but if all your other delimiters are commas, shouldn't the one in sMyString be as well?
sMyString = Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34)
where 44 is the code for a comma.
On the other hand, if they are all supposed to be semicolons, then the formula should have been
Range("C6:C25").Formula = "=#IF(ISNA(MATCH($B6;StepList;0));"""";IF(#INDEX(StepIdTable;MATCH($B6;StepList;0);COLUMN())=" & sMyString & ";INDEX(StepIdTable;MATCH($B6;StepList;0);COLUMN())))"
But when using Formula, you have to enter the formula string using commas as delimiters regardless of locale. However in your case, if you want to standardise on semicolons both in VBA and on your sheet, you could try FormulaLocal:
Range("C6:C25").FormulaLocal = "=#IF(ISNA(MATCH($B6;StepList;0));"""";IF(#INDEX(StepIdTable;MATCH($B6;StepList;0);COLUMN())=" & sMyString & ";INDEX(StepIdTable;MATCH($B6;StepList;0);COLUMN())))"
see this reference
BTW I find it helpful to put the whole formula string into a debug.print statement to see how it looks.
It's all about the number of double quotes. Try one of this approaches:
Sub test1()
sMyString = """"""";"""""""
frm = "=IF(F13=" & sMyString & ",1,0)" 'so
frm = "=IF(F13="""""";"""""",1,0)" 'or so
Debug.Print frm '=IF(F13=""";""",1,0) within the cell
Range("F13").Formula = frm
End Sub
Explanation: since the escape of double quotes is their doubling, each conversion of a string containing double quotes adds or subtracts (depending on the direction of the conversion) these pairs, resembling a cascading effect

Inserting Multiple Double Quotes In A Cell Using VBA

I have the following formula that I want to insert into a cell but it has multiple double quotes and I can't seem to make it work. There is plenty of solutions on the web but most have 1 or 2 sets of double quotes. I tried double quotes on all double quotes but it gives me a runtime error 438 object doesn't support this property
Here is the formula that I want to insert:
="Closing Costs" & " Current Buffer" & " " &TEXT('Closing Costs'!D32,"$ 0")
Here is the code that is not working:
ActiveSheet.Range("F18").Value = "=""Closing Costs"" & ""
Current Buffer"" & "" "" &TEXT('Closing Costs'!D32,""$ 0"")"
The following should work (all on one line):
ActiveSheet.Range("F18").Value = "=""Closing Costs"" & "" Current Buffer"" & "" "" &TEXT('Closing Costs'!D32,""$ 0"")"
It looks the same as your sample - but perhaps looks here are deceptive.
Using the chr() command can be helpful in these scenarios. chr(34) returns a double quote character, so the below should do the trick.
ActiveSheet.Range("F18").Value = "=" & chr(34) & "Closing Costs Current Buffer" & chr(34) & " " & chr(34) & TEXT('Closing Costs'!D32," & chr(34) & "$ 0" & chr(34) & ")"
It’s a little bit longer, but I find using chr(34) to be a little easy to determine quotation placement.

string text with blank cells

I am trying to string together text from cells across by row when some of the cells are blank. I also want to have commas separating the text from each cell but cannot figure out how to only have commas put in when there is data in a cell.
I have tried using variations of concatenate and stringing the text using A1&B1&C1 but nothing is returning the data in the format I want.
Is there a combination of nested formulas I can use that will return the data in the format I want?
Here's a VBA solution I had before the newer versions:
Public Function JOINCELLS(ByVal ref As Range) As String
Dim c As Range
For Each c In ref
If Len(c) > 0 Then
JOINCELLS = JOINCELLS & c & ", "
End If
Next
JOINCELLS = Left(JOINCELLS, Len(JOINCELLS) - 2)
End Function
Then, it's just =JOINCELLS(A1:D1).
The formula below could do the trick. Concatenate the val The trim would remove spaces from the ends, and the substitute would swap the space for the comma(space).
=SUBSTITUTE(TRIM(A1 & " " & B1 & " " & C1 & " " & D1), " ", ", ")
Edit from the comments below - you could update the spaces between the quotes to be any character that does not appear in your cells. You would just have to the substitute formula to match. If you don't use the | character for example:
=SUBSTITUTE(TRIM(A1 & "|" & B1 & "|" & C1 & "|" & D1), "|", ", ")

range.value = [combination string and cell reference]

I have a paragraph which is included in a spreadsheet. I also have a cell reference within the paragraph. I can format the cell reference in the spreadsheet using the '&' operator, but I need to format it with VBA.
This is what I have inside of my subroutine:
Range("A53").Value = "'=Prices quoted are firm for '&Q3&' days from date of quotation for delivery within 6 months, unless specifically'"
I've tried several different variations of this with no luck. Does anyone know where I'm going wrong?
You'll want to use .Formula instead of .Value on the range:
Range("A53").Value = """=Prices quoted are firm for "" & Q3 & "" days from date of quotation for delivery within 6 months, unless specifically"""
Also, double quotes ("") when already inside quotes turn into a single quotes. That can get kind of nasty looking, so you could also use chr(34) like:
Range("A53").Value = chr(34) & "=Prices quoted are firm for " & chr(34) & " Q3 & " & chr(34) & " days from date of quotation for delivery within 6 months, unless specifically" & chr(34)
Which, in this case is probably even nastier looking so I'm not sure why I mentioned it.

How to embed variable into a cell formula

Here is my code:
For i = Z To lastRow
Range("$A" & i).Select
ActiveCell.FormulaRC1C = "='Sheet1'!$A" & i
etc, etc.
The last line is having a problem (obviously) because I can't seem to figure out the quotes needed. How do I format the double quotes? Is there a better way to say this?
I don't think the quotes are the problem (they look OK). You're using the FormulaR1C1 method, which expects the address to be in R1C1 notation (e.g. R3C4), whereas you're supplying the address in e.g. C4.
Try using .Formula
There's no readable way to insert quote marks into code that generates a string. You can try triple-quotes - """ - and good luck with your debugging.
I tend to use the chr() function, with the knowledge that character 34 is the double-quote:
strQ = "He said: " & chr(34) & "It's a dead parrot" & chr(34)
strQ = strQ & ", and explained: " & "It has ceased to be" & chr(34) & "."
Debug.Print strQ
Will concatenate this string:
He said: "It's a dead parrot", and explained: "It has ceased to be".

Resources