VBA,Combine .Formula with VBA - excel

I have the following vba code which puts a formula in a cell.
.Range("D" & lastrow + 1).Formula = "=INDEX(Spread!$C:$C, MATCH(1,INDEX((A330 = Spread!$A:$A) * (""Stack"" =Spread!$B:$B),),0))"
How can I change A330 in my formula to vba code like Worksheets("Manager").Range("C2").Value

Just concate it like a normal string using the & operator. Since you are not joining text ,but rather a VBA object, do not wrap quotes around the object.
"=INDEX(Spread!$C:$C, MATCH(1,INDEX((" & Worksheets("Manager").Range("C2").Value & "= Spread!$A:$A) * (""Stack"" =Spread!$B:$B),),0))"

You can also store the data in a variable and use it instead of A330. This way you can always use newly created variable inside your code anytime anywhere in your code
myData = Worksheets("Manager").Range("C2").Value
.Range("D" & lastrow + 1).Formula = "=INDEX(Spread!$C:$C, MATCH(1,INDEX((myData = Spread!$A:$A) * (""Stack"" =Spread!$B:$B),),0))"

Related

Excel VBA cannot populate cells with "=("

I'm trying to dynamically populate a couple of cells in Excel with VBA with some simple formulas.
I need a formula like
=(B1-C2)
In Excel
Which is very simple to code... But I get a application or object defined error '1004'. With this simple loop.
For i = 1 To 3
Range("A" & i).Value = "=("
Next i
Therefore I tried to first declare a variable like
dim tmp as string
tmp = "=("
For i = 1 To 3
Range("A" & i).Value = tmp
Next i
But it did not work.
I have also tried to declare it as variant but with no luck.
Does anyone why this happens and how to fix it? Thanks

Worksheet Function Countifs for resizable range

I have a table in which I count the records through Worksheet.Function.Countif.
It is nice because it counts the rows using .Rows.Count and so I am alwasy ensured if my table changes the size.
It looks like that (subset of the code):
endrow = .Cells(.Rows.Count, 20).End(xlUp).Row
ws1.Cells(6, 34).Formula = "=COUNTIF(" & .Range("U6:U" & endrow).Address & ",U6)"
I wish to write the the worksheet.function formula in the same way as above but for 'Countifs'. In excel, I would type it like that:
=COUNTIFS($U$6:$U$144;U6;$T$6:$T$144$;"<>"&T6)
How to write it in vba, using 'endrow' as in the first demonstarted code, i.e. without '144' as the last row but with '& endrow' ?
I was trying multiple times, but I cannot get it to work :/
I will appreciate any help.
Try this:
ws1.Cells(6, 34).Formula = "=COUNTIFS($U$6:$U$" & endrow & ",U6,$T$6:$T$" & endrow & "," & """" & "<>" & """" & "&T6" & ")"
This formula gets the last row of column A:
=IFERROR(LOOKUP(2,1/(NOT(ISBLANK(A:A))),ROW(A:A)),0)

get the differences between 2 Integer columns into third column

I have to calculate the difference between the values of 2 columns(firstCol and lastCol) and set those differences in a different column(NextColumn). The row count keeps changing, so I have to calculate the rowCount before calculating the difference. I'm trying to write a loop using Range so that the difference can be calculated but it doesn't seem to work.
For i = 3 To lastRow
Range(Cells(3, NextColumn), Cells(lastRow, NextColumn)).FormulaR1C1 = "=Range(Cells(i, firstCol),Cells(i,firstCol)).Value - Range(Cells(i, lastCol),Cells(i,lastCol)).Value"
Next i
Any help would be greatly appreciated!
Thank you
Nick
For any input of "i" into the formula, you need to use " & i & " when using using a formula="", such as:
"=A" & i & "+B" & i
When you change to just a formula that doesn't input a formula to the cell (math happens in VBA), you can ignore the Excel formatting and "" blocking, such as:
= Cells(i,"A").Value + Cells(i,"B").Value
Make sure to use your loop variable where appropriate, so that you would have an outcome in a loop like:
Dim i as Long
For i = 1 to 10
Cells(i,"C").Formula = Cells(i,"A").Value + Cells(i,"B").Value
Next i
Why looping ? Unless I misunderstood the question, something like this should do the trick:
Range("X3:X"& lastrow).Formula = "=C3-D3"
The formula will adjust.
vba needs to be outside the quotes and you do not need the loop:
Range(Cells(3, NextColumn), Cells(lastRow, NextColumn)).Formula = "=" & Cells(3, firstCol).Address(0, 0) & "-" & Cells(3, lastCol).Address(0, 0)
No loop needed. Excel will change the relative references as needed.

Special paste in Excel with VBA

I'm working on Excel with VBA. I need to copy and paste some information from one row to and specific destination. I'm using this code:
''CUSTOM MESSAGE
Sheets("Extract").Range("AI" & sourceRow & "").Copy Destination :=
Sheets("Print").Range("H" & destRow + 7 & "")
The result of this code is:
But I need something like this:
As you can see, I need to change to the next row, before going out of the table.
Any idea?
Can you wrap the text in that cell? Something like this?
Sheets("Extract").Range("AI" & sourceRow).Copy Sheets("Print").Range("H" & destRow + 7).WrapText = True
The format of the Range() is incorrect.
Try:
Sheets("Extract").Range("AI" & sourceRow).Copy Sheets("Print").Range("H" & destRow + 7)

Using value from variable in dynamic hyperlink

I have a summary sheet which I use as an index to access several sheets via a hyperlink. I'm extending it so they link directly to the appropriate row within each sheet.
I'm newish to VBA and not sure on the syntax.
I'm basically trying to use the value from variable j as the row number in the cell reference.
"'!A" is the first part of my cell ref in the code below. I'm then trying to concatenate the string to add j as the row number. Can this be done using the code below? I tried surrounding j with brackets, apostrophe to no avail.
For j = 2 To LastUsedRow
link = "=Hyperlink(""#'" & (ThisWorkbook.Worksheets(i).Name) & "'!A" & j",""" & (ThisWorkbook.Worksheets(i).Name) & """)"
Many thanks
Debug.Print is your friend. This is what you get with your current code (assuming j = 1 and ThisWorkbook.Worksheets(i).Name = "Sheet1"):
=Hyperlink("#'Sheet1'!A1,"Sheet1")
The formula should look like this:
=HYPERLINK("#'Sheet2'!A1","Sheet2")
So...add just add the missing ":
link = "=HYPERLINK(""#'" & (ThisWorkbook.Worksheets(i).Name) & _
"'!A" & j & """,""" & (ThisWorkbook.Worksheets(i).Name) & """)"
The including of double quotes within VBA strings is tricky.
link = "=Hyperlink(""#'" & (ThisWorkbook.Worksheets(i).Name) & "'!A" & j & """,""" & (ThisWorkbook.Worksheets(i).Name) & """)"
will get link as =Hyperlink("#'SheetName'!A2","SheetName")
You need to call on what you want to pull from j, such as: j.Value

Resources