| | |
VBA - Excel - sheet,range lookup with variables
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
hi everyone, I am doing a project for college, which involves programming using VBA in Excel. I have most of it sorted and i am having trouble on this 1 part. I need to find out how to use variables to access a sheet and range within the spreadsheet
variable1 = sheets("sheet").range("range1")
variable2 = sheets("sheet").range("range2")
txtBox = sheets(variable1).range("variable2")
this would look in "sheets" find "range1" which has the name of another sheet. then it would look in "range2" which has the name of a range, then these are combined looking in the sheet and range given by values in the spreadsheet.
this seems to be erroneous, does anyone have any suggestions ?
variable1 = sheets("sheet").range("range1")
variable2 = sheets("sheet").range("range2")
txtBox = sheets(variable1).range("variable2")
this would look in "sheets" find "range1" which has the name of another sheet. then it would look in "range2" which has the name of a range, then these are combined looking in the sheet and range given by values in the spreadsheet.
this seems to be erroneous, does anyone have any suggestions ?
•
•
•
•
Originally Posted by bops
hi everyone, I am doing a project for college, which involves programming using VBA in Excel. I have most of it sorted and i am having trouble on this 1 part. I need to find out how to use variables to access a sheet and range within the spreadsheet
variable1 = sheets("sheet").range("range1")
variable2 = sheets("sheet").range("range2")
txtBox = sheets(variable1).range("variable2")
this would look in "sheets" find "range1" which has the name of another sheet. then it would look in "range2" which has the name of a range, then these are combined looking in the sheet and range given by values in the spreadsheet.
this seems to be erroneous, does anyone have any suggestions ?
sheets("sheet").range("range1") returns an object, namely the range named range1 in the worsheet called sheet. So you'd need to do:
Set variable1=sheets("sheet").range("range1")
Set variable2 = sheets("sheet").range("range2")
Secondly,
sheets(variable1).range("variable2")
The sheets object can only be referenced by a sheet name or a sheet index. Not a range which variable1 contains.
Perhaps you could explain the problem you are trying to solve so we can help better.
Mark Nemtsas
Time and Billing Software - Time Tracking Software - Roller Shutters - Roller Blinds -
Baby Books
Time and Billing Software - Time Tracking Software - Roller Shutters - Roller Blinds -
Baby Books
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
It is alright now, i worked on it and it seemed to work, my project is actually more complex than the example i used, but that was simply what the problem was, i have found that it worked, but i had named something wrong. You can use variables as names to work out a given range, since it worked for me
I can explain in more detail what the problem was if you want, just let me know, Thanks for your help anyway
I can explain in more detail what the problem was if you want, just let me know, Thanks for your help anyway
![]() |
Similar Threads
- Retriving data from excel sheet into an asp page (ASP)
- Open Excel Sheet in Vb.net (VB.NET)
- To Fetch EXCEL sheet values thro' ASP page. (Existing Scripts)
- Excel Form - For/Next - Naming Convention Problem (Visual Basic 4 / 5 / 6)
- excel, dynamic name range through web link (Windows Software)
- VBA Date Format (Visual Basic 4 / 5 / 6)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Another problem.
- Next Thread: Need SQL Help (VB6 and Access)
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age append application basic beginner birth bmp calculator cd cells.find click client code college column component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver subroutine table tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





