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
Related
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.
I won't bring the entire code, I'll try to show what is relevant.
The following code (which is inside a loop, but it doesn't matter) calls a function (compareStrings) which returns an integer.
Sheet1.Range("S" & i).Value = compareStrings(sheet1.Range("J" & i).Value, sheet1.Range("K" & i).Value)
So basically I have a loop that fills column S with integers.
I then sort S column in ascending order.
later I have another loop, that is supposed to do something with all the values that are less than 5.
The loop looks like this:
With Sheet1.Range("S" & i)
Do Until .Value < 5
If .Value = 0 Then
'some statement
Else
'some statement
End If
i = i + 1
Loop
End With
For some reason it doesn't go in the loop although I have many rows with values that are < 5. I actually tried to change it to <> and it doesn't go in either. It is as if it doesn't see it as an integer, although I have put integers in these cells.
Any ideas?
Thanks
"I have another loop that is supposed to do something with all the values that are less than 5."
Your logic doesn't make sense though.
Do Until .Value < 5 will not do anything with values less than 5. It's the same as saying "take action if my value is greater than or equal to 5.
Do While might be a better option.
I was wondering if there is a workaround to not retype the code if a deeper select case dont' match the criteria.
I have to check whether the Month is one of those stated below, if it is I have to check if the day of that month match with the criteria. If the day dont match any case, the code that have to run is the one set in the above Select statement in the ELSE case.
The only way I know to workaround this is by using and IF statement instead of the nested select case and using a GO TO LABEL to bring me back to the CASE ELSE statement but it seems very unclassy.
Is there a way not to retype the same code in the else case if the deeper select case dont match the criteria ?
Select Case Month(Range("b" & Start))
Case 1
Case 4
Case 5
Case 6
Case 8
GiornoMese = listaattiva(j).Text & "/" & Month(Range("b" & Start)) & "/" & Year(Range("b" & Start))
Select Case Day(GiornoMese)
Case 15
**HERE IF THE DAY IS NOT THE 15th
CODE SHOULD JUMP OUT AND RUN CODE TYPED
IN THE CASE ELSE STATEMENT**
Case 12
Case Else
'Debug.Print listaattiva(j).Text & "/" &
'Month (Range("b" & Start)) & "/" & Year(Range("b" & Start))
GiornoMese = listaattiva(j).Text & "/" & Month(Range("b" & Start)) & "/" & Year(Range("b" & Start))
If Weekday(GiornoMese, vbMonday) = 7 Then
'Domenica - Tutte le ore del giorno sono F3
For Each quarto In listaattiva(j).Attributes
'Debug.Print listaattiva(j).Attributes.Length
'Debug.Print quarto.Text
F3 = F3 + quarto.Text
'Debug.Print F3
Next
#MarcinSzaleniec This would make me retype the code (or ctrl-v paste) I've already stated in the CASE ELSE STATEMENT.
I got to check if the data listed in a XML file are on Public Holidays.
To do so I check first the month and then the day.
If it's not an actual Public Holidays Month I just go with standard code in the ELSE STATEMENT, if it's a Month AND an Holiday day I write different code BUT if it's a Month but not a Holiday day I would run the already stated code for the ELSE STATEMENT.
Mine is more a "beauty" matter, I'd like to keep the code in the same SUB having a vertical work flow not having to make ppl jump on other subs.
Make separate sub from the code in Case Else section. You may pass necessary variables as arguments. Try something like:
Sub DoIfElse (_Year as Integer, _Month as Integer)
(...)
end Sub
It is hard do decipher which variables are really needed because you published only fragment of the code, but I think you know what do you need.
Then, you can use DoIfElse in various places in your main subroutine, as you wish.
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))
Let's say you have a formula that produced the following numbers:
50.77%
24.62%
24.62%
Added together they equal 100%, however they do not want the decimal places shown, and the number still visually showing they equal 100%. If you take the decimal places out you get 51% 25% 25% which actually equal 101%.
They suggested that I put some type of formula that will round the largest decimal place up.. ie... 50.77% becomes 51%, and round the others down... if it goes over 100%, if it is still not equal to 100%, round up the next largest number until and so on until the total does equal 100%.
Anyone know what type of formula of vba coding that I could use for this?
I do not even know where to start.
You can use helper columns with this formula:
=IF(SUM($B$1:B1)+ROUND(A2,2)>1,1-SUM($B$1:B1),ROUND(A2,2))
Unless I'm missing something, you can do: =TEXT(SUM(A1:A3),"##"), assuming your numbers are in A1:A3.
What I usually do in cases like this is to set a formula at the last number (e.g. A4) like this one :
=1-(SUM(A1:A3))
Edit:
This is what I do, if I have to use vba:
Calculate the whole range. Round each number as you wish.
See the difference which you should add or substract (e.g. if you get the sum
to 99.9%, then your difference is 0.01).
Add this difference to the last number in the range. Via VBA.
Even if it is negative, (e.g. if you have a sum of 100.01) then you should sum with a negative number, thus anything is ok.
The only I can think of is "cheating" in a really nasty way... assuming that you have formulas, which do the calculation, a replacement is not possible... So I simply change the formatting (and keep the old values) like this:
Private Sub recalc_percs()
Dim RangeToCheck As Range
Set RangeToCheck = Range("A5:A9") 'designet to work with a range containing only 1 area and 1 column
Dim x(1) As Variant, i As Long, b(2) As Double
RangeToCheck.NumberFormat = "0%"
x(0) = Evaluate(RangeToCheck.Address & "*100")
x(1) = Evaluate("INDEX(ROUND(" & RangeToCheck.Address & "*100,0),)")
While Application.Sum(x(1)) <> 100 And Abs(100 - Application.Sum(x(1))) < 4 'the <4 is for deleting or whatever to not loop endless times
For i = 1 To RangeToCheck.Rows.Count 'get highest and lowest difference
b(0) = x(1)(i, 1) - x(0)(i, 1)
If b(0) < b(1) Then b(1) = b(0)
If b(0) > b(2) Then b(2) = b(0)
Next
If Application.Sum(x(1)) < 100 Then b(0) = b(1) Else b(0) = b(2)
For i = 1 To RangeToCheck.Rows.Count
If b(0) = x(1)(i, 1) - x(0)(i, 1) Then
x(1)(i, 1) = x(1)(i, 1) + (0.5 + (b(0) > 0)) * 2
Exit For
End If
Next
Wend
For i = 1 To RangeToCheck.Rows.Count
If Application.Round(x(0)(i, 1), 0) <> x(1)(i, 1) Then
RangeToCheck.Cells(i).NumberFormat = """" & x(1)(i, 1) & "%"""
End If
Next
End Sub
It may need to change some parts... but basically it is working. Also it is not looking very well, but I am in a hurry right now... sorry.
If you have any questions, just ask... I'll reply as soon as possible ;)