I asked a question yesterday on how to use VBA to display formulas within cells, which I now can use, except for my attempt at a subtraction.
I have my code here, is anyone able to point me in the right direction to get this to work.
With Worksheets(c10)
.Cells(NewEngineRowNumber + 3, 2).Formula = "=Sum((" & .Range(.Cells(NewEngineRowNumber + 6, 2)).Address(0, 0) & ") - (" & .Range(.Cells(NewEngineRowNumber + 2, 2)).Address(0, 0) & "))"
End With
I belived this to be the most likly method after a few different attempt to make it work.
To produce something like in Cells(NewEngineRowNumber+3,2) in the formula bar to display =Cells(BX) - Cells(BY), where X = NewEngineRowNumber +6 and Y = NewEngineRowNumber +2
Any help in helping me understand how to edit this to make it work would be much appreciated.
Thanks
Don't try doing all in one code line. Do separating it in multiple code lines is better for debugging:
With Worksheets(c10)
sAddress1 = .Cells(NewEngineRowNumber + 6, 2).Address(0, 0)
sAddress2 = .Cells(NewEngineRowNumber + 2, 2).Address(0, 0)
sFormula = "=Sum(" & sAddress1 & "-" & sAddress2 & ")"
.Cells(NewEngineRowNumber + 3, 2).Formula = sFormula
End With
But why are you thinking the SUM is necessary at all?
sFormula = "=" & sAddress1 & "-" & sAddress2
should also work.
Related
This question might have been answered in other questions but i can't find the correct syntax to use, it is just not working for me...
So i'm trying to put formula in a range of cells with a loop in vba, but the cells where i need to put those formula are dynamically decided :
Code :
Cells(j, (LastColumn + nbVersions2) + 4).Formula = "=IF(" & Cells(5, nbVersionsCat1 + 1) &
"="""",IF(" & Cells(5, nbVersionsCat1 + 1) & "="""",0,Sheet1!D4),IF('AnotherSheet'!" &
Cells(5, nbVersionsCat1 + 1) & "="""",Sheet1!C4,1))"
So i want to know if the line "IF(" & Cells(5, nbVersionsCat1 + 1) & ".... is the correct syntax to use...
Thank you very much !
I am new to Excel VBA.
I have these random columns:
Range("Y1").Value = "LITIGATE_PERSON_ADDRESS"
Range("Z1").Value = "LITIGATE_PERSON_CITY"
Range("AA1").Value = "LITIGATE_PERSON_TK"
Range("AB1").Value = "LITIGATE_PERSON_ADDRESS_TYPE"
Here's the part of code that is being used for these columns.
Worksheets("MAIN_CONTROL").Cells(i, 25).Value = sourceADR
sourceADR = Replace(sourceADR, "Attica", "")
'...................................................................
sourceADR = Replace(sourceADR, "-", " ")
sourceADR = Replace(sourceADR, " ", " ")
sourceADR = Trim(sourceADR)
auxC = sourceADR
Worksheets("MAIN_CONTROL").Cells(i, 26).Value = sourceADR
'..............................
If (Len(sourceADR) < 1) Then GoTo aseAddr
'..............
mainAddress = Split(sourceADR)
addrAA = ""
Worksheets("MAIN_CONTROL").Cells(i, 24).Value = Str(UBound(mainAddress)) & "##" & Str(LBound(mainAddress))
For jA = UBound(mainAddress) To LBound(mainAddress) Step -1
'......................................................
If (regex.Test(Trim(mainAddress(jA)))) Then
auxC = Replace(auxC, Trim(mainAddress(jA)), "")
destws.Range("BT" & i).Value = Trim(mainAddress(jA))
destws.Range("AA" & i).Value = Trim(mainAddress(jA))
destws.Range("Z" & i).Value = addrAA
auxC = Trim(auxC)
destws.Range("Y" & i).Value = auxC
'--------------------------------------------------------
'-------------------------------------------------------
GoTo aseAddr
End If
auxC = Replace(auxC, Trim(mainAddress(jA)), "")
addrAA = mainAddress(jA) & " " & addrAA
'
'.....................................................
Next jA
'.........................................................
'destws.Range("Y" & i).Value = addrAA
'.....................
aseAddr:
'.................................
My problem is that these columns may change order.
I was suggested to use application.match so that my code may follow but i don't know how to put it inside my code.
Can anyone help?
Thanks in advance
Welcome to SO.
In general, the way you can use an excel function in VBA is the following:
application.WorksheetFunction.Match() 'where match() can be replaced by one of the available worksheet functions.
The function's arguments work pretty much the same way as they do when you use the formula in your worksheet.
The Match functionality is explained thoroughly here
So for example let's say you have an array like the following one in cells A1:A13
And you want to find the location of "Friday". You would do it like so:
Debug.Print Application.WorksheetFunction.Match("Friday", sht.Range("A1:A13"), 0)
And you would get 5 as a result.
That should pretty much cover the "How to use Application.Match()".
I hope you're all doing well!
A quick but tricky question (at least for me)
I have a code that interpolates values according to dates. Basically I want to insert the interpolation value to a cell in the main workbook in the sheet "Deals".
BInterpol is a function that works with a program installed in my computer. In the function, I take dates( D4:D18) linked to values (E4:E18), the following argument is the date that is located in another workbook and sheet and then linear is the method of interpolation. When I write the code below, it gives me Expected end of statement error and it highlights Linear. Any idea what needs to be change so it works ? (I want to make so that the formula is written in the cell needed so the interpolation is done there)
wb2.Sheets("Deals ").Range("V" & x).Value = "=BInterpol('INTERP'!D4:D18,'INTERP'!E4:E18," & wb1.Sheets("New").Range( "I" & j) & " , " Linear " )"
Try with .Formula and concatenate the Linear variable to the string with & as mentioned in the comments:
wb2.Sheets("Deals MTL").Range("V" & x).Formula = "=BInterpol('OIS INTERP'!D4:D18,'OIS INTERP'!E4:E18," & wb1.Sheets("NewTrades").Range( "I" & j) & "," & Linear ")"
Depending on what exactly is Range("I" & j) you may be interested in getting its address and not its value:
wb2.Sheets("Deals MTL").Range("V" & x).Formula = "=BInterpol('OIS INTERP'!D4:D18,'OIS INTERP'!E4:E18," & _
wb1.Sheets("NewTrades").Range("I" & j).Address & _
"," & Linear ")"
Nevermind, got it !
wb2.Sheets("Deals").Range("V" & x).Value = "=BInterpol('INTERP'!D4:D18,'INTERP'!E4:E18," & wb1.Sheets("New").Range( "I" & j) & " , "**"** Linear **"**" )"
The Linear needed two pairs of " for some reason ( don't know why, but that solved it.
Can anyone tell me why is this not working? This stop working my macro and an error message appears
Sh.Cells(2, 13).Formula = WorksheetFunction.VLookup(Sh.Cells(2, 6) & " - " & Sh.Cells(2, 8), Worksheets("Licenciaturas").Range("H2:K2928"), 4, False)
Thanks!
Are you trying to add it as a formula to the cell, or just return the result of the VLookup and place that in the cell?
Your code looks like it's trying to do a bit of both.
If you want a formula to appear in your sheet use:
Sh.Cells(2, 13).FormulaR1C1 = "=VLOOKUP(R2C6 & "" - "" & R2C8, 'Licenciaturas'!R2C8:R2928C8,4,FALSE)"
The formula will appear as: =VLOOKUP($F$2 & " - " & $H$2, Licenciaturas!$H$2:$H$2928,4,FALSE)
My countif function works fine in the excel sheet itself as shown below
=COUNTIF(F111506:K111519,">0")
I however do not get the same results when executing this in a macro. Stepping through the macro, I do the see the correct range highlighted.
nonZeroCount = Application.CountIf(Range(("F" & curRow + 5), ("K" & curRow + 18)).Select, ">0")
I always get a value of 0. Can anybody suggest what I am doing wrong?
thanks
Use this one:
nonZeroCount = Application.CountIf(ThisWorkbook.Worksheets("shName").Range("F" & curRow + 5 & ":K" & curRow + 18), ">0")
where shName relevant sheet name.
Or another way:
With ThisWorkbook.Worksheets("shName")
nonZeroCount = Application.CountIf(.Range(.Cells(curRow + 5, "F"), .Cells(curRow + 18, "K")), ">0")
End With
Maybe use the Evaluate command:
FormulaResultCount = Evaluate("CountA(C:C)")
Replace the stuff in quotes with your formula.
Another way to get your desired result is
nonZeroCount = WorksheetFunction.CountIf(Range("F" & curRow + 5, "K" & curRow + 18), ">0")