|
-
Feb 2nd, 2006, 11:05 PM
#1
Thread Starter
Member
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
-
Feb 2nd, 2006, 11:10 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 2nd, 2006, 11:14 PM
#3
Thread Starter
Member
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.
-
Feb 2nd, 2006, 11:22 PM
#4
Thread Starter
Member
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
-
Feb 2nd, 2006, 11:24 PM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 2nd, 2006, 11:28 PM
#6
Thread Starter
Member
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?
-
Feb 2nd, 2006, 11:31 PM
#7
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 2nd, 2006, 11:33 PM
#8
Thread Starter
Member
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
-
Feb 2nd, 2006, 11:38 PM
#9
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 2nd, 2006, 11:56 PM
#10
Thread Starter
Member
Re: VBA in excel: EASY question for you
Mismatch Error on the Range line...
=/
-
Feb 2nd, 2006, 11:58 PM
#11
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 3rd, 2006, 12:01 AM
#12
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 3rd, 2006, 12:06 AM
#13
Thread Starter
Member
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??
-
Feb 3rd, 2006, 12:16 AM
#14
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 3rd, 2006, 12:25 AM
#15
Thread Starter
Member
Re: VBA in excel: EASY question for you
-
Feb 3rd, 2006, 01:44 AM
#16
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 3rd, 2006, 09:24 AM
#17
Lively Member
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.
-
Feb 3rd, 2006, 09:29 AM
#18
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 3rd, 2006, 10:16 AM
#19
Addicted Member
Re: VBA in excel: EASY question for you
here is how you should write it and should works fine
VB Code:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|