Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default More 1004 Errors...

Your responses to my last concerns were great and solved the problem. I seem
to have created a new one that it doesn't address...

Private Sub CommandButton1_Click() 'Normalize
Dim i, j, numberofRows As Integer

Application.ScreenUpdating = False
Sheets("YTD BD").Activate
Range("A1").Select
numberofRows = ActiveCell.CurrentRegion.Rows.Count
numberofColumns = ActiveCell.CurrentRegion.Columns.Count
Range(Cells(2, 1), Cells(numberofRows, 5)).Select
Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlNo,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

*** The following line gave the 1004 error
ActiveSheet.Range(Cells(1, numberofColumns + 2)).Select

*** So I tried:

With ActiveSheet
.Range(.Cells(1, numberofColumns + 2)).Select
End With

Which also gave me the same 1004 error.

Any advice?

Thanks,

Jim Berglund



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default More 1004 Errors...

You can only select a range on a sheet that's selected/activated.

When you're code is in a general module, the unqualified ranges refer to the
activesheet. But when the code is behind a worksheet (I'm guessing that your
code is), then the unqualified ranges refer to the worksheet owning the code.

This seemed to work for me:

Option Explicit
Private Sub CommandButton1_Click() 'Normalize
Dim i As Long, j As Long, numberofRows As Long, numberOfColumns As Long

Application.ScreenUpdating = False
With Sheets("YTD BD")
.Activate
.Range("A1").Select
numberofRows = .Range("a1").CurrentRegion.Rows.Count
numberOfColumns = .Range("a1").CurrentRegion.Columns.Count
.Range(.Cells(2, 1), .Cells(numberofRows, 5)).Select
Selection.Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
.Cells(1, numberOfColumns + 2).Select
End With
Application.ScreenUpdating = True
End Sub

But it's easier to do it without the activating and selecting:

Option Explicit
Private Sub CommandButton1_Click() 'Normalize
Dim i As Long, j As Long, numberofRows As Long, numberOfColumns As Long

Application.ScreenUpdating = False
With Sheets("YTD BD")
With .Range("a1").CurrentRegion
numberofRows = .Rows.Count
'numberOfColumns = .Columns.Count
End With
.Range(.Cells(2, 1), .Cells(numberofRows, 5)).Sort _
Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End With
Application.ScreenUpdating = True
End Sub



Jim Berglund wrote:

Your responses to my last concerns were great and solved the problem. I seem
to have created a new one that it doesn't address...

Private Sub CommandButton1_Click() 'Normalize
Dim i, j, numberofRows As Integer

Application.ScreenUpdating = False
Sheets("YTD BD").Activate
Range("A1").Select
numberofRows = ActiveCell.CurrentRegion.Rows.Count
numberofColumns = ActiveCell.CurrentRegion.Columns.Count
Range(Cells(2, 1), Cells(numberofRows, 5)).Select
Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlNo,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

*** The following line gave the 1004 error
ActiveSheet.Range(Cells(1, numberofColumns + 2)).Select

*** So I tried:

With ActiveSheet
.Range(.Cells(1, numberofColumns + 2)).Select
End With

Which also gave me the same 1004 error.

Any advice?

Thanks,

Jim Berglund


--

Dave Peterson

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
Run time 1004 cluckers Excel Discussion (Misc queries) 5 January 22nd 10 09:17 PM
why am i getting an error 1004?? please help SAM SEBAIHI Excel Discussion (Misc queries) 2 February 16th 07 02:21 AM
Excel Throwing Circular Errors When No Errors Exist MDW Excel Worksheet Functions 1 August 10th 06 02:15 PM
Unresolved Errors in IF Statements - Errors do not show in results Markthepain Excel Worksheet Functions 2 December 3rd 04 08:49 AM
Error no. 1004 Cyrus[_2_] Excel Programming 1 October 22nd 03 01:06 PM


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