Unexpected behavior in VBA code

MyGiG

New Member
Joined
Mar 17, 2016
Messages
10
Hello, I've written some code that converts 1500 data points to about 200 (copies every 20th data point to the right), since Exel can't use more than 250 to draw a graph. So the wird part is, if I use the code below, everything works fine, but if I devide the result by 10, I get "0" in the fields.
shrani.si
[/URL][/IMG]
Sub Gumb_graf()


navpicno = 2
navpicnoBrezPreskoka = 2
obmocjePolnihCelic = 2

'prešteje polne celice leve tabele
Do While Worksheets("List1").Cells(navpicnoBrezPreskoka, 1) <> ""
obmocjePolnihCelic = obmocjePolnihCelic + 1
navpicnoBrezPreskoka = navpicnoBrezPreskoka + 1
Loop

'določanje celic, katere se bodo zajele
vsakiKolikiPodatek = Round(obmocjePolnihCelic / Worksheets("1").Cells(16, 16), 0)

navpicnoBrezPreskoka = 2

'praznjenje desne tabele, da se ne prekriva
Do While Worksheets("List1").Cells(navpicnoBrezPreskoka, 16) <> ""
Worksheets("List1").Cells(navpicnoBrezPreskoka, 16) = ""
Worksheets("List1").Cells(navpicnoBrezPreskoka, 17) = ""
Worksheets("List1").Cells(navpicnoBrezPreskoka, 18) = ""
Worksheets("List1").Cells(navpicnoBrezPreskoka, 19) = ""
navpicnoBrezPreskoka = navpicnoBrezPreskoka + 1
Loop

navpicnoBrezPreskoka = 2
navpicno = 2

Do While Worksheets("List1").Cells(navpicnoBrezPreskoka, 1) <> ""
Worksheets("List1").Cells(navpicnoBrezPreskoka, 16) = Worksheets("List1").Cells(navpicno, 5)
Worksheets("List1").Cells(navpicnoBrezPreskoka, 17) = Worksheets("List1").Cells(navpicno, 2)
Worksheets("List1").Cells(navpicnoBrezPreskoka, 18) = Worksheets("List1").Cells(navpicno, 3)
Worksheets("List1").Cells(navpicnoBrezPreskoka, 19) = Worksheets("List1").Cells(navpicno, 4)
navpicno = navpicno + vsakiKolikiPodatek
navpicnoBrezPreskoka = navpicnoBrezPreskoka + 1
Loop


End Sub

Now, if i devide the result by 10 (code below) I get zeros at the end

Sub Gumb_graf()


navpicno = 2
navpicnoBrezPreskoka = 2
obmocjePolnihCelic = 2

'prešteje polne celice leve tabele
Do While Worksheets("List1").Cells(navpicnoBrezPreskoka, 1) <> ""
obmocjePolnihCelic = obmocjePolnihCelic + 1
navpicnoBrezPreskoka = navpicnoBrezPreskoka + 1
Loop

'določanje celic, katere se bodo zajele
vsakiKolikiPodatek = Round(obmocjePolnihCelic / Worksheets("1").Cells(16, 16), 0)

navpicnoBrezPreskoka = 2

'praznjenje desne tabele, da se ne prekriva
Do While Worksheets("List1").Cells(navpicnoBrezPreskoka, 16) <> ""
Worksheets("List1").Cells(navpicnoBrezPreskoka, 16) = ""
Worksheets("List1").Cells(navpicnoBrezPreskoka, 17) = ""
Worksheets("List1").Cells(navpicnoBrezPreskoka, 18) = ""
Worksheets("List1").Cells(navpicnoBrezPreskoka, 19) = ""
navpicnoBrezPreskoka = navpicnoBrezPreskoka + 1
Loop

navpicnoBrezPreskoka = 2
navpicno = 2

Do While Worksheets("List1").Cells(navpicnoBrezPreskoka, 1) <> ""
Worksheets("List1").Cells(navpicnoBrezPreskoka, 16) = Worksheets("List1").Cells(navpicno, 5)
Worksheets("List1").Cells(navpicnoBrezPreskoka, 17) = Worksheets("List1").Cells(navpicno, 2) / 10
Worksheets("List1").Cells(navpicnoBrezPreskoka, 18) = Worksheets("List1").Cells(navpicno, 3) / 10
Worksheets("List1").Cells(navpicnoBrezPreskoka, 19) = Worksheets("List1").Cells(navpicno, 4) / 10
navpicno = navpicno + vsakiKolikiPodatek
navpicnoBrezPreskoka = navpicnoBrezPreskoka + 1
Loop


End Sub
shrani.si
[/URL][/IMG]

Does anyone know why this happens?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hello, I've written some code that converts 1500 data points to about 200 (copies every 20th data point to the right), since Exel can't use more than 250 to draw a graph.

