Results 1 to 22 of 22

Thread: Not working "If bigger then.."

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Question Not working "If bigger then.."

    Hello,

    I am starting with Visual Basic and I don´t know , why my programm doesn´t working.

    Can you help me, please??

    Code:

    https://pastebin.com/MeAw7Jtn

    Thank you very much !

  2. #2
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Not working "If bigger then.."

    where are you calling Sub pocitac() from?
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Re: Not working "If bigger then.."

    Quote Originally Posted by Goggy View Post
    where are you calling Sub pocitac() from?
    They said me : Every programm star with "Sub name_programm () "
    so I write it

  4. #4
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Not working "If bigger then.."

    If your developing in excel,

    Try this. Go to the developers tab, click macro's. Select your pocitac subroutine and run it.
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  5. #5
    gibra
    Guest

    Re: Not working "If bigger then.."

    You should post code HERE, not in external server with a lot of advertising.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: Not working "If bigger then.."

    I agree. Press the # button and paste the code between the resulting tags. Lots of people won't even go looking at a post to a different site.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Re: Not working "If bigger then.."

    Ok, here it is
    I do not know, which type use..
    Code:
    Sub pocitac()
     
    'Athletic calculator
    sto = InputBox("Zadejte čas na 100m (formát času např.: 9.46)", "Čas za 100m") 'Sprint for 100m
    max100 = 17.15
     
     
    If sto > max100 Then
                        score100 = 0#   'Přiřazení bodové hodnoty. Příkaz FoundCell.Row zjistí na kolikítém řádku se čas nachazí a sloupec je pořád stejný
                      Sheets("List2").Select                   'Aktivuje List2
                      ActiveSheet.Cells(1, 1).Value = "100 m " 'Hlavička
                      ActiveSheet.Cells(2, 1).Value = score100 ' Zapíše bodový výsledek na danou pozici
                      MsgBox "Bodová hodnota  za čas: " & sto & "  je  " & score100, , "Bodová hodnota za 100m"
                Else
                       
                           For I = 1 To 1402   ' řádek 1 až 1402
                             rozsah = "B" & I    ' Sloupec B řádek 1 - 1402
                                     With Range(rozsah) 'Vyhledání zadané hodnoty dveste ve sloupci B a řádku 1 až 1402
                                         Set FoundCell = .Cells.Find(what:=sto, _
                                                           after:=.Cells(.Cells.Count), _
                                                           LookIn:=xlFormulas, _
                                                           Lookat:=xlPart, _
                                                           searchorder:=xlByRows, _
                                                           searchdirection:=xlNext, _
                                                           MatchCase:=False)
                                       End With
                                   If FoundCell Is Nothing Then
                                                    'posunout o jednu bunku dolu??
                 Else
       
                        score100 = Cells(FoundCell.Row, 1).Value 'Přiřazení bodové hodnoty. Příkaz FoundCell.Row zjistí na kolikítém řádku se čas nachazí a sloupec je pořád stejný
                      Sheets("List2").Select                   'Aktivuje List2
                      ActiveSheet.Cells(1, 1).Value = "100 m " 'Hlavička
                      ActiveSheet.Cells(2, 1).Value = score100 ' Zapíše bodový výsledek na danou pozici
                      MsgBox "Bodová hodnota  za čas: " & sto & "  je  " & score100, , "Bodová hodnota za 100m" ' Vypsání bodů
              End If
              Next I
           
    End If
     
    End Sub
    Last edited by Shaggy Hiker; Dec 18th, 2017 at 12:43 PM. Reason: Added CODE tags.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: Not working "If bigger then.."

    I believe that the use of ActiveSheet means that this is VBA, so I have moved the thread to Office Development.
    My usual boring signature: Nothing

  9. #9
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Not working "If bigger then.."

    Did you try to run it from the developers tab, like i suggested?
    If so , could you tell the results?
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  10. #10

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Re: Not working "If bigger then.."

    Run-time error 13
    Type mismatch

  11. #11

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Re: Not working "If bigger then.."

    I try this:
    Code:
    Sub pocitac()
    Dim sto As String
    Dim stomax as Double
    
    sto = InputBox("Zadejte čas na 100m (formát času např.: 9.95)", "Čas za 100m") ' Round = vstup je číslo a zaokrouhluje se na 2 desetiné místa
    
    stomax=sto
    If stomax > 16.79 Then
    But it still doesn´t work

  12. #12
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Not working "If bigger then.."

    Ah ok

    In what line does this error occure?
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  13. #13
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Not working "If bigger then.."

    Does a sheet called "List2" exist in you excel workbook?
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  14. #14

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Re: Not working "If bigger then.."

    I dont know I write for example 22.25 into a InputBox, press Enter and immediately it shows a Error.. So Debug or Add watch don´t help me.

    Yes, "List2" Exist

  15. #15
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Not working "If bigger then.."

    Ok i have added this to the top of the routine

    Code:
    Dim InputAsDouble As Double
    and changed the beginning like so:

    Code:
    'Athletic calculator
    sto = InputBox("Zadejte cas na 100m (formát casu napr.: 9.46)", "Cas za 100m") 'Sprint for 100m
    
    InputAsDouble = CDbl(sto)
    
    max100 = 17.15
     
     
    If InputAsDouble > max100 Then
    atleast now your not compairing a string with an numeric value.

    this seems to be working, aldo i can't test all, because i have no idea of what it should be doing....
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  16. #16
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Not working "If bigger then.."

    P.s

    Always declare your variable's
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  17. #17

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Re: Not working "If bigger then.."

    I try it, but still same error.

    Code:
    Sub pocitac()
    
    Dim InputAsDouble As Double
    
    sto = InputBox("Zadejte cas na 100m (formát casu napr.: 9.46)", "Cas za 100m") 'Sprint for 100m
    
    InputAsDouble = CDbl(sto)
     
        If InputAsDouble > 16.79 Then
    
            score100 = 0
            Sheets("List2").Select    'List2 = Sheet2               'Aktivuje List2
            ActiveSheet.Cells(1, 1).Value = "100 m " 'Hlavička
            ActiveSheet.Cells(2, 1).Value = score100 ' Zapíše bodový výsledek na danou pozici
            MsgBox "Bodová hodnota  za čas: " & sto & "  je  " & score100, , "Bodová hodnota za 100m" ' Vypsání bodů
        
        End If
    
    End Sub

  18. #18
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Not working "If bigger then.."

    ok not sure what I'm NOT seeing, because that routine works for me...

    p.s

    Aldo i do suspect that
    Code:
    ActiveSheet.Cells(2, 1).Value = score100
    should be

    Code:
    ActiveSheet.Cells(2, 1).Value = InputAsDouble
    Last edited by Goggy; Dec 18th, 2017 at 01:39 PM.
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  19. #19

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Re: Not working "If bigger then.."

    It part of my code. Here is full:

    Code:
    Sub pocitac()
    
    Dim InputAsDouble As Double
    
    sto = InputBox("Zadejte cas na 100m (formát casu napr.: 9.46)", "Cas za 100m") 'Sprint for 100m
    
    InputAsDouble = CDbl(sto)
     
        If InputAsDouble > 16.79 Then
                        score100 = 0
                        Sheets("List2").Select                   'Aktivuje List2
                        ActiveSheet.Cells(1, 1).Value = "100 m " 'Hlavička
                        ActiveSheet.Cells(2, 1).Value = score100 ' Zapíše bodový výsledek na danou pozici
                        MsgBox "Závodník byl diskvalifikován nebo překročil maxímálná čas. Počet bodů:  " & score100, , "Bodová hodnota za 100m" ' Vypsání bodů
                                
                    Else
                               
    
        For I = 1 To 1402   ' řádek 1 až 1402
            rozsah = "B" & I    ' Sloupec B řádek 1 - 1402
                    With Range(rozsah) 'Vyhledání zadané hodnoty dveste ve sloupci B a řádku 1 až 1402
                        Set FoundCell = .Cells.Find(what:=sto, _
                                        after:=.Cells(.Cells.Count), _
                                        LookIn:=xlFormulas, _
                                        Lookat:=xlPart, _
                                        searchorder:=xlByRows, _
                                        searchdirection:=xlNext, _
                                        MatchCase:=False)
                    End With
                If FoundCell Is Nothing Then
                                 'posunout o jednu bunku dolu??
                Else
    
                        score100 = Cells(FoundCell.Row, 1).Value 'Přiřazení bodové hodnoty. Příkaz FoundCell.Row zjistí na kolikítém řádku se čas nachazí a sloupec je pořád stejný
                        Sheets("List2").Select                   'Aktivuje List2
                        ActiveSheet.Cells(1, 1).Value = "100 m " 'Hlavička
                        ActiveSheet.Cells(2, 1).Value = score100 ' Zapíše bodový výsledek na danou pozici
                        MsgBox "Bodová hodnota  za čas: " & sto & "  je  " & score100, , "Bodová hodnota za 100m" ' Vypsání bodů
                End If
              Next I
           
    End If
    
    End Sub

  20. #20
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Not working "If bigger then.."

    Try it like so

    Code:
    Sub pocitac()
    
    Dim InputAsDouble As Double
    Dim Sto As String
    Dim FoundCell As Excel.Range
    Dim rozsah As String
    Dim score100 As Variant
    
    Sto = InputBox("Zadejte cas na 100m (formát casu napr.: 9.46)", "Cas za 100m") 'Sprint for 100m
    
    InputAsDouble = CDbl(Sto)
     
        If InputAsDouble > 16.79 Then
                        Sheets("List2").Select                   'Aktivuje List2
                        ActiveSheet.Cells(1, 1).Value = "100 m " 'Hlavicka
                        ActiveSheet.Cells(2, 1).Value = InputAsDouble ' Zapíše bodový výsledek na danou pozici
                        MsgBox "Závodník byl diskvalifikován nebo prekrocil maxímálná cas. Pocet bodu:  " & CStr(InputAsDouble), , "Bodová hodnota za 100m" ' Vypsání bodu
                                
                    Else
                               
    
        For I = 1 To 1402   ' rádek 1 až 1402
            rozsah = "B" & I    ' Sloupec B rádek 1 - 1402
                    With Range(rozsah) 'Vyhledání zadané hodnoty dveste ve sloupci B a rádku 1 až 1402
                        Set FoundCell = .Cells.Find(what:=Sto, _
                                        after:=.Cells(.Cells.Count), _
                                        LookIn:=xlFormulas, _
                                        Lookat:=xlPart, _
                                        searchorder:=xlByRows, _
                                        searchdirection:=xlNext, _
                                        MatchCase:=False)
                    End With
                If FoundCell Is Nothing Then
                                 'posunout o jednu bunku dolu??
                Else
    
                        score100 = Cells(FoundCell.Row, 1).Value 'Prirazení bodové hodnoty. Príkaz FoundCell.Row zjistí na kolikítém rádku se cas nachazí a sloupec je porád stejný
                        Sheets("List2").Select                   'Aktivuje List2
                        ActiveSheet.Cells(1, 1).Value = "100 m " 'Hlavicka
                        ActiveSheet.Cells(2, 1).Value = score100 ' Zapíše bodový výsledek na danou pozici
                        MsgBox "Bodová hodnota  za cas: " & Sto & "  je  " & score100, , "Bodová hodnota za 100m" ' Vypsání bodu
                End If
              Next I
           
    End If
    
    End Sub
    let me know if it worked out
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  21. #21

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Re: Not working "If bigger then.."

    Still not

    It ends on this:

    InputAsDouble = CDbl(Sto)

  22. #22
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Not working "If bigger then.."

    not sure this will work... but there is an immediates window (ctrl+g) and the code window.
    in the code window on left hand side there should be a gutter. click in it, along the line that errors. a ball should appear
    in the immediates window type:
    Code:
    pocitac
    Your code should run up to the marked line.
    back in the immediates window type:
    Code:
    ?Sto
    Does it return a value on the next line and does that value look like a number ?
    If it looks like a string, you probably cannot convert it to a number... hence the error

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width