Remove single quote VBA function - vba

To simplify, I have two sheets in an Excel workbook.
If I write =sheet2!R3C2 in one of the cells of sheet1, the cell takes the value of the cell R3C2 of sheet2 correctly.
I would like to generalize that for many rows in VBA, so my code is:
row = XX a long
temp = "=sheet2!R" & row - 1 & "C2"
Range("c" & row).Value = temp
But when I use this code, the value of the cell(row,3) is =sheet2!'RXXC2'
How can I remove the single quotes ??

Range("C" & row).Formula = temp
would produce the correct formula in your cell.
What you should consider doing instead of looping is
Range("A1").Formula = "=Sheet2!$B1"
Range("A1").Resize(100, 1).Formula = Range("A1").Formula
The first line inserts a formula =Sheet2!$B1 in cell A1 of your active sheet. The $ dollar sign assures that the column will not be incremented (same applies with numbers)
Then the second line duplicates the formula across 100 rows down from A1 replacing the number after the B column
So now
A1 =Sheet2!B1
A2 =Sheet2!B2
A3 =Sheet2!B3
...
Also, it's a bit unclear what you're trying to actually do so consider another option which is saving the value of formula into another range using the Evaluate() function
Range("c" & row).Value = Evaluate(temp) Or Range("C" & Row).Value = [temp]

try to write Range("c" & row).FormulaR1C1=temp

You want to set the formula, not the value:
Range("c"&row).FormulaR1C1 = temp

Related

Concatenate 2 Columns in VBA

I have 2 columns (column F and G), let's say they both go up to row 10. In row S, I want a concatenate statement: F(G)
I've tried:
SHEET.range("S2:S2" & LastRow).FormulaR1C1 = "=CONCATENATE(RC[-13],""("",RC[-12],"")"")"
and
SHEET.column(18).FormulaR1C1 = "=CONCATENATE(RC[-13],""("",RC[-12],"")"")"
The problem with either is that after the 10th row, the formula keeps running such that every cell goes "()". How do I edit the macro to end when there's no more data in column F and G?
with:
range("S2:S2" & LastRow)
if LastRow is 10 then the string will be S2:S210 as you have the 2 in the second reference. You want to remove that:
range("S2:S" & LastRow)
So:
SHEET.range("S2:S" & LastRow).FormulaR1C1 = "=CONCATENATE(RC[-13],""("",RC[-12],"")"")"

putting a string from a cell into the middle of my index-match VBa script

I am trying to use the index-match formula to reorganize data such that all of the names in column J that have a matching value in column A will be placed in the same spot. I'm going to do this for 5 different columns so that the 5 names on a team will be in the same row as the name of the corresponding client.
My issue is that the index-match formula needs to be able to dynamically shorten or lengthen the size the arrays it uses based on how many clients there are when the VBA script is run.
I can dynamically determine what numbers I need in the formula using COUNTA, but the code will not compile when I try to put it in my formula. My formula is below
Range("B7").Select
ActiveCell.Formula = "=INDEX('test sheet two'!" & Range("J3") & ",MATCH(Sheet1!A5,'test sheet two'!" & Range("J1") & ",0)"
As you can see I need the strings in cells J3 and J1 to be used as the arrays for the index match. J3 = $J$2:$J$2369 and J1 = $A$2:$A$1113
When I run the code it gives me a "Application-Defined or Object-defined error."
You need to use the Range member of worksheet
so use 'test sheet two'!Range("J2:J2369") rather than 'test sheet two'!("J2:J2369").
The following runs
ActiveCell.Formula = _
"=INDEX('test sheet two'!Range(""" & Range("J3") & """) _
,MATCH(Sheet1!A5,'test sheet two'!Range(""" & Range("J1") & """),0))"
Your formula was not including the column criteria for the INDEX Function.
Try:
Range("B7").Select
ActiveCell.Formula = "=INDEX('test sheet two'!" & Range("J3") & "," & _
"MATCH(Sheet1!A5,'test sheet two'!" & Range("J1") & ",0), 1)"
Notice the additional , 1)" on the end of the formula.
Also, you do not have to first Select the cell which you want to enter the formula in, you could just use:
Range("B7").Formula =

Set sum range when range varies

I need to use a sum formula in my VBA script that always starts at cell B10 but, will finish at a different cell (always in column B) depending on results of the previous macro's.
The first cell for sum range will always be cell B10 ... the end of the sum range will always be 3 rows above the cell that formula is populating.
I tried the following but, I get
'Compile error: Argument not optional' error.
ActiveCell.Formula = "=SUM(B10:" & ActiveCell.Offset(-3, 0).Range & "))"
How can I use a varying cell reference in my formula?
As you are only looking for the Row value you can better use that property directly:
ActiveCell.Formula = "=SUM(B10:B" & (ActiveCell.Row - 3) & ")"

Excel - how to increment formula reference along a row

I have really long strings of text saved in roughly 1000 cells down Column A.
I have created the following formula (some VBA included for FindN), placed in cell B1:
=MID($A1,FindN("978",$A1,1),13)
I can copy this formula down Column B just fine. However, I also want to copy this formula across each row, so for example the formulas for the cells across the row should be as follows:
Cell C1: =MID($A1,FindN("978",$A1,2),13)
Cell D1: =MID($A1,FindN("978",$A1,3),13)
Cell E1: =MID($A1,FindN("978",$A1,4),13)
etc...
If I copy the formula in Cell B1 across the row, it will copy across =MID($A1,FindN("978",$A1,1),13) - but I need the "1" to increment by 1 each time.
I think I'd need to adjust the formula slightly, but a bit lost on how to do this...
Any help would be greatly appreciated. Please let me know if I should clarify further.
Use COLUMN() - it gives the column number of the current cell. You can offset this as required.
In this case for your incrementing number use COLUMN() - 1, so that in B you have 1, C; 2 etc.
You need use CELL formula to get current column number. Try something like this:
=MID($A1,FindN("978",$A1,CELL("column";A1)+1),13)
I dont have English Excel and im not sure first argument in CELL forumla is "column"
Try this :
Sub Fill()
With Sheets("Sheet1")
For i = 1 To .Cells(Rows.Count, "A").End(xlUp).Row
.Cells(i, 1).FormulaR1C1 = "=MID($A1,FindN(" & _
Chr(34) & "978" & Chr(34) & _
",$A1," & i - 1 & "),13)"
Next i
End With
End Sub

Excel VBA - Sum function

I'm trying to calculate the sum of my columns (column I). From 1 to the last record in I it has. When I record a macro I get this as output, but this is gibberish to me.
ActiveCell.FormulaR1C1 = "=SUM(R[-11]C:R[-4]C)"
I found another topic and there they said
LastRow = .Range("I" & .rows.Count).End(xlUp).row
Range("I"&LastRow) = "SUM(I1:I...)"
Except in my case, I can't figure how to enter the lastrow of I in it.
All help is welcome :)
There are two ways of referencing a cell - 'R1C1' and 'A1'. The former works like co-ordinates, with a relative number of rows (R) and cells (C).
The other reference style refers to the cell name on the sheet - B6, F67 etc.
Let's say you want to put your Sum() in cell B1 and LastRow has a value of 6:
ActiveSheet.Range("B1") = "=Sum(I1:I" & LastRow & ")"
Would insert the following function in cell B1:
=SUM(I1:I6)