Og så har jeg også lige et andet lille problem med koden.
Hvis man indtaster andet end tal, altså feks bogstaver så tager error handler'en fejlen.
Men hvis man indtaster bogstaver igen, så får jeg fejl. Jeg skal på en måde have slettet det der er i Catch_Error: Jeg har fundet noget der hedder Err.Clear, men kan ikke rigtig få det til at virke
Håber der er nogen der ved hvordan jeg får den til og virke altså Err.Clear
Her er hele koden
Sub Main() ' ' Ohm Makro ' Makro oprettet 02-08-2005 af Alex Sleiborg ' Dim Nummer As Integer 'Count nummer Nummer = 1
Start: On Error GoTo Catch_Error:
Dim U As String 'U = Spænding som måles i volt Dim I As String 'I = Strøm som måles i Amperer Dim R As String 'R = Modstand som måles i ohm Dim P As String 'P = Effekt som måles i watt
Dim Resultat As Double 'Resultatet af de forskellige formeller
Dim Formel As String 'Beskriver hvilken formel der bliver brugt Dim Tabel1 As String 'Beskriver om det er Amperer, Volt, Watt eller Ohm Dim Tabel2 As String 'Beskriver om det er Amperer, Volt, Watt eller Ohm Dim Beskriv As String 'A, V, Ohm, W Dim X As String 'Bliver brugt for og kunne flytte resultatet ned i tabellen Dim Y As String 'Bliver brugt for og kunne flytte resultatet ned i tabellen Dim Udregning As String 'Beskriver hva det er man udregner
U = "" ' Må ikke indeholde noget, ellers bliver der fejl hvis man laver flere beregninger I = "" R = "" P = ""
Dim A As Byte Færdig: Do A = InputBox("Velkommen til ohm Lommeregneren" & vbCrLf & "Vælg hvad du vil have regnet ud" & vbCrLf & "1. Effekt" & vbCrLf & "2. Strøm" & vbCrLf & "3. Spænding" & vbCrLf & "4. Modstand" & vbCrLf & "5. Afslut programmet") If A > 5 Or A <= 0 Then MsgBox " Du skal skrive et tal imellem 1-5!", vbCritical Loop While A > 5 Or A <= 0
On Error GoTo Start:
Select Case A
'Effekten "Watt" Formellen (U*I) (I²*R) (U²/R) Case 1 U = InputBox("Indtast Spændingen. Hvis du ikke har spændingen, indtast ikke noget") Udregning = "effekt" If U = "" Then Do R = InputBox("Indtast Modstand") I = InputBox("Indtast Strømmen") Loop While R = "" Or I = "" If Val(R) = 0 Or Val(I) = 0 Then On Error GoTo Catch_Error:
Resultat = (I * I) * R
Formel = "I² * R" Tabel1 = "Amperer" Tabel2 = "Ohm" Beskriv = "W" X = I Y = R
GoTo Tabel: End If
I = InputBox("Indtast Strømmen. Hvis du ikke har strømmen, indtast ikke noget") If I = "" Then Do R = InputBox("Indtast Modstand") Loop While R = "" Or U = "" If Val(R) = 0 Or Val(U) = 0 Then On Error GoTo Catch_Error:
Resultat = (U * U) / R
Formel = "U² / R" Tabel1 = "Volt" Tabel2 = "Ohm" Beskriv = "W" X = U Y = R
GoTo Tabel: End If
If Val(U) = 0 Or Val(I) = 0 Then On Error GoTo Catch_Error: Resultat = U * I
Formel = "U * I" Tabel1 = "Volt" Tabel2 = "Amperer" Beskriv = "W" X = U Y = I
GoTo Tabel:
'Strøm "Amperer" Formellen (U/R) (P/U) (Kvadratroden af P/R) Case 2 U = InputBox("Indtast Spændingen. Hvis du ikke har spændingen, indtast ikke noget") Udregning = "strøm" If U = "" Then Do R = InputBox("Indtast Modstand") P = InputBox("Indtast Effekten") If Val(R) = 0 Or Val(P) = 0 Then On Error GoTo Catch_Error: Loop While R = "" Or P = ""
Resultat = Sqr(P / R)
Formel = "Kvadrat af P/R" Tabel1 = "Watt" Tabel2 = "Ohm" Beskriv = "A" X = P Y = R
GoTo Tabel: End If
P = InputBox("Indtast Effekten. Hvis du ikke har effekten, indtast ikke noget") If P = "" Then Do R = InputBox("Indtast Modstand") If Val(U) = 0 Or Val(R) = 0 Then On Error GoTo Catch_Error: Loop While U = "" Or R = ""
Resultat = U / R
Formel = "U / R" Tabel1 = "Volt" Tabel2 = "Ohm" Beskriv = "A" X = U Y = R
GoTo Tabel: End If
If Val(P) = 0 Or Val(U) = 0 Then On Error GoTo Catch_Error: Resultat = P / U
Formel = "P / U" Tabel1 = "Watt" Tabel2 = "Volt" Beskriv = "A" X = P Y = U
GoTo Tabel:
'Spænding "Volt" Formellen (Kvadratroden af P*R) (P/I) (R*I) Case 3 I = InputBox("Indtast Strømmen. Hvis du ikke har strømmen, indtast ikke noget") Udregning = "spænding" If I = "" Then Do R = InputBox("Indtast Modstand") P = InputBox("Indtast Effekten") If Val(R) = 0 Or Val(P) = 0 Then On Error GoTo Catch_Error: Loop While R = "" Or P = ""
Formel = "Kvadrat af P*R" Tabel1 = "Watt" Tabel2 = "Ohm" Beskriv = "V" X = P Y = R
GoTo Tabel: End If
P = InputBox("Indtast Effekten. Hvis du ikke har effekten, indtast ikke noget") If P = "" Then Do R = InputBox("Indtast Modstand") If Val(R) = 0 Or Val(I) = 0 Then On Error GoTo Catch_Error: Loop While R = "" Or I = ""
'R_ = CDbl(strR) 'I_ = CDbl(strI) Resultat = R * I
Formel = "R * I" Tabel1 = "Ohm" Tabel2 = "Amperer" Beskriv = "V" X = R Y = I
GoTo Tabel: End If
If Val(P) = 0 Or Val(I) = 0 Then On Error GoTo Catch_Error: Resultat = P / I
Formel = "P / I" Tabel1 = "Watt" Tabel2 = "Amperer" Beskriv = "V" X = P Y = I
GoTo Tabel:
'Modstand "Ohm" (P/I²) (U²/P) (U/I) Case 4 U = InputBox("Indtast Spændingen. Hvis du ikke har spændingen, indtast ikke noget") Udregning = "modstand" If U = "" Then Do I = InputBox("Indtast Strømmen") P = InputBox("Indtast Effekten") If Val(P) = 0 Or Val(I) = 0 Then On Error GoTo Catch_Error: Loop While P = "" Or I = ""
Resultat = P / (I * I)
Formel = "P/I²" Tabel1 = "Watt" Tabel2 = "Amperer" Beskriv = "O" X = P Y = I
GoTo Tabel: End If
P = InputBox("Indtast Effekten. Hvis du ikke har effekten, indtast ikke noget") If P = "" Then Do I = InputBox("Indtast Strømmen") If Val(U) = 0 Or Val(I) = 0 Then On Error GoTo Catch_Error: Loop While U = "" Or I = ""
Resultat = U / I
Formel = "U / I" Tabel1 = "Volt" Tabel2 = "Amperer" Beskriv = "O" X = U Y = I
GoTo Tabel: End If
If Val(U) = 0 Or Val(P) = 0 Then On Error GoTo Catch_Error: Resultat = (U * U) / P
Formel = "U²/P" Tabel1 = "Volt" Tabel2 = "Watt" Beskriv = "O" X = U Y = P
Har fundet ud af hvordan man får cancel til og virke
Sub InputBoxNew() Dim Temp$
Temp = InputBox("Enter something here:", "Inputbox") If StrPtr(Temp) = 0 Then MsgBox "You pressed Cancel!" 'Option 1 Else If Temp = "" Then MsgBox "You entered nothing and pressed OK" ' Option 2 Else MsgBox Temp 'Option 3 End If End If End Sub
'denne kommando betyder at du ikke får fejlmeldinger on error resume next
'hvis du vil se fejlen kan du bruge efterfølgende linie if err.number > 0 then msgbox("Fejl: " & Err.Description)
'denne linie slår fejlmelding til igen on error goto 0
'denne linie nulstiller fejltælleren err.clear
Et lille eksempel: prøv at indsætte tal eller bogstaver i inputboxen
option explicit dim x on error resume next x = inputbox("Indtast tal som skal ganges med 5") msgbox (x*5) if err.number > 0 then msgbox("Fejl: " & Err.Description) err.clear on error goto 0
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.