Problem with using VBA to insert formula in Excel sheet - excel

I recently ran into an issue when using VBA to insert a formula into an Excel sheet. Below the relevant piece of code;
Calculatie_cell.Offset(n, 5) = "=SOM(H" & (n + 3 - 1) & ":H" & (n + 3 - Onderdelen_aantal) & ")"
Calculatie_cell.Offset(n, 8) = "=SOM(K" & (n + 3 - 1) & ":K" & (n + 3 - Onderdelen_aantal) & ")"
For the first iteration where it triggers, the expected output is;
=SOM(H4:H7)
=SOM(K4:K7)
The output I recieve is;
=#SOM(H4:H7)
=#SOM(K4:K7)
This output yields a #NAME? error. Pressing F2, enter on the cell (like inputting it by hand), gives an error stating that this formula is not supported by older versions of Excel with the option to correct it to the expected output. If I accept this it works.
I can't find why Excel thinks it needs the # and the code below does not filter the # from the formule (but it does filter it out of random text with an #)
Calculatie_cell.Offset(n, 5) = Replace(Calculatie_cell.Offset(n, 5), "#", "")
Calculatie_cell.Offset(n, 8) = Replace(Calculatie_cell.Offset(n, 8), "#", "")
Does anyone have a suggestion how I can get rid of the #, or another way to make the formula work? Thanks in advance!
Relevant note; I use Excel 2016 in Dutch, meaning "SOM" is the correct way to spell "SUM"

First of all, regardless of your Office installation language, VBA asks for engligh input.
Therefore, replace SOM by SUM in your code.
It should appear as SOM in the workbook, but not in the code.
Second of all, using oCell = "=SOME_FORMULA" usually does not yield the intended result.
Instead, one should use oCell.Formula2 = "=SOME_FORMULA".
This is why an # shows up in the formula.
See this article for information.
Finally, your code should be
Calculatie_cell.Offset(n, 5).Formula2 = "=SUM(H" & (n + 3 - 1) & ":H" & (n + 3 - Onderdelen_aantal) & ")"
Calculatie_cell.Offset(n, 8).Formula2 = "=SUM(K" & (n + 3 - 1) & ":K" & (n + 3 - Onderdelen_aantal) & ")"

User #Solar Mike was right.
Even though Excel wants the code to be "SOM" (Dutch spelling), in VBA it needs to ben SUM (English spelling). It auto-translates and works right of the bat. Thank you for your help!
If you post it as an answer, I will accept it as such.

Related

VBS Excel cell to SAP

