How do I enter a formula in VBA? [duplicate] - excel

This question already has an answer here:
Excel VBA Formula German/French/Italian/Russian/Dutch/Foreign Function
(1 answer)
Closed 3 years ago.
I'm making a bot that erases certain columns and fills them in with new data. By erasing the data, the formula becomes
=IFERROR(INDEX('AP query'!O:O,MATCH(Findings!#REF!,'AP query'!L:L,0)),"Non PO")
So I tried making a macro and filling the cell with the formula but it says there's an error. Is there a fix or another way to enter the formula into the the cell using macros?
Range("B3").Formula = "=IFERROR(INDEX('AP query'!O:O,MATCH(Findings!C3,'AP query'!L:L,0)),"Non PO")"
srcWorkbook.Worksheets("Findings").Range("B3").Copy
srcWorkbook.Worksheets("Findings").Range("B3:B" & LR).PasteSpecial xlPasteFormulas

When you wish to use a string in a formula =IF(A1="Hi",... in VBA, just "double up" the quotes:
Range("B3").Formula = "=IFERROR(INDEX('AP query'!O:O,MATCH(Findings!C3,'AP query'!L:L,0)),""Non PO"")"
Because, as VBA compiles that line, it'll hit the first quote at "Non Po" and think that's the end of the line. So (...trying to visualize it), it'd be reading that line like your formula is:
=IFERROR(INDEX('AP query'!O:O,MATCH(Findings!C3,'AP query'!L:L,0)),
Tangiental tip - sometimes when I hit similar issues, even just pasting the code here on SO, and formatting it as code, helps because as you can see in your post, the Non PO is a different color, but all formulas (strings maybe?) should be the same (all red, as in the answer I posted above).

Related

How to add running count to excel cell, which already has text in it?

Would anyone have advise on how to add running count to excel cells, which already have text in them, and without removing the said text.
Few screenshots will follow to clarify my issue.
I have received a ton of sheets which have a test case name as a text/string in the C column and they are missing a running count from the beginning of the cell (not sure if the "running count" is the correct term).
In the first screenshot you can see how I need them to be, as well as in the couple first rows of the second screenshot (column C). So the original text could be for example "Purchase order" and I want it to be "1. Purchase order" and the next cell would be "2. Purchase order" etc.
Screenshot 1.
Screenshot 2.
I imagine there isn't an already existing function in excel which would solve my problem. I have played around with macros and VBA some years ago, but don't have any clue from on top of my head how to solve this.
Perhaps a macro that would go through each cell from the selected column one by one, cut the existing text, add a variable number to the cell and then copy the cut text back there after the variable, then add +1 to the variable before moving to the next cell?
So I somewhat understand the logic how it could be done, but don't have any memory how the syntax and the operators etc. work in VBA.
Thank you in advance.
Not exactly sure what you want from the images, but you can do things like this:
If cell A1 has 942
and cell B1 has "slices of bread"
Then C1 can have the result "942 slices of bread":
=A1&" "&B1
Not sure neither if running count is the right term, but you can do it with a helper column and an easy formula:
=COUNTIF($C$2:C2;C2)&". "&C2
Then you can copy/paste as values and delete helper column

Prevent # signs in formulas [duplicate]

This question already has an answer here:
Excel VBA - How to add dynamic array formula
(1 answer)
Closed 2 years ago.
I was playing around with Excel to try and help out a friend with a problem, and wrote the following macro
Sub test()
Worksheets("Sheet3").Cells(1, 4) = "=SUM(A1:A2)"
End Sub
The idea was to try and figure out if I could create a macro that writes formulas in a specific cell (which we know we could) but also be able to change the range of the formula.
For example, if instead of just 2 values to sum up, I had 4 values, we want the macro to the able to count all the values and then set a range for all the formulas.
The problem here is that when I write the piece of code shown above, for some reason in the cell it appears " =#SUM(A1:A2) ". I have no idea what the "#" symbol is supposed to do nor why it is showing up. As a result though, I get a "name?" error and the function doesn't work. However, if I manually delete the "#" symbol, it works perfectly.
Can anyone explain why the "#" symbol is showing up and how not make it show up ?
This should work :)
As the .Formula is made for excel to recognize that you want to print a formula.
Worksheets("Sheet3").Cells(1, 4).Formula = "=SUM(A1:A2)"

An "IF" Excel formula that meets criteria of above and below cells

I'm designing a poker training spreadsheet and I've been trying to work out a formula that highlights each row as you progress through the questions with a "-->" arrow.
Here is a screenshot:
My goal is to insert a "-->" on a row where the previous question has been answered (either "yes" or "no"), but also to instead insert "" if another question in the column is currently highlighted with a "-->" - this way it should smoothly run continuously and no two rows should be highlighted at one time.
The first question was simple enough: I used
=IF(AND(N2="", G3<>"-->"), "-->", "")
I tried to use the following formula on the remaining rows but it didn't work for some reason
=IF(AND(N2="Yes", (COUNTIF(G4:$G$49, "<>"%"-->")>0)), "-->", " ")
This was initially a test to get the functionality working if each answer was correct, which I would then change to 'if the answer is "yes" or "no".
If I got you correctly,
this should do the trick:
=IF(COUNTIF($M$2:$M$20,"<>"&"")+2=ROW(),"-->","")

Excel - Refer to same cell above for two spaces, increment cell reference by 1, repeat pattern [duplicate]

This question already has an answer here:
Drag a formula down with Row changes by another interval other than one
(1 answer)
Closed 5 years ago.
This is a weird incrementing problem, and I've tried switching to R1C1 cell referencing but Excel doesn't recognize formulas to refer to those cells so I'm back.
So say I've got cell C2800, and I need this to be =C1109. Next, I need cell C2801 to also =C1109. I then need this pattern to repeat as I drag. Something like this
=C1109
=C1109
=C1110
=C1110
=C1111
=C1111
...
I've tried the drag down straight but excel thinks this is a flat cell incrementer and simply adds 1 each movement. So essentially I need a formula for a cell refer to a position 1691 spots directly above, and then 1692 spots directly above, then 1691, then 1692, continue and repeat. Let me know if any of you know how to make this feasible
Use INDEX():
=INDEX(C:C,INT((ROW(1:1)-1)/2)+1109)
Where /2 is the repetition wanted and the +1109 is the starting row.

Excel - How to leave cell blank instead of 0? [duplicate]

This question already has answers here:
Pulling data from big excel datatable with incremental column in Vlookup or IndexMatch without zeros
(2 answers)
Closed 6 years ago.
I'm not using a numerical formula. All I have is a simple reference to another cell.
=(Sheet1!D8)
If that cell is blank, I want to have the cell on this page blank too, not a 0. How do I do that? I want it to appear empty to anyone, not just me, so I don't think simply editing excel settings would work? None of my google search results, including MS office site, have been helpful.
My suggestion:
=IF(ISBLANK(Sheet1!D8),"",Sheet1!D8)
You can force a formula to evaluate as text instead of as a number by adding '& ""' to the end like below, but if you are dealing with numbers this isn't great.
=(Sheet1!D8)&""
See this answer for other ideas.

Resources