Results 1 to 19 of 19

Thread: VBA in excel: EASY question for you

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    48

    VBA in excel: EASY question for you

    Here's my question, just scroll to the 'While Loop', and look at:

    distance = Sheets("QMP-Auto").Range("C[range0]").Value

    The C column has values from 0-1500, which i call 'distance'. I want to keep adding .01 to 't60auto' until 'distance' gets above 60, that way i can use the inital value of t60auto when distance > 60.

    I think everything is fine until i get to Range, then it screws up... HELP!

    Thanks!

    -----------

    Sub sixtyauto()

    Dim dt
    Dim rangeadd
    Dim t60
    Dim distance
    Dim range0

    dt = 0.01
    rangeadd = 1
    t60auto = 0
    distance = 0
    range0 = 3

    While distance < 60
    t60auto = t60auto + dt
    distance = Sheets("QMP-Auto").Range("C[range0]").Value
    range0 = range0 + 1
    Wend

    End Sub

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA in excel: EASY question for you

    Welcome to the Forums.

    You have your variable enclosed in the double quotes so its evaluated as a string and not a variable.
    Code:
    distance = Sheets("QMP-Auto").Range("C[" & range0 & "]").Value
    also, you should dim your variables as the needed type and not as Variants (no type specified will be dimmed as a Variant). Use Integer for range0 and Single for t60.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    48

    Re: VBA in excel: EASY question for you

    Holy crap that was fast. Thanks Rob! This place may become my new home for the next few months.

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    48

    still not working

    Still not working... Check it out again please. Thanks!!!! I'm slow, sorry about this...

    --------------

    Sub sixtyauto()

    Dim dt As Single
    Dim rangeadd As Integer
    Dim t60auto As Single
    Dim distance As Single
    Dim range0 As Integer

    dt = 0.01
    rangeadd = 1
    t60auto = 0
    distance = 0
    range0 = 3

    While distance < 60
    t60auto = t60auto + dt
    distance = Sheets("QMP-Auto").Range("C['range0']").Value
    range0 = range0 + 1
    Wend

    End Sub

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA in excel: EASY question for you

    Sorry for the delay, posting

    You used single quotes instead of double quotes like I posted. The single quotes wrapped around with double quotes still makes vba look at it as a complete string and not a variable.


    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    48

    One more try man...

    While distance < 60
    t60auto = t60auto + dt
    distance = Sheets("QMP-Auto").Range("C[" & range0 & "]").Value
    range0 = range0 + 1
    Wend

    THis still didn't work... =/ Any suggestions?

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA in excel: EASY question for you

    Did you change your Dim statements to include As Integer for range0?

    Dim range0 As Integer
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    48

    Re: VBA in excel: EASY question for you

    Yup heres my entire snip:

    Sub sixtyauto()

    Dim dt As Single
    Dim rangeadd As Integer
    Dim t60auto As Single
    Dim distance As Single
    Dim range0 As Integer

    dt = 0.01
    rangeadd = 1
    t60auto = 0
    distance = 0
    range0 = 3

    While distance < 60

    distance = Sheets("QMP-Auto").Range("C[" & range0 & "]").Value
    range0 = range0 + 1
    t60auto = t60auto + dt
    Wend

    End Sub

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA in excel: EASY question for you

    Ok, looks like you have the range parameter incorrect.
    Code:
    distance = Workbooks(1).Sheets("Sheet1").Range("C" & range0).Value
    Code:
    Sub sixtyauto()
    
        Dim dt As Single
        Dim rangeadd As Integer
        Dim t60auto As Single
        Dim distance As Single
        Dim range0 As Integer
        
        dt = 0.01
        rangeadd = 1
        t60auto = 0
        distance = 0
        range0 = 3
        
        While distance < 60
            distance = Sheets("QMP-Auto").Range("C" & range0).Value
            range0 = range0 + 1
            t60auto = t60auto + dt
        Wend
    
    End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    48

    Re: VBA in excel: EASY question for you

    Mismatch Error on the Range line...

    =/

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA in excel: EASY question for you

    Really? It ran fine for me in Excel 2003. What version are you running?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA in excel: EASY question for you

    You could use a CStr conversion on the Integer variable to see f thats it.
    Code:
    distance = Sheets("QMP-Auto").Range("C" & CStr(range0)).Value
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    48

    Re: VBA in excel: EASY question for you

    Excel 2002.... Kinda strange though, I didn't realize there was a 2002 till just now..

    With the CStr version, i get a 'subscript out of range' error. Maybe it's my version??

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA in excel: EASY question for you

    You have it exactly aas I had posted in #9?

    Where is the code located? Behind the sheet or in the ThisWorkbook class?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    48

    Re: VBA in excel: EASY question for you

    In a module...

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA in excel: EASY question for you

    Then you should reference the workbooks collection also in this line for better programming practice. Change the workbook to your workbooks name.
    Code:
    distance = Workbooks("Book1.xls").Sheets("QMP-Auto").Range("C" & range0).Value
    Now to figure out why 2002 is acting different then 2003 lets try placing the entire range identifier into a variable instead.

    Code:
    Sub sixtyauto()
    
        Dim dt As Single
        Dim rangeadd As Integer
        Dim t60auto As Single
        Dim distance As Single
        Dim range0 As Integer
        Dim rangeTemp As String
        dt = 0.01
        rangeadd = 1
        t60auto = 0
        distance = 0
        range0 = 3
        rangeTemp = "C3"
    
        Do While distance < 60
            distance = Workbooks("Book1.xls").Sheets("QMP-Auto").Range(rangeTemp).Value
            If distance = 0 Then Exit Do
            range0 = range0 + 1
            rangeTemp = "C" & CStr(range0)
            t60auto = t60auto + dt
        Loop
    
    End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  17. #17
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: VBA in excel: EASY question for you

    I just looked at it quickly, but might it have something to do with the source data? You have values being read into "distance" type single. Maybe your loop is hitting a string or reaching the end of the column and reading a null value? Both would kick out a mismatch I think.

  18. #18
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA in excel: EASY question for you

    Yes, thats true. Good spot mikey. I assumed that there would be integer type data in the cells.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  19. #19
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: VBA in excel: EASY question for you

    here is how you should write it and should works fine

    VB Code:
    1. distance = Sheets("QMP-Auto").Range("C" & range0 & "").Value
    Last edited by billhuard; Feb 3rd, 2006 at 10:23 AM.

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