I am struggeling to get the right informations from an Excel sheet via vbs script into SAP.
A single cell works fine and will be added into SAP but the original Excel sheet contains a cell where the last 4 digits needs to be deleted and afterwards combined with another cell and then added to SAP.
This works for me:
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP06/ssubTABFRA1:SAPLMGMM:2000/subSUB5:SAPLZMM_GINCOFIELDS:5802/ctxtMARC-PRCTR").text = (objSheet.Cells(6,"P") & (objSheet.Cells(38,"P")).Value)
But this dont:
session.findById("wnd[0]/usr/subSUBSCR_BEWERT:SAPLCTMS:5000/tabsTABSTRIP_CHAR/tabpTAB1/ssubTABSTRIP_CHAR_GR:SAPLCTMS:5100/tblSAPLCTMSCHARS_S/ctxtRCTMS-MWERT[1,1]").text = (objSheet.Cells(40,"F") & (Left((40,"F"), Len(40,"F") - 4)) & (objSheet.Cells(39,"L") & (objSheet.Cells(39,"O").Value)
The error message while starting the script means translated: ...at 207 ')' excpected...
Thanks in advance for having a look at this and providing help or a hint.
You are using Left(40,"F"): I suspect that what you actually mean is objSheet.Cells(40, "F"). Same with your Len call, I suspect what you actually want is: Len(objSheet.Cells(40, "F")). So putting that together:
Left(objSheet.Cells(40, "F"), Len(objSheet.Cells(40, "F")) - 4) & objSheet.Cells(39, "L") & objSheet.Cells(39, "O")

formula for difference between start & end time includes milliseconds in Excel

I want to find the time difference between start & end time using Excel
my Data :
A1 = 16:00:03:38
B2 = 16:14:13:58
which is in pattern of "h:mm:ss:ms" h=hours,mm=minutes,ss=seconds,ms=milliseconds.
am using like this =B2-A1 but it not giving result instead of it giving output like this " #VALUE! "
if i change like this
A1 = 16:00:03.38
B2 = 16:14:13.58
answer is = 00:14:10:20
the answer is giving perfect
but i don't want to change : to .
is it possible to take difference between two time's as per my requirements.
Let the formula do the conversion:
=TIMEVALUE(LEFT(B2,8) & "." & RIGHT(B2,2))-TIMEVALUE(LEFT(A1,8) & "." & RIGHT(A1,2))

Code works the 1st time, but not the 2nd time

This code just works on the first time because when It's activated again, It'll try to sum a function, not the value from this function.
For example: on first try the user enters with 100 on J" & lrTest - 1 and G6 = 0. Then it'll be =sum (0, 100), which is equal 100. But on the second try, it'll try to sum the function and won't go on.
If cmbcategoria.Text = "Casa" Then
Range ("G6").Formula = "=sum ("G6", J" & lrTest - 1 &")"
End If
What can I do to solve it?
Ps: I guess you don't know the full code, but if needed, I can post it.
This is how my workbook looks by now
This will work =)
If cmbcategoria.Text = "Casa" Then
Range("G6").Formula = "=SUM(" & Range("G6").Value & ", J" & lrTest - 1 & ")"
End If

Using SUMIF to Evaluate two rows of Data and output anwser to two specific cells

My data looks like this:
1 cs
2 ea
1 cs
1 cs
1 cs
1 cs
11 cs
There are numbers in the left column and quantity’s in the right
"CS" = Cases and "EA" = Each.
I have used this formula for each result Separately:
(CELLB8)=SUMIF(E2:E4,"CS",D2:D4)
(CELLC8)==SUMIF(E2:E4,"EA",D2:D4)
I need to combined these two "if" statement into one, but still have the output for "cases" and "each" in their Cells.
I don't know if this is even possible. I have looked everywhere for answer.
Thank you!
Your question is not that clear but if you want to visualize your answer in one cell next to each other you can use & for this:
=SUMIF(E2:E4,"CS",D2:D4) & "CS " & SUMIF(E2:E4,"EA",D2:D4) & "EA"
A little bit more detail as follows:
In the picture,
cell (A9) = cumulation of "CS":
=SUMIF(B2:B8,"CS",A2:A8) & " CS "
cell (B9) = cumulation of "EA":
=SUMIF(B2:B8,"EA",A2:A8) & " EA"
cell (A10) = combines cumulations:
=SUMIF(B2:B8,"CS",A2:A8) & " CS & " & SUMIF(B2:B8,"EA",A2:A8) & " EA"

Move Two characters from beginning to end of string

I have an Excel 2010 document which has hundreds of rows each with a cell as below:
(without the quotes, of course)
"XX - A Name of Something Here"
Which I need to change to:
"A Name of Something Here (XX)"
I'm trying to figure out the best way to accomplish this and could really use some assistance.
Try this:
=RIGHT(A3, LEN(A3) - 5) & " (" & LEFT(A3, 2) & ")"
which means
all but the last 5 chars of the string, counting from right to left
5 because XX space dash space is 5 chars long
a space and an open parenthesis
the leftmost two chars ie XX
a close parenthesis.
This formula will work given your example
=MID(A1&" ("&A1,5,LEN(A1))&")"
How about:
=MID(A1,3,9999) & "(" & LEFT(A1,2) & ")"
but how about the dash??
=RIGHT(A1, LEN(A1) - 5) & " (" &LEFT(A1, 2) & ")"
This will work too - I like "concatenate" since I don't need to keep track of the &'s
=CONCATENATE(RIGHT(A3,LEN(A3)-5)," (", LEFT(A3,2),")")
So many fun ways to do the same thing...

Resources