Solved

VBA Single Data Type - keep results to one decimal place

Posted on 2009-05-05
1
1,387 Views
Last Modified: 2012-06-22
I have a loop that takes a number from the user, tests an Excel document for instances of this number, and then subtracts one tenth from the input number, and tests again. The loop operates until the number ends up at zero.

Now the problem that I have is after two or three loops the result of the Input = Input - 0.1 begins resulting in a number with several decimal places when I want to keep it to just one. (i.e. at the the third time the loop operates starting at 1 it results in 0.6999999 instead of just 0.7.

I need it to remain at the one decimal place so I get an even number each time, in sequence (i.e. 1, 0.9, 0.8, 0.7 and so on until Zero.) I'm using Single data type. Is there a better data type to use?
Private Sub btnProcess_Click()
 

Dim strPercent2 As String

strPercent2 = Me.txtPercentage.Value
 

Dim sngPercent2 As Single

Dim strSearch As String
 

If IsNumeric(strPercent2) Then

sngPercent2 = CSng(strPercent2)
 

    Do While sngPercent2 > 0.1
 

    strSearch = CStr(sngPercent2)

 

 

 

    Dim i As Long

    i = 1

    Do Until i > Cells(65536, "a").End(xlUp).Row

        If InStr(Trim(Cells(i, "a").Value), strSearch) > 0 Then
 

            Rows(i - 1 & ":" & i + 3).delete

        Else

            i = i + 1

        End If

       

    Loop

 

        MsgBox CStr(sngPercent2)

        Application.ScreenUpdating = True

        sngPercent2 = sngPercent2 - 0.1

 

    Loop
 
 

Else

End If
 

End Sub

Open in new window

0
Comment
Question by:ThePATMAN26
1 Comment
 
LVL 59

Accepted Solution

by:
Saurabh Singh Teotia earned 500 total points
ID: 24307423
Replace this line...
sngPercent2 = sngPercent2 - 0.1

with this..
sngPercent2 = round(sngPercent2 - 0.1,1)
This will take care of your problem...
Saurabh...
 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now