Hi

There's no limit in excel current and last versions for the number of points in a chart series (other than available memory).
Even in old versions like excel 2007 you could have 32000 points in a chart series.

In your case since you just have 1500 points per series I don't think that that will be a problem.

Please clarify.
 
Upvote 0
First of all, thank you for taking the time to look at my issue.
Well, I’ve tried using all of the data, but when the graphs are drawn (~500 data points per force plus ~500 for distance) it only draws for the first ~250. But in anycase, taking less data points is actually helpful for me, since the graphs are only for visual representation and are more pleasing to the eye that way.
Do you have any idea what is causing the “0”s i keep getting?
 
Upvote 0
If it helps, on the left is data i get out of a machine, to the right is data, that I use to draw 3 graphs. But that is not the problem, the problem is, when i run the code it works fine, but when I add “/10” to the output, zeros happen (2nd picture).
 
Upvote 0
Hi

I suspect you are performing the division with empty cells.

Let's say that B1 is empty.

If you use:

Code:
Range("A1").Value = Range("B1").Value

then A1 will also be empty.

but if you use

Code:
Range("A1").Value = Range("B1").Value / 10

then vba, since you want to perform an arithmetic operation will use the default 0 value for the cell value, and then you get also 0 in A1

For ex., if you'd use

Code:
Range("A1").Value = Range("B1").Value + 1

you'd get 1 in A1.

Please comment.
 
Upvote 0
Thank you for the reply.
I've allready considered that and double checked it. the line
Code:
[COLOR=#333333][I]Do While Worksheets("List1").Cells(navpicnoBrezPreskoka, 1) <> ""[/I][/COLOR]
looks for data in colum A and if there is data, there should also be data in colum B, C, D and E (since that's how the machine outputs data). so if the cells is empty the code shouldn't execute and there for always devide cells with a number in it.
thanks for the link, will check it.
 
Upvote 0
OK.

Try this simple test

Code:
Do While Worksheets("List1").Cells(navpicnoBrezPreskoka, 1) <> ""
             Worksheets("List1").Cells(navpicnoBrezPreskoka, 16) = Worksheets("List1").Cells(navpicno, 5)
             Worksheets("List1").Cells(navpicnoBrezPreskoka, 17) =[B][COLOR=#800000] "Test"[/COLOR][/B]
             Worksheets("List1").Cells(navpicnoBrezPreskoka, 18) = Worksheets("List1").Cells(navpicno, 3) / 10
             Worksheets("List1").Cells(navpicnoBrezPreskoka, 19) = Worksheets("List1").Cells(navpicno, 4) / 10
        navpicno = navpicno + vsakiKolikiPodatek
        navpicnoBrezPreskoka = navpicnoBrezPreskoka + 1
    Loop
 
Upvote 0
Great idea, haven't thought of that yet. Will do it tomorrow at work and post the results!
 
Upvote 0
I tested it and here are the results.
When using the code:
Code:
    Do While Worksheets("List1").Cells(navpicnoBrezPreskoka, 1) <> ""        
        Worksheets("List1").Cells(navpicnoBrezPreskoka, 16) = Worksheets("List1").Cells(navpicno, 5)
        Worksheets("List1").Cells(navpicnoBrezPreskoka, 17) = Worksheets("List1").Cells(navpicno, 2) / 10
        Worksheets("List1").Cells(navpicnoBrezPreskoka, 18) = "Test"
        Worksheets("List1").Cells(navpicnoBrezPreskoka, 19) = Worksheets("List1").Cells(navpicno, 4) / 10
        navpicno = navpicno + vsakiKolikiPodatek
        navpicnoBrezPreskoka = navpicnoBrezPreskoka + 1
    Loop
comes the following output:
test-in-10-rezultat.png


and if I use this code:
Code:
    Do While Worksheets("List1").Cells(navpicnoBrezPreskoka, 1) <> ""        
        Worksheets("List1").Cells(navpicnoBrezPreskoka, 16) = Worksheets("List1").Cells(navpicno, 5)
        Worksheets("List1").Cells(navpicnoBrezPreskoka, 17) = Worksheets("List1").Cells(navpicno, 2)
        Worksheets("List1").Cells(navpicnoBrezPreskoka, 18) = "Test"
        Worksheets("List1").Cells(navpicnoBrezPreskoka, 19) = Worksheets("List1").Cells(navpicno, 4)
        navpicno = navpicno + vsakiKolikiPodatek
        navpicnoBrezPreskoka = navpicnoBrezPreskoka + 1
    Loop

I get this as output:
test-brez-10-rezultat.png

So yes, there is something wrong with my loop. Thanks for the help! :D
 
Upvote 0

Forum statistics

Threads
1,214,797
Messages
6,121,629
Members
449,041
Latest member
Postman24

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top