473,406 Members | 2,343 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,406 software developers and data experts.

Clicking buttons from other sheets (Excel)

TheServant
1,168 Expert 1GB
Hi guys,
Almast a virgin VB coder (~7days) but I have a question on how functions/subs are referenced. I have a button on each of my worksheets which does the same to all of them, but obviously with the different data supplied. I also have a "Total" sheet which sums up the results.

When I update a global variable in the Total sheet I want it to re-do all the calcs on each sheet. I am sure there is a way for a global function or something, but for now: How do I click/activate a button function from another sheet? This is what I ahev so far and only the first line works...

Expand|Select|Wrap|Line Numbers
  1. Private Sub total_Click()
  2.     Worksheets("Sheet1").Activate
  3.     Worksheets("Sheet1").button1()
  4. End Sub
Hope it makes sense, thanks for your help.
Jul 10 '08 #1
4 1983
MikeTheBike
639 Expert 512MB
Hi

Probably the easiest way is to have all your code in a separate code module (not the sheet object module) something like this
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Sub TotalSheetButtonSub()
  4.     Sheets("Sheet1").Activate
  5.     Sheet1ButtonSub
  6.     Sheets("Sheet2").Activate
  7.     Sheet2ButtonSub
  8.     Sheets("Total Sheet").Activate
  9. End Sub
  10. Sub Sheet1ButtonSub()
  11.     MsgBox "Sheet1 Procedure"
  12. End Sub
  13. Sub Sheet2ButtonSub()
  14.     MsgBox "Sheet2 Procedure"
  15. End Sub
You do not say which type of button you are using (Forms or Controls Toolbox Toolbar) but the code in a separate code module is avaiable to both types.


I assume you need to active each sheet because the code in each sub refers to the active sheet? If so, then this is not necessary if your code referes to a reference of the sheet in question. This is paticulary useful if the code is the same for each sheet processed as you can pass the sheet reference as an argument to the Sub and only have one instance of the code to mantain instead of multile copies of the same code.

For instance
Expand|Select|Wrap|Line Numbers
  1. Sub TotalSheetButtonSub()
  2.     SheetButtonSub Sheets("Sheet1")
  3.     SheetButtonSub Sheets("Sheet2")
  4. End Sub
  5. Sub SheetButtonSub(ByRef Sht As Worksheet)
  6.     With Sht
  7.         MsgBox "This sheet Name is " & .Name
  8.     End With
  9. End Sub
Just some thoughts and ideas, perhaps for the (not too distant!) future?

MTB
Jul 11 '08 #2
TheServant
1,168 Expert 1GB
Hi

Probably the easiest way is to have all your code in a separate code module (not the sheet object module) something like this
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Sub TotalSheetButtonSub()
  4.     Sheets("Sheet1").Activate
  5.     Sheet1ButtonSub
  6.     Sheets("Sheet2").Activate
  7.     Sheet2ButtonSub
  8.     Sheets("Total Sheet").Activate
  9. End Sub
  10. Sub Sheet1ButtonSub()
  11.     MsgBox "Sheet1 Procedure"
  12. End Sub
  13. Sub Sheet2ButtonSub()
  14.     MsgBox "Sheet2 Procedure"
  15. End Sub
You do not say which type of button you are using (Forms or Controls Toolbox Toolbar) but the code in a separate code module is avaiable to both types.


I assume you need to active each sheet because the code in each sub refers to the active sheet? If so, then this is not necessary if your code referes to a reference of the sheet in question. This is paticulary useful if the code is the same for each sheet processed as you can pass the sheet reference as an argument to the Sub and only have one instance of the code to mantain instead of multile copies of the same code.

For instance
Expand|Select|Wrap|Line Numbers
  1. Sub TotalSheetButtonSub()
  2.     SheetButtonSub Sheets("Sheet1")
  3.     SheetButtonSub Sheets("Sheet2")
  4. End Sub
  5. Sub SheetButtonSub(ByRef Sht As Worksheet)
  6.     With Sht
  7.         MsgBox "This sheet Name is " & .Name
  8.     End With
  9. End Sub
Just some thoughts and ideas, perhaps for the (not too distant!) future?

MTB

Fanstastic, thanks a lot for your reply. Only one thing: How do I tie a module to a control box button? But the module works exactly how I need it too so already 90% there!
Jul 14 '08 #3
MikeTheBike
639 Expert 512MB
Fanstastic, thanks a lot for your reply. Only one thing: How do I tie a module to a control box button? But the module works exactly how I need it too so already 90% there!
Hi

If you mean a 'Control Toolbox' button, then, based on my previous example, all you do in the 'CommandButton_Click' event is put

'TotalSheetButtonSub' to run all procedures or

'Sheet1ButtonSub' to run the specific sheet procedure.


??

MTB
Jul 14 '08 #4
TheServant
1,168 Expert 1GB
Hi

If you mean a 'Control Toolbox' button, then, based on my previous example, all you do in the 'CommandButton_Click' event is put

'TotalSheetButtonSub' to run all procedures or

'Sheet1ButtonSub' to run the specific sheet procedure.


??

MTB
Yup, that was it, thanks for al your help Mike, problem solved and lesson learnt!
Jul 16 '08 #5

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...
13
by: Botao | last post by:
Hi, Every Guru, I'd like to put a button on a page. When clicking the button, the table below it gets selected so the user can do Ctrl C to copy the entire table without using the mouse to...
2
by: PrinStation | last post by:
I am looking to import data from Excel to Access. The data is set up in one Excel file, but on 12 (monthly) sheets. After importing the first month/sheet (which I did successfully), I was looking...
2
by: Osiris Sawiris | last post by:
I inherited an Inventory control application that updates the stock from the branches at the end of each day. We receive the stock transactions via e-mail attachments (Excel Sheets). Those...
4
by: sunilkeswani | last post by:
I need help with exporting data from 2 access tables, into 2 existing spreadsheets in a single Excel file. Currently, I am using this code: DoCmd.TransferSpreadsheet acExport, 8, "Table1",...
4
by: Hitesh | last post by:
Hi, I have three datagrid control on my aspx page and one export to excel button, i want to export all the 3 datagrids contents in one excel file. how can i achive that? -- Thanks Hitesh
1
by: VK | last post by:
Hey, I have some nested datagrids, which I would like to export to a excel. Exporting is not a problem - however I would like to export the nested data into different sheets on the same excel...
2
by: TJ | last post by:
Hi, Basically, I want to create excel file on the fly, then force users to download it without using automation. Here is one of ways.. System.IO.StringWriter sw = new...
3
by: stuart79 | last post by:
I have created a Form for a group of 30 - 40 users. My users also need to access other excel spread sheets while the form is running. Currently when we try to do this we cannot open other excel...
1
by: dwmaillist | last post by:
Hello, I am interested in extracting the information from an excel workbook with many sheets in it and outputting a csv like text file. Since I want a single file with all information for all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.