473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Excel: Variabel for Sheets?

125 100+
I'm making a macro in Excel.

In my macro i have something like this in many places:

Expand|Select|Wrap|Line Numbers
  1. Sheet1.Range(Col & Row).Value
  2. Sheet2.Range(Col & Row).Value
  3.  
I whant variables for the Sheets so I can change them in one place and don't need to run a replace on them in the hole module.

Something like
Expand|Select|Wrap|Line Numbers
  1. Dim mySheet1 ' as ? _ mySheet1 = "Sheet1"
  2. Dim mySheet2 ' as ? _ mySheet2 = "Sheet2"
  3. mySheet1.Range(Col & Row).Value
  4. mySheet2.Range(Col & Row).Value
  5.  
Tryied some different things but can't get it working, can you do it by using right thig at "as ?" or can you do it any other way?
Sep 3 '08 #1
5 1542
FishVal
2,653 Expert 2GB
Hello.

You may easily get an answer yourself.
Toggle breakpoint anywhere within the scope of the variable.
In VBE immediate window type:

? TypeName(<...variable name...>)

You will get the variable type.

Also you may add variable to Watch window and get plenty of info about it.

Also I strongly recommend you to use Object Browser (F2 in VBE) to view classes available in referenced libraries.

Kind regards,
Valentine
Sep 3 '08 #2
asedt
125 100+
In VBE immediate window type:

? TypeName(<...variable name...>)

You will get the variable type.
Hi

Thanks dident know about that "Interpreter window" before.

I did as you said and got "Worksheet" as answer.

So the declaration is:

Expand|Select|Wrap|Line Numbers
  1. Dim mySheet As Worksheet
  2.  
But then the assignment I don't know how to do, tryied all of this but don't get it working.

Expand|Select|Wrap|Line Numbers
  1. mySheet = ActiveWorkbook.Worksheets(1)
  2. mySheet = ActiveWorkbook.Sheets(1)
  3. mySheet = ActiveWorkbook.Worksheets.Item(1)
  4. mySheet = ActiveWorkbook.Sheets.Item(1)
  5.  
Tryed with <"Sheet1">, <"1"> insted of <1> at index, nothing working. Tryed to find answers in the Object browser and Whatch window but culdn't find anything about assignment of Worksheets.
Sep 4 '08 #3
FishVal
2,653 Expert 2GB
Use "Set" operator to assign a value to object variable.

Set objVar1 = objVar2
Sep 4 '08 #4
asedt
125 100+
Use "Set" operator to assign a value to object variable.

Set objVar1 = objVar2
Now it works, thanks :)

Hmm newer used set when assigning Integers or Strings so i didn't now about that you needed to use it for "object variables".
Sep 4 '08 #5
FishVal
2,653 Expert 2GB
You are welcome.

Regards,
Fish

P.S. BTW there are two operators in Basic to assign value to variable. "Let" is being used for non-object variables and "Set" is used for object ones. "Let" is assumed by default, so usually it is omitted.
Sep 4 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ilan | last post by:
Hi all I need to add data from two Excel sheets (both on the same workbook) to an existing table in my SQL DB. The problem is that each sheet holds different fields for the same record, though...
1
by: javzxp | last post by:
Hi I'd like to use C# to open an existing Excel workbook and save each worksheet it contains into a new Excel file. The name of each new Excel file should be the name of the worksheet copied...
8
by: Sam | last post by:
Hi All, I posted this message few days ago. No one has answered so far. I just would like to know if it is possible to do or not. Even if you can not tell me how to do this, maybe you know which...
1
by: Esmail Bonakarian | last post by:
Greetings all, What is the best way to access specific records in an Excel file? I have an Excel file, I want to randomly and repeatedly (maybe around up to 50 times) draw some rows of data...
0
by: ammu001 | last post by:
Response.ContentType Excel I am trying to export a datagrid into an excel sheet. What it now does is the entire page is saved as an excel sheet in XlHtml file format. I want it in...
4
by: =?Utf-8?B?QnJpYW5ESA==?= | last post by:
Hi Does anyone know of a way (via code behind) to pull a single sheet out of a Excel workbook and convert it to a stand alone html document? Thanks Brian
3
by: Chris | last post by:
I have a python script that is driving Excel and using the win32com module. However, upon program completion there's still an Excel.exe process running in the background that I must terminate...
1
by: afr0ninja | last post by:
Hello! I'm having an issue with the OutputTo function of Access. I have a few queries that I export to excel (by way of the OutputTo function) This works fine. I then take an excel sheet...
1
by: DennisBetten | last post by:
First of all, I need to give some credit to Mahesh Chand for providing me with an excellent basis to export data to excel. What does this code do: As the title says, this code is capable of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.