Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default macro code doesnt work in command button

Hi All, this is a weird one. Any help will be appreciated.

I have a macro which works perfectly. It runs from sheet2, make
changes on sheet1 then finishes on bach sheet2. Which is what I want
It works fine when I run it as a macro, however when I copy and past
the EXACT code into a command button and click the button I get a
error!?!?

Here is the code...

Sub temp()
Sheets("sheet1").Select
Range("AF4").Select
Selection.End(xlToLeft).Select
ActiveCell.Columns("A:A").EntireColumn.Select
Selection.Copy
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
ActiveSheet.Paste
ActiveCell.Offset(12, 2).Range("A1").Select
Sheets("sheet2").Select
ActiveCell.Select
End Sub

Here is the error...

Runtime error '1004'
Select method of range class failed

the line that is highlighted when I run the debugger is the 2nd line
Range("AF4").Select.


CHEER

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default macro code doesnt work in command button

Put the below in a general module
Sub temp()
Sheets("sheet1").Select
Range("AF4").Select
Selection.End(xlToLeft).Select
ActiveCell.Columns("A:A").EntireColumn.Select
Selection.Copy
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
ActiveSheet.Paste
ActiveCell.Offset(12, 2).Range("A1").Select
Sheets("sheet2").Select
ActiveCell.Select
End Sub

Modify your commandbutton code to this:

Private Sub Commandbutton1_Click
Temp
End Sub


The problem is that unqualifed references in a sheet module refer to that
sheet. So Range("AF4").Select refers to the sheet containing the code, but
Sheet1 is active and you can not select on the sheet containing the code.
This behavior is different than in a general module where the unqualified
range refers to the active sheet.
--
Regards,
Tom Ogilvy


"The Grinch " wrote in message
...
Hi All, this is a weird one. Any help will be appreciated.

I have a macro which works perfectly. It runs from sheet2, makes
changes on sheet1 then finishes on bach sheet2. Which is what I want.
It works fine when I run it as a macro, however when I copy and paste
the EXACT code into a command button and click the button I get an
error!?!?

Here is the code...

Sub temp()
Sheets("sheet1").Select
Range("AF4").Select
Selection.End(xlToLeft).Select
ActiveCell.Columns("A:A").EntireColumn.Select
Selection.Copy
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
ActiveSheet.Paste
ActiveCell.Offset(12, 2).Range("A1").Select
Sheets("sheet2").Select
ActiveCell.Select
End Sub

Here is the error...

Runtime error '1004'
Select method of range class failed

the line that is highlighted when I run the debugger is the 2nd line:
Range("AF4").Select.


CHEERS


---
Message posted from http://www.ExcelForum.com/



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default macro code doesnt work in command button

Thanks guys, im sorted now

--
Message posted from http://www.ExcelForum.com

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,337
Default macro code doesnt work in command button

glad to help

--
Don Guillett
SalesAid Software

"The Grinch " wrote in message
...
Thanks guys, im sorted now!


---
Message posted from
http://www.ExcelForum.com/



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
U.S. Command Button Macro won't work in Australian version 2003 Ron in Dallas Excel Discussion (Misc queries) 1 December 23rd 05 05:08 PM
In excel my bold command doesnt work? Parvez Ansari Excel Worksheet Functions 3 March 10th 05 10:47 PM
Assign Macro to button in Excel doesnt work Any ideas? Mike@Becketts Excel Discussion (Misc queries) 2 December 20th 04 02:47 PM
Embed command button from the control toolbox doesnt work guichre Excel Worksheet Functions 1 November 16th 04 02:28 AM
Embed command button from the control toolbox doesnt work Roland Excel Worksheet Functions 0 November 16th 04 02:20 AM


All times are GMT +1. The time now is 08:13 PM.

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"