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.
Related
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
I am using variable ranges, and want to insert those ranges into a formula that can be saved in a document that does not use VBA.
Do I have to use a with statement?
My If Statement works until I try and insert a relative range. I searched and didn't see much on this, so this issue is probably so simply I am an idiot.
The program will be doing things all over the worksheet, so relative coding is needed. The number of items in the range changes with each iteration. I will be using an end range integer [myRange] for the number instead of 10, but I wanted to simplify what I am struggling with into the smallest steps.
Dim Test As String
Test = "=IF(COUNTIF(" & ActiveCell.Offset(1, 0).Address & ":" & ActiveCell.Offset(10, 0).Address & "" _
& ")," & """Not Complete""" & "," & "" & """"")"
ActiveCell.Formula = Test
Desired output in target cell:
"=IF(COUNTIF($J$3:$J$12),"Not Complete","")"
As mentioned in comments, your COUNTIF statement is currently invalid. What are you counting in the range J3:J12? This needs to be added (in place of "CRITERIA" in below solution) before your equation will be valid
Test = "=IF(COUNTIF(" _
& ActiveCell.Offset(1).Resize(10).Address(True, True) _
& ",""CRITERIA""), ""Not Complete"", """")"
If you want your output to actually have the quotes you can output
Chr(34) & Test & Chr(34) which seems cleaner than wrapping the whole string in quotes from a readability point of view IMO
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
Not sure how to word this well but how do for loops work in range names? what's the correct convention? E.g. I understand how the following works:
For i = 1 to 3
Sheets("1").Range("A1:B" & i).......
Next i
But if i want it to work for the the A column as well - what's the correct convention? Can't seem to find one that works
e.g. something like below
For i = 1 to 3
Sheets("1").Range("A" & i ":B" & i).......
Next i
Clarification
Sorry - I should be clarify my question
I'm not really looking for a loop, I just want to include a defined integer i column A
e.g.
Sheets("1").Range("A" & i :B3")
if that makes sense?
Probably cleener to do a range resize and enter the value in a single shot:
i = 3
Sheets("1").Range("A1").Resize(i, 2).Value = "Test"
You are missing one & in the line Sheets("1").Range("A" & i ":B" & i) after the first i. Please use this: Sheets("1").Range("A" & i & ":B" & i).Value = "Test".
------------ Old answer ------------------
If you want to loop through all cells in Range("A1:B3") you could use this:
For each cell in Sheets("1").Range("A1:B3")
cell.Value = "Test"
Next cell
This loop will progress like this: A1,B1,A2,B2,A3,B3. Hope this helps.
Probably you should switch from Range("A1:B2") to Range(Cells(1,1),Cells(2,2))
Just to clarify in VBA one has strings which are delimited by double quote character, e.g.
"hello"
"world"
To join strings together one uses an ampersand, so
"hello" & " " & "world"
Joining strings is technically known as string concatenation.
In the light of your question it is possible to join the evaluation of variables so if i = 1 then
"hello" & i & "world"
outputs hello1world.
The Range method returns a range that is either a single cell Range("A1") or multiple cells Range("A1:B1").
Simply start and end strings with double quotes and use ampersands between components of the expression so the answer for you is
Sheets("1").Range("A" & i & ":B3")
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".