ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   bad data when i change string to double (https://www.excelbanter.com/excel-programming/369382-bad-data-when-i-change-string-double.html)

[email protected]

bad data when i change string to double
 
I created macro in excel and i have problem. I created buttons with
numbers(like in windows calculator) and when i add two integer numbers
everything is ok but when i add two fractions i have bad result. I think
problem is when i change string to double(becouse when i add point i use
string).
thanks Pawel

Jim Thomlinson

bad data when i change string to double
 
Post some code...
--
HTH...

Jim Thomlinson


" wrote:

I created macro in excel and i have problem. I created buttons with
numbers(like in windows calculator) and when i add two integer numbers
everything is ok but when i add two fractions i have bad result. I think
problem is when i change string to double(becouse when i add point i use
string).
thanks Pawel


[email protected]

bad data when i change string to double
 

first form:

Public mpl As Double

Private Sub dodawanie_Click()
If liczba1 = Empty Or liczba2 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If

If liczba1 < Empty And liczba2 < Empty Then

Dim b, a As Double
a = CSng(liczba1)
b = CSng(liczba2)

wynik = a + b

End If
End Sub

Public Sub odejmowanie_Click()
If liczba1 = Empty Or liczba2 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If

If liczba1 < Empty And liczba2 < Empty Then
Dim a, b As Double
a = CSng(liczba1)
b = CSng(liczba2)

wynik = a - b

End If
End Sub

Private Sub mnozenie_Click()
If liczba1 = Empty Or liczba2 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If

If liczba1 < Empty And liczba2 < Empty Then
Dim a, b As Double
a = CSng(liczba1)
b = CSng(liczba2)

wynik = a * b

End If

End Sub

Private Sub dzielenie_Click()

If liczba1 = Empty Or liczba2 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If

If liczba1 < Empty And liczba2 < Empty Then

Dim a, b As Double
a = CSng(liczba1)
b = CSng(liczba2)

If b = 0 Then
x = MsgBox("nie dzielimy przez 0", vbCritical + vbOKOnly,
"DEBILU!!!")
End If

If b < 0 Then
wynik = a / b
End If
End If
End Sub

Private Sub sin_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If

If liczba1 < Empty Then
Dim a As Double
a = CSng(liczba1)

wynik = Math.sin(a)
End If

End Sub

Private Sub cos_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If

If liczba1 < Empty Then
Dim a As Double
a = CSng(liczba1)

wynik = Math.cos(a)

End If
End Sub

Private Sub tan_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If liczba1 < Empty Then
Dim a As Double
a = CSng(liczba1)

wynik = Math.tan(a)
End If
End Sub


Private Sub ctg_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If liczba1 < Empty Then
Dim a As Double
a = CSng(liczba1)

wynik = 1 / Math.tan(a)
End If
End Sub


Private Sub CommandButton1_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If liczba1 < Empty Then
liczba1 = liczba1 * (-1)
End If
End Sub

Private Sub CommandButton2_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If liczba1 < Empty Then
Dim a As Double
a = CSng(liczba1)
If a <= 0 Then
x = MsgBox("nie pierwiastkujemy liczb mniejszych od 0", vbCritical +
vbOKOnly, "DEBILU!!!")
End If
If a 0 Then
wynik = Sqr(a)
End If
End If
End Sub

Private Sub cyferki1_Click()

cyferki.Show

End Sub

Private Sub na16_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If liczba1 < Empty Then
Dim a As Double, b As String

a = CSng(liczba1)

b = Conversion.Hex(a)

wynik = b
End If
End Sub


Private Sub silnia_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If liczba1 < Empty Then
Dim a As Double
Dim b As Double
a = CSng(liczba1)
b = 1
If a < 0 Then
x = MsgBox("nie ma silni z liczb mniejszych od 0", vbCritical +
vbOKOnly, "DEBILU!!!")
End If
If a = 0 And a <= 170 Then

For x = 1 To a

b = b * x

Next x
End If

If a 170 Then
x = MsgBox("nie policze silni z wiekszej liczby niz 170",
vbCritical + vbOKOnly, "SORRY!!!")
End If
wynik = b
End If
End Sub

Sub Mplus_Click()
If wynik = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If wynik < Empty Then
mpl = wynik
End If
End Sub

Private Sub Mr_Click()
Dim a As Double
a = Val(mpl)
liczba1 = a

End Sub

Private Sub xdoy_Click()
If liczba1 = Empty Or liczba2 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If liczba1 < Empty And liczba2 < Empty Then
Dim a, b As Double
a = CSng(liczba1)
b = CSng(liczba2)
On Error Resume Next
wynik = a ^ b

If wynik 1E+301 Then
x = MsgBox("za duza wartosc", vbCritical + vbOKOnly, "DUPA!!!")
End If

End If
End Sub


second form:

Private Sub C_Click()

UserForm1.liczba2 = Empty
UserForm1.liczba1 = Empty

End Sub

Private Sub CommandButton2_Click()
Dim d As String
Dim a As Double

If wyb1 = True Then
d = UserForm1.liczba1

Dim CzyJest As Variant

CzyJest = InStr(d, ",")
If CzyJest = 0 Then

d = d & ","
Else
x = MsgBox("nie za duzo tych przecinkow?", vbCritical + vbOKOnly,
"COS PRZEGINASZ!!!")
End If

UserForm1.liczba1 = d
End If
If wyb2 = True Then
d = UserForm1.liczba2

CzyJest = InStr(d, ",")
If CzyJest = 0 Then

d = d & ","
Else
x = MsgBox("nie za duzo tych przecinkow?", vbCritical + vbOKOnly,
"COS PRZEGINASZ!!!")
End If

UserForm1.liczba2 = d
End If
End Sub

Private Sub cztery_Click()

Dim d As String
Dim a As Double

If wyb1 = True Then
d = UserForm1.liczba1
d = d & "4"


UserForm1.liczba1 = d
End If
If wyb2 = True Then
d = UserForm1.liczba2
d = d & "4"

UserForm1.liczba2 = d
End If
End Sub

Private Sub dwa_Click()

Dim d As String
Dim a As Double

If wyb1 = True Then
d = UserForm1.liczba1
d = d & "2"

UserForm1.liczba1 = d
End If
If wyb2 = True Then
d = UserForm1.liczba2
d = d & "2"

UserForm1.liczba2 = d
End If
End Sub

Private Sub dziesiec_Click()

Dim d As String
Dim a As Double

If wyb1 = True Then
d = UserForm1.liczba1
d = d & "0"

UserForm1.liczba1 = d
End If
If wyb2 = True Then
d = UserForm1.liczba2
d = d & "0"

UserForm1.liczba2 = d
End If
End Sub

Private Sub dziewiec_Click()

Dim d As String
Dim a As Double

If wyb1 = True Then
d = UserForm1.liczba1
d = d & "9"


UserForm1.liczba1 = d
End If
If wyb2 = True Then
d = UserForm1.liczba2
d = d & "9"

UserForm1.liczba2 = d
End If
End Sub

Private Sub jeden_Click()


Dim d As String
Dim a As Double
If wyb1 = True Then
d = UserForm1.liczba1
d = d & "1"

UserForm1.liczba1 = d
End If
If wyb2 = True Then
d = UserForm1.liczba2
d = d & "1"

UserForm1.liczba2 = d
End If
End Sub


Private Sub osiem_Click()

Dim d As String
Dim a As Double

If wyb1 = True Then
d = UserForm1.liczba1
d = d & "8"

UserForm1.liczba1 = d
End If
If wyb2 = True Then
d = UserForm1.liczba2
d = d & "8"

UserForm1.liczba2 = d
End If
End Sub

Private Sub piec_Click()

Dim d As String
Dim a As Double

If wyb1 = True Then
d = UserForm1.liczba1
d = d & "5"

UserForm1.liczba1 = d
End If
If wyb2 = True Then
d = UserForm1.liczba2
d = d & "5"

UserForm1.liczba2 = d
End If
End Sub

Private Sub siedem_Click()

Dim d As String
Dim a As Double

If wyb1 = True Then
d = UserForm1.liczba1
d = d & "7"

UserForm1.liczba1 = d
End If
If wyb2 = True Then
d = UserForm1.liczba2
d = d & "7"

UserForm1.liczba2 = d
End If
End Sub

Private Sub szesc_Click()

Dim d As String
Dim a As Double

If wyb1 = True Then
d = UserForm1.liczba1
d = d & "6"

UserForm1.liczba1 = d
End If
If wyb2 = True Then
d = UserForm1.liczba2
d = d & "6"

UserForm1.liczba2 = d
End If
End Sub

Private Sub trzy_Click()

Dim d As String
Dim a As Double

If wyb1 = True Then
d = UserForm1.liczba1
d = d & "3"

UserForm1.liczba1 = d
End If
If wyb2 = True Then
d = UserForm1.liczba2
d = d & "3"

UserForm1.liczba2 = d
End If
End Sub



Jim Thomlinson

bad data when i change string to double
 
Let me know how this goes...

Public mpl As Double

Private Sub dodawanie_Click()
Dim b as Double, a As Double 'otherwise b is variant

If liczba1 = Empty Or liczba2 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If

If liczba1 < Empty And liczba2 < Empty Then

msgbox isnumeric(liczba1)
msgbox isnumeric(liczba2)
a = Cdbl(liczba1)
b = Cdbl(liczba2)

wynik = a + b

End If
End Sub

--
HTH...

Jim Thomlinson


" wrote:


first form:

Public mpl As Double

Private Sub dodawanie_Click()
If liczba1 = Empty Or liczba2 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If

If liczba1 < Empty And liczba2 < Empty Then

Dim b, a As Double
a = CSng(liczba1)
b = CSng(liczba2)

wynik = a + b

End If
End Sub

Public Sub odejmowanie_Click()
If liczba1 = Empty Or liczba2 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If

If liczba1 < Empty And liczba2 < Empty Then
Dim a, b As Double
a = CSng(liczba1)
b = CSng(liczba2)

wynik = a - b

End If
End Sub

Private Sub mnozenie_Click()
If liczba1 = Empty Or liczba2 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If

If liczba1 < Empty And liczba2 < Empty Then
Dim a, b As Double
a = CSng(liczba1)
b = CSng(liczba2)

wynik = a * b

End If

End Sub

Private Sub dzielenie_Click()

If liczba1 = Empty Or liczba2 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If

If liczba1 < Empty And liczba2 < Empty Then

Dim a, b As Double
a = CSng(liczba1)
b = CSng(liczba2)

If b = 0 Then
x = MsgBox("nie dzielimy przez 0", vbCritical + vbOKOnly,
"DEBILU!!!")
End If

If b < 0 Then
wynik = a / b
End If
End If
End Sub

Private Sub sin_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If

If liczba1 < Empty Then
Dim a As Double
a = CSng(liczba1)

wynik = Math.sin(a)
End If

End Sub

Private Sub cos_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If

If liczba1 < Empty Then
Dim a As Double
a = CSng(liczba1)

wynik = Math.cos(a)

End If
End Sub

Private Sub tan_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If liczba1 < Empty Then
Dim a As Double
a = CSng(liczba1)

wynik = Math.tan(a)
End If
End Sub


Private Sub ctg_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If liczba1 < Empty Then
Dim a As Double
a = CSng(liczba1)

wynik = 1 / Math.tan(a)
End If
End Sub


Private Sub CommandButton1_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If liczba1 < Empty Then
liczba1 = liczba1 * (-1)
End If
End Sub

Private Sub CommandButton2_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If liczba1 < Empty Then
Dim a As Double
a = CSng(liczba1)
If a <= 0 Then
x = MsgBox("nie pierwiastkujemy liczb mniejszych od 0", vbCritical +
vbOKOnly, "DEBILU!!!")
End If
If a 0 Then
wynik = Sqr(a)
End If
End If
End Sub

Private Sub cyferki1_Click()

cyferki.Show

End Sub

Private Sub na16_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If liczba1 < Empty Then
Dim a As Double, b As String

a = CSng(liczba1)

b = Conversion.Hex(a)

wynik = b
End If
End Sub


Private Sub silnia_Click()
If liczba1 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If liczba1 < Empty Then
Dim a As Double
Dim b As Double
a = CSng(liczba1)
b = 1
If a < 0 Then
x = MsgBox("nie ma silni z liczb mniejszych od 0", vbCritical +
vbOKOnly, "DEBILU!!!")
End If
If a = 0 And a <= 170 Then

For x = 1 To a

b = b * x

Next x
End If

If a 170 Then
x = MsgBox("nie policze silni z wiekszej liczby niz 170",
vbCritical + vbOKOnly, "SORRY!!!")
End If
wynik = b
End If
End Sub

Sub Mplus_Click()
If wynik = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If wynik < Empty Then
mpl = wynik
End If
End Sub

Private Sub Mr_Click()
Dim a As Double
a = Val(mpl)
liczba1 = a

End Sub

Private Sub xdoy_Click()
If liczba1 = Empty Or liczba2 = Empty Then
x = MsgBox("puste pole", vbCritical + vbOKOnly, "Uzupelnij!!!")
End If
If liczba1 < Empty And liczba2 < Empty Then
Dim a, b As Double
a = CSng(liczba1)
b = CSng(liczba2)
On Error Resume Next
wynik = a ^ b

If wynik 1E+301 Then
x = MsgBox("za duza wartosc", vbCritical + vbOKOnly, "DUPA!!!")
End If

End If
End Sub


second form:

Private Sub C_Click()

UserForm1.liczba2 = Empty
UserForm1.liczba1 = Empty

End Sub

Private Sub CommandButton2_Click()
Dim d As String
Dim a As Double

If wyb1 = True Then
d = UserForm1.liczba1

Dim CzyJest As Variant

CzyJest = InStr(d, ",")
If CzyJest = 0 Then

d = d & ","
Else
x = MsgBox("nie za duzo tych przecinkow?", vbCritical + vbOKOnly,
"COS PRZEGINASZ!!!")
End If

UserForm1.liczba1 = d
End If
If wyb2 = True Then
d = UserForm1.liczba2

CzyJest = InStr(d, ",")
If CzyJest = 0 Then

d = d & ","
Else
x = MsgBox("nie za duzo tych przecinkow?", vbCritical + vbOKOnly,
"COS PRZEGINASZ!!!")
End If

UserForm1.liczba2 = d
End If
End Sub

Private Sub cztery_Click()

Dim d As String
Dim a As Double

If wyb1 = True Then
d = UserForm1.liczba1
d = d & "4"


UserForm1.liczba1 = d
End If
If wyb2 = True Then
d = UserForm1.liczba2



All times are GMT +1. The time now is 08:04 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com