I am having these codes with
cells(4,9)=0, cells(4,10)=1, cells(4,11)=0.01
in the sheet:
AF_from = Cells(4, 9).Value
AF_to = Cells(4, 10).Value
af_jump = Cells(4, 11).Value
For x = AF_from To AF_to Step af_jump
Cells(19, 8).Value = x
then the result is 0, 0.01,0.02,0.03...as expected until 0.83, then unexpected results 0.839999, 0.849999, 0.859999...1.999998
What should I do to fix this? Thanks.
First of all I wouldn't use a For loop using Double types. Explaining a little bit better: counters are ok when you use an IntegerorLong but you may encounter problems if you use fractions.
You could multiply your af_jump and AF_to (in this case by 100) and use those. Then write Cells(19, 8).Value = x/100.
You need also to be sure that all those variables have been set to their correct type. Especially the x which if not, will reproduce numbers as 0.84 like 0.8399999999. That is the "normal" way vba calculates. This is due to a finite bit precision.
https://stackoverflow.com/tags/floating-point/info
Assuming this is Excel, set the number format to two decimals.
Related
Recently started working with VBA. Youtube and this forum have been excellent help so far. However, my problem is this:
Private Sub CommandButton1_Click()
Dim i As Integer
i = 1
Do While Sheet2.Cells(i, 1).Value <> 0
Sheet3.Cells(i, 1).Value = Sheet2.Cells(i, 1)
i = i + 1
Loop
Do While Sheet2.Cells(i, 10).Value <> 0
Sheet3.Cells(i, 4).Value = Sheet2.Cells(i, 10)
i = i - 1
Loop
Do While Sheet2.Cells(i, 6).Value <> 2
Sheet3.Cells(i, 3).Value = Sheet2.Cells(i, 6)
i = i + 1
Loop
End Sub
This script populates information in the correct sheet as well as the correct columns, at least until it encounters an empty cell from Sheet2. At this point is moves on to the next "Do While" instead of referring to the next non blank cell.
I've also encountered a Runtime Error 6- Overflow with this specific line:
Do While Sheet2.Cells(i, 6).Value <> 2
Sheet3.Cells(i, 3).Value = Sheet2.Cells(i, 6)
i = i + 1 <<<<----- ERROR??
I'm fairly certain that the Overflow Error is a result of Dim i as Integer vs. Dim i as String, but then again I've pretty much but working by trial and error, moving portions of script around and playing with expressions/functions.
As mentioned before I'm new to VBA. I'm also the kind of guy that learns by doing as well. I've looked all over different forums, youtube videos, etc. trying to create a script that works the way I want it to.
UPDATE
Thanks for the quick help and recommendations. I realized I didn't give near enough information in my first post.
1.) I don't need/want to leave an empty cell on sheet3. The script is now looping but not skipping over blanks if that makes sense?
For example:
doing need to do
101 101
102 102
104
104 105
105 106
106
I'm stil running off the end of the world as well so to speak. For some reason the Debug Function is bringing up this line
i = i + 1
2.) the line:
Do While Sheet2.Cells(i, 6).Value <> 2
Sheet3.Cells(i, 3).Value = Sheet2.Cells(i, 6)
is supposed to recognize a column of text not numbers. Not exactly sure if this is having an effect on anything
Thanks again everyone.
UPDATE 3:
I've got rid of the Error Messages and corrected all the expressions in the script. Also, I found that instead of using Do Until....... And........ had zero results. I had to go back to Do While and substitute an Or statement vs. the former.
So far the script is working better than I had expected. Thank You all for the help and insight.
Good news aside, I still need to figure out how to compose an "IF" statement so that the script will skip over blank cells in sheet2.cells(i, 1) and not import present values for that row in the 2 adjacent columns
Here is the current and running script:
Private Sub CommandButton1_Click()
Dim i As Long
i = 3
Do While (Not IsEmpty(Sheet2.Cells(i, 10))) Or (Sheet3.Cells(i, 4).Value <> 0)
Sheet3.Cells(i, 4).Value = Sheet2.Cells(i, 10).Value
i = i + 1
Loop
i = 3
Do While (Not IsEmpty(Sheet2.Cells(i, 1))) Or (Sheet3.Cells(i, 1).Value <> 0)
Sheet3.Cells(i, 1).Value = Sheet2.Cells(i, 1).Value
i = i + 1
Loop
i = 3
Do While (Not IsEmpty(Sheet2.Cells(i, 6))) Or (Sheet3.Cells(i, 3).Value <> 0)
Sheet3.Cells(i, 3).Value = Sheet2.Cells(i, 6).Value
i = i + 1
Loop
End Sub
Thank you again everyone. Hope I can return the favor in the near future.
A few thoughts:
As #dgorti said, make sure to set i before each Do loop to be the index of the row you want to start with.
Dim any integer value as Long, not Integer. Long is 32 bits; Integer is 16.
To skip over empty cells, use IsEmpty. For example, in your first loop: Edited
Do Until (Not IsEmpty(Sheet2.Cells(i, 1))) and (Sheet2.Cells(i, 1).Value = 0)
Edit And, as #A.S.H. points out, you should also range-check i. Since VBA doesn't have short-circuit operators, I would do that at the end of your loop:
Do ...
... 'vvvvv representable in a Long
If i = 65536 Then Exit Do ' Or If i = 1... for loops that count down
i = i + 1
Loop
That way you never run off the end.
Edit I fixed the test above — you want to run until you hit a non-empty cell with a value of 0, right? So Do Until (which is a real thing :) ) loops until exactly that condition holds. The Not IsEmpty() prevents the test against 0 from giving the wrong result on blank cells.
i am pretty new in using VBA for Excel, but after some searching without result i guess i need some help from you.
I am trying to format a specific cell so that, if one condition is met, it shows the value from another cell, but preceded by a specific text, like "Constantin". So, the displayed result would be, for example Constantin 80.25 .
The code that i'm trying looks like this:
If Cells(4, 1) < 0 Then
With Range("A1")
.NumberFormat = "'Constantin' 0.00"
.Value = - Cells(4, 1)
End With
End If
I know the syntax is not right, this is my problem, i guess i can't find the right syntax. I hope I'm not bothering you with this, probably, simple question, but i just couldn't find what i needed. Thanks a lot
It looks like you don't need the word as part of the actual format, in which case something like this will suffice:
If Cells(4, 1).Value < 0 Then
Range("A1").Value = "Constantin " & (Cells(4, 1).Value * -1)
End If
If you really wanted to use a With block then:
With Cells(4, 1)
If .Value < 0 Then
Range("A1").Value = "Constantin " & (.Value * -1)
End If
End With
I need to subtract two columns from a large array and see which ones are positive and of those positive values I need to find the positive values row and append a few things onto that value.
Here is the general concept I'm thinking so far
While < 8000
if (cell(i,1).Value - cell(i,2) > 0)
print in another sheet cell(i,3).value (cell(i,2).Value-cell(i,4)) cell.value(i,4)
for example...
suppose I have something like this
[2 2 hi yo]
[3 2 go mo]
this macro would return "go 1 mo" in another sheet.
Sub Leaves()
Dim i As Integer
Dim g As Integer
Dim Quantity As Integer
Dim Executed As Integer
Dim Leaves As Integer
i = 1
g = 1
Do While i < 8000
Quantity = Worksheets("Sheet1").Cells(i, 3).value
Executed = Worksheets("Sheet1").Cells(i, 5).value
Leaves = Quantity - Executed
If Leaves > 0 Then
Worksheets("Sheet2").Cells(g, 1).value = _
Worksheets("Sheet1").Cells(i, 9).value & _
Worksheets("Sheet2").Cells(i, 2).value & _
Leaves & Worksheets("Sheet2").Cells(i, 3).value
g = g + 1
End If
i = i + 1
Loop
End Sub
The above code gives me a Type mismatch error.
It is helpful if you say what line is throwing an error. Also, one should strive to create a Minimal, complete, and verifiable example. The effort to do so often resolves the question before you need to post it.
The code itself seems fine and it runs for me (on an empty workbook) with no type mismatch. Thus, the problem must be with your assumptions about the spreadsheet.
Either of the lines
Quantity = Worksheets("Sheet1").Cells(i, 3).value
Executed = Worksheets("Sheet1").Cells(i, 5).value
will trigger a type mismatch if the corresponding value can't be converted to an integer. This could happen, for example, if one of the cells contains a string (other than something like "1") or an error value such as #N/A or #Value!.
The line which begins
Worksheets("Sheet2").Cells(g, 1).value = _
will throw a type mismatch if one of the values being concatenated can't be converted to a string. An error value in one of the cells is the most likely culprit. If this is the case and for some reason you actually want to create a string that includes substrings which look like e.g. "#N/A" then you could use the Text property of those cells rather than Value.
Dear Madams and Sirs,
In my code I make use of an CountIf statement.
Cells(5, 3).Value = Application.WorksheetFunction.CountIf(CR, ">" & yel)
Where:
Dim yel As Double
yel = Cells(2, 5).Value
and:
Dim CR As Range
Set CR = Range("D9:Z26")
The weird thing is: If I use the dynamic criterion (& yel) the code give me a zero as result. And when I use a fixed criterion (">0") the code produces the correct number.
Can anyone tell me what I am doing wrong?
Best regards and thanks in advance,
Wouter
I venture to bet on this guess:
You are not using an English Excel. Your Excel version uses , as decimal mark.
The conversion of yel to a string leaves it with a , but the internal engine only understands the English ..
Try if this works:
Dim strYel As String
strYel = Replace(CStr(yel), ",", ".")
Cells(5, 3).Value = Application.WorksheetFunction.CountIf(CR, ">" & strYel)
I'm new to VBA and need to add two values using VBA in Excel. I receive imported values with dot-separator and if I try to add them, the dot is ignored.
So:
1.12
1.34
should result in 2.46, but I'm getting 246
Code:
Do Until (Cells(iRow, 1) = 100)
addValue = Cells(iRow, 2).Value
dValue = dValue + addValue
iRow = iRow + 1
MsgBox (dValue)
Loop
Thanks for some 101-tips...
Here is the updated code:
Dim iRow As Integer
Dim addValue As Double
Dim dValue As Double
iRow = 2
Do Until (Cells(iRow, 1) = 100)
addValue = Cells(iRow, 8)
Debug.Print "Row"; iRow; " is "; addValue
modValue = Replace(addValue, ".", ",")
MsgBox (addValue)
newVal = Cells(iRow, 8).Value
dValue = dValue + newVal
iRow = iRow + 1
Loop
MsgBox (dValue)
Basically I have a lit of values like this:
100 header
200 1.12
200 1.34
200 1.54
100 header
...
I want to sum the 200 correspondig values 1.12, 1.34, 1.54. The values show up as 112, 134 and 154.
Thanks
Would you place this line after addValue =, please?
Debug.Print "Row"; iRow; " is "; addValue
Offhand, I am suspecting that the 1.12 is not really a double.
Also, did you declare your variables? Nothing fancy, but something like
Dim iRow As Integer
Dim addValue As Double
Dim dValue As Double
Finally, you may want to consider placing the MsgBox after the loop, instead of having it give a subtotal on every; iteration.
I think you have some funny number formatting applied to the values in Excel. Highlight the range and hit Ctrl-Shift-` (or format the cells as "General" from the format menu). I bet those numbers are actually 112 and 134.
Right... The problem is formatting, and Excel's insistence on describing (say) '4/7' as 'Fourth of July' rather than four-sevenths*. Or indeed, as a textual 'Four-slash-seven'.
I do not know what strange format has been picked up in those 'decimal' cells, but you have two jobs to do:
Change the number format of those cells to 'general' so you can see
what's actually in them, rather than whatever Excel wants to
present;
Change your code to use the 'Value2' property of a range, which
gives you the raw data instead of a formatted interpretation of the
'value' - newVal = Cells(iRow, 8).Value2
I would also recommend that you declare NewVal as Double. This will coerce a type conversion to floating-point number, a far better idea than letting a 'variant' decide what type it would like to be, based on formatting information in a range.
I'd better warn you that task (1) works about half the time on data imported from Oracle databases, and numbers copied from spreadsheets originating France (where the comma is used as a decimal point) are better-suited to the semiotic analyses developed by French Deconstructivist philosophers, rather than statistical analysis and arithmetic calculation.
*You'll be able (or unable) to reproduce this error some of the time, depending on your machine locale. And that's today's big lesson: a problem is worsened when it is intermittent, unpredictable, or inconsistently-reproducible.