I am tryin to postprocess data. As well I got column which have hour minute and second. With VBA I created a loop to go through every single row in a for loop.
For i = 2 To Max
Range("GE" & CStr(i + Offset)).Formula = "=TIME(AK & CStr(i + Offset);AL & CStr(i + Offset);AM & CStr(i + Offset))"
Next i
It comes to Run-time error 1004: Application defined or object defined error.
How can we edit this to be working?
For i = 2 To Max
Range("GE" & CStr(i + Offset)).Formula = "=TIME(AK" & CStr(i + Offset) & ",AL" & CStr(i + Offset) & ",AM" & CStr(i + Offset) & ")"
Next i
The loop variable needs to be "outside" the string you're entering as a formula to allow it to be built correctly.
This fixes the typo in your code, but you should consider setting the range specifically for the worksheet.
I've also changed your semicolons to commas as that's how the TIME function works in Excel here, but if your settings are different you might need to revert that.
Related
I have the following question, is it posible to insert a formula inside a variable in VBA Excel to use this variable after?
For example:
MinO = "=MIN(" & .Cells(3, 7 + NumSheet).Address(False, False) & ":" & .Cells(3, 9 + NumSheet).Address(False, False) & ")"
For N = 1 To NumSheet + 1
.Cells(4, 5 + N).Value = 80 * ((.Cells(3, 8 + NumSheet).Value - .Cells(3, 5 + N).Value) / (.Cells(3, 8 + NumSheet).Value - MinO))
.Cells(4, 5 + N).Font.Bold = True
Next N
So MinO is a variable with a formula inside, I know in Range().Formula it works but how can I make it work using a variable?
I know there is also the Application.WorksheetFunction.Min but I want to see if it's posible doing it the other way as WorksheetFunction sometimes give me some errors.
Thank you so much for your answers!
You can do this, but therefore you need the Evaluate() function. (The URL refers to the official documentation of that function)
So, in your case, you might use: (seems not to be completely correct, so I'll put it in comment)
' .Cells(4, 5 + N).Value = 80 * ((.Cells(3, 8 + NumSheet).Value - .Cells(3, 5 + N).Value) / (.Cells(3, 8 + NumSheet).Value - Evaluate(MinO)))
As mentioned in below comments, the following seems to be the correct approach:
MinO = Evaluate("=MIN(" & .Cells(3, 7 + NumSheet).Address(False, False) & ":" & .Cells(3, 9 + NumSheet).Address(False, False) & ")")
Usually when I work with formulas from excel i write them in cell (check that everything works) and then in VBA immediate window (CTRL+G) I type
? Activecell.Formula
or
? Activecell.FormulaR1C1
And it displays formula that you need... That is good start then you start changing parts to reflect your desired variable changes.
Remember that this output is string and in code needs to start and end with 1x", when you want to add string in formula you need to add "" also sometimes you need to add """" and """ for strings in string... :)
I don't think this will work, as it is basically just a text string. I believe that you try to look into why the Application.WorksheetFunction is giving errors instead, or creating your own function if you have some special cases that the worksheet function cannot handle.
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 trying to find the average of a column with undefined number (i) of values (range is likely to be between 3-20 cells in column I, row 24 up to undefined number - j = 23 + i). The code previously included only the standard average function.
Now, I'd like it to average those cells in the give range, only including those cells in the proposed range. Hence, ignoring the identified outliers that are above and/ or below LFe, UFe. Location of that averaged value is Range("E" & m).
Is this even possible in the given worksheetfunction? And if so, what do I need to change to get this working?
I have tried to use existing solutions based on conditions such as 'average only cells that are positive' or 'only those that do not include N/A', but can't seem to get it working.
'Identifying outliers based on interquartile range
qe1 = Application.Quartile(Range("E24:E" & j), 1)
qe3 = Application.Quartile(Range("E24:E" & j), 3)
IQRe = qe3 - qe1
UFe = qe3 + (IQRe * 1.5)
LFe = qe3 - (IQRe * 1.5)
'[EDIT] Currently I have it as follows:
Range("E" & m).Value = Application.WorksheetFunction.AverageIfs(Range("E24:E" & j), "<" & UFe, Range("E24:E" & j), ">" & LFe, Range("E24:E" & j))
Which returns: #VALUE! without the .WorksheetFunction, and 'run error '424' Object required' as written above.
The only option that functioned, BUT not with my variable ranges is formatting it similarly as following: (How can this otherwise be adapted to work for my variable ranges, including 'j' etc.?)
Range("E" & m).Select
ActiveCell.FormulaR1C1 = "=AVERAGEIF(R[-6]C[4]:R[-3]C[4],"">""& 4.3,R[-6]C[4]:R[-3]C[4])"
Not enough rep to comment (sorry!), but here are a few ideas I had:
1) As SJR has mentioned, if there is a numeric value you're basing the color-coding from, you can use that in your code instead of a color. I'm thinking UFe & LFe are the upper and lower thresholds. So you could just have cells with formulas to automate the UFe & LFe via formulas instead and then use "AVERAGEIFS".
2) Alternatively, you could integrate your variables into a ".formula" instead, like:
Sheet1.Range("E" & m).Formula = "=AVERAGEIFS($E$24:$E$" & j & ",$E$24:$E$" & j & ","">" & LFe & """,$E$24:$E$" & j & ",""<" & UFe & """)"
Just change "Sheet1" to whatever sheet you're referencing
I am trying to insert a SUMIF formula into a cell using VBA. VBA is not allowing my code to run as it is. Any suggestions are appreciated.
ws and na are properly set earlier on in the code. If I simply change the SUMIF formula to a random value "x" , it appears in the desired cell. The error is occurring within the SUMIF formula that I am trying to insert into a cell.
ws.Range("B" & na.Row + 2).Value = "=SUMIF(OFFSET(B1,,,ROW()-1,1),"<>#N/A"))"
The purpose of this formula is to SUM a column of numbers while ignoring any cells that contain "#N/A".
When using quotes in a formula, you need to "double up":
ws.Range("B" & na.Row + 2).Formula = "=SUMIF(OFFSET(B1,,,ROW()-1,1),""<>#N/A"")"
You can use AGGREGATE and remove the OFFSET which is volatile
ws.Range("B" & na.Row + 2).Formula= "=AGGREGATE(9,6,B1:B" & na.Row + 1 & ")"
Try using 'Chr(34)':
ws.Range("B" & na.Row + 2).Formula = "=SUMIF(OFFSET(B1,,,ROW()-1,1)," & Chr(34) & "<>#N/A" & Chr(34) & ")"
Edit: Deleted quotes written by mistake
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.