Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Reffering code to command button on diffrent sheets


Hi

Got a little probleme here :)

I want my command button on "sheet1" to copy things from "sheet2" and
paste it again in "sheet1". Excel only wants to do this when i put the
code in "ThisWorkbook", because if i put the code in sheet1 it doesnt
seem to know sheet2. But when i put my code in "ThisWorkbook" the
command button looks for its code in sheet1, not in "thisWorkbook",
where i just put it.
How can i tell tho command button to look for its code in
"thisWorkbook" ?

hope You can help me out.

Greetings


--
BlonTMamba
------------------------------------------------------------------------
BlonTMamba's Profile: http://www.excelforum.com/member.php...o&userid=30774
View this thread: http://www.excelforum.com/showthread...hreadid=504411

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Reffering code to command button on diffrent sheets

create a macro in a module

something akin to

sub copyFromAtoB()

worksheets(2).range("someRange").copy
worksheets(1).range("anotherRange").paste

end sub

Then instead of using the "comand button" from the control toolbox, use
the "button" button from the forms toolbox. This will now ask you which
macro you want to attach.

Hope this Helps

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Reffering code to command button on diffrent sheets

This works fine for me

Private Sub CommandButton1_Click()
Me.Range("A1").Value = Worksheets("Sheet2").Range("A1").Value
End Sub


--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)

"BlonTMamba" wrote
in message ...

Hi

Got a little probleme here :)

I want my command button on "sheet1" to copy things from "sheet2" and
paste it again in "sheet1". Excel only wants to do this when i put the
code in "ThisWorkbook", because if i put the code in sheet1 it doesnt
seem to know sheet2. But when i put my code in "ThisWorkbook" the
command button looks for its code in sheet1, not in "thisWorkbook",
where i just put it.
How can i tell tho command button to look for its code in
"thisWorkbook" ?

hope You can help me out.

Greetings


--
BlonTMamba
------------------------------------------------------------------------
BlonTMamba's Profile:

http://www.excelforum.com/member.php...o&userid=30774
View this thread: http://www.excelforum.com/showthread...hreadid=504411



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Reffering code to command button on diffrent sheets


hey

thanks all for replying!!!

alltough that was not exacly what i meant :) i learnd some new things
tho

and I just found the function i meant :) i will show you:

i put the routine for command button 2 (wich i called CB2) in
ThisWorkbook.
than i put the following code for the command button in sheet1:

Private Sub CommandButton2_Click()
Call [ThisWorkbook].CB2
End Sub

that was probally to easy for you to think of i just started
working with excel like this :)

thanks again

greets


--
BlonTMamba
------------------------------------------------------------------------
BlonTMamba's Profile: http://www.excelforum.com/member.php...o&userid=30774
View this thread: http://www.excelforum.com/showthread...hreadid=504411

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Reffering code to command button on diffrent sheets

Think Bob showed you how to properly structure your code and address Sheet2
from the Sheet1 code module so you don't have to pursue the "Bandaid"
approach you have come up with.



--
Regards,
Tom Ogilvy


"BlonTMamba" wrote
in message ...

hey

thanks all for replying!!!

alltough that was not exacly what i meant :) i learnd some new things
tho

and I just found the function i meant :) i will show you:

i put the routine for command button 2 (wich i called CB2) in
ThisWorkbook.
than i put the following code for the command button in sheet1:

Private Sub CommandButton2_Click()
Call [ThisWorkbook].CB2
End Sub

that was probally to easy for you to think of i just started
working with excel like this :)

thanks again

greets


--
BlonTMamba
------------------------------------------------------------------------
BlonTMamba's Profile:

http://www.excelforum.com/member.php...o&userid=30774
View this thread: http://www.excelforum.com/showthread...hreadid=504411





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Reffering code to command button on diffrent sheets


oowww....

hmm okay. well how does that one exactly work then? and why is it more
convenient than mine?

Do u put all the code in sheet 1? or just the one bob showed me?

Greets


--
BlonTMamba
------------------------------------------------------------------------
BlonTMamba's Profile: http://www.excelforum.com/member.php...o&userid=30774
View this thread: http://www.excelforum.com/showthread...hreadid=504411

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Reffering code to command button on diffrent sheets

If you want to address another sheet from a sheet module, you qualify it
with a sheet qualifier

All the code goes in the click event of the command button. You always have
the option to put common functions and common subroutines in a General
module (not the ThisWorkbook Module) so they can be called from anywhere
without special qualification.

The problem you were having is that in a sheet module, an unqualfied
reference to a range implicitely is a reference to the range in the
worksheet that contains the code. You solved this by moving it to the
thisworkbook module where this implicit qualification doesn't occur.
Another option, and probably preferred vice the one you chose, would have
been to move it to a general module where the implicit qualification also
doesn't occur. But the correct solution is to write the code with proper
qualifications and place it in the action event for the control that
triggers it. This has the side affect of being faster if you avoid
selecting and so forth.

Class modules such as thisworkbook, sheet modules and userform modules
should be reserved to contain code associated with events for those objects.
Organization is good practice, supports maintaining code and reduces
unexpected code behavior.

In the end, how you write your code is up to you and your employer, however.

--
Regards,
Tom Ogilvy

"BlonTMamba" wrote
in message ...

oowww....

hmm okay. well how does that one exactly work then? and why is it more
convenient than mine?

Do u put all the code in sheet 1? or just the one bob showed me?

Greets


--
BlonTMamba
------------------------------------------------------------------------
BlonTMamba's Profile:

http://www.excelforum.com/member.php...o&userid=30774
View this thread: http://www.excelforum.com/showthread...hreadid=504411



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
command button code dummy Excel Discussion (Misc queries) 2 December 1st 09 02:57 PM
VBA code behind command button [email protected] Excel Worksheet Functions 1 March 22nd 06 08:13 PM
Command button and linking with other sheets in the workbooks Ken Vo Excel Discussion (Misc queries) 2 January 17th 06 09:30 PM
Command Button VBA code Dave Peterson Excel Discussion (Misc queries) 2 January 25th 05 11:28 PM
Create Command Button from Code Bruce B[_2_] Excel Programming 0 July 14th 03 02:01 PM


All times are GMT +1. The time now is 04:03 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"