Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 661
Default Macro Error in Command Button

I have recorded a macro that runs fine when I use the Macro Run command, but
when I copy the macro text and assign it to a command button it gives an
error. The following is the macro:

It gets hung up on the 2nd line (Rows...)

Any idea why it would work as a macro and not when placed into a command
button?

Sheets("Jacobs-Planning").Select
Rows("1:343").Select
Selection.Clear
Range("B7").Select
ActiveWindow.SmallScroll Down:=-30
Range("A5").Select
Range("MasterData").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:= _
Sheets("Master Sheet").Range("A2:A3"), CopyToRange:=Range("A6"),
Unique:= _
False
Cells.Select
With Selection.Font
.Name = "Calibri"
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
With Selection.Font
.Name = "Calibri"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Range("H6").Select
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default Macro Error in Command Button

What kind of command button (Forms toolbar or Controls Toolbox)? Where did
you paste the code? What version of Excel is this?

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services, Inc.
http://PeltierTech.com/WordPress/
_______


"Paul" wrote in message
...
I have recorded a macro that runs fine when I use the Macro Run command,
but
when I copy the macro text and assign it to a command button it gives an
error. The following is the macro:

It gets hung up on the 2nd line (Rows...)

Any idea why it would work as a macro and not when placed into a command
button?

Sheets("Jacobs-Planning").Select
Rows("1:343").Select
Selection.Clear
Range("B7").Select
ActiveWindow.SmallScroll Down:=-30
Range("A5").Select
Range("MasterData").AdvancedFilter Action:=xlFilterCopy,
CriteriaRange:= _
Sheets("Master Sheet").Range("A2:A3"), CopyToRange:=Range("A6"),
Unique:= _
False
Cells.Select
With Selection.Font
.Name = "Calibri"
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
With Selection.Font
.Name = "Calibri"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Range("H6").Select
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 661
Default Macro Error in Command Button


It was a control toolbox, but I changed it to a Forms Button at it appears
to work.

It is excel 2003.

I think this is solved.

Thanks

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Macro Error in Command Button

If this macro is assigned to a commandbutton placed on a worksheet (from the
control toolbox), then part of the problem is then unqualified ranges.

Those unqualified ranges (like rows("1:343") will refer to the sheet that owns
the code--not the activesheet. This is different than code placed in a general
module.

And since you can only select a range on a worksheet that is active, the kind of
thing blows up:

Sheets("Jacobs-Planning").Select
Rows("1:343").Select


This may work for you:

Option Explicit
Private Sub CommandButton1_Click()
With Worksheets("Jacobs-Planning")
.Rows("1:343").Clear 'use .clearcontents to keep the formatting

.Range("MasterData").AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=Worksheets("Master Sheet").Range("A2:A3"), _
CopyToRange:=.Range("A6"), _
Unique:=False

With .Cells.Font
.Name = "Calibri"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
End With

End Sub

This line may have to change:

.Range("MasterData").AdvancedFilter _

to something like:

worksheets("Someothersheetnamehere").Range("Master Data").AdvancedFilter _

I wasn't sure what worksheet owned the MasterData range, so I guessed
"jacobs-planning".

Paul wrote:

I have recorded a macro that runs fine when I use the Macro Run command, but
when I copy the macro text and assign it to a command button it gives an
error. The following is the macro:

It gets hung up on the 2nd line (Rows...)

Any idea why it would work as a macro and not when placed into a command
button?

Sheets("Jacobs-Planning").Select
Rows("1:343").Select
Selection.Clear
Range("B7").Select
ActiveWindow.SmallScroll Down:=-30
Range("A5").Select
Range("MasterData").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:= _
Sheets("Master Sheet").Range("A2:A3"), CopyToRange:=Range("A6"),
Unique:= _
False
Cells.Select
With Selection.Font
.Name = "Calibri"
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
With Selection.Font
.Name = "Calibri"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Range("H6").Select
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
End Sub


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default Macro Error in Command Button

I should have pointed out that I've written about these two ways to assign
macros to buttons:

http://peltiertech.com/WordPress/200...tton-or-shape/
http://peltiertech.com/WordPress/200...tivex-control/

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services, Inc.
http://PeltierTech.com/WordPress/
_______


"Paul" wrote in message
...

It was a control toolbox, but I changed it to a Forms Button at it appears
to work.

It is excel 2003.

I think this is solved.

Thanks



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 error Ram[_5_] Excel Programming 13 July 6th 07 12:29 PM
Error When Assigning a Macro to a Command Button Gavin Ayling Excel Programming 0 January 24th 07 04:24 PM
Automation Error when deleting command bar button Frank Kabel Excel Programming 6 August 4th 04 04:09 PM
Command Button run-time error Dominique Feteau Excel Programming 1 June 29th 04 02:57 AM
Run-time error on command button Phil Hageman[_3_] Excel Programming 4 November 4th 03 08:06 PM


All times are GMT +1. The time now is 11:31 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"