Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 79
Default Combined code not working

The following code (below) has been pieced together from individual modules
and works when run from the worksheet titled "Charts!" When I try to run it
from another sheet I get the following message : Runtime error '1004'
Selection method of range class failed and the debug highlights the
following line of code:

Range("Charts!b25:b56").Select




Sub Delete_CR()
Dim Rng As Range, rng1 As Range
On Error Resume Next
Set Rng = Range("Charts!B25:IV26").SpecialCells(xlFormulas, xlErrors)
On Error GoTo 0
If Not Rng Is Nothing Then Rng.EntireColumn.Delete
Dim n As Long, lastrow As Long
lastrow = Range("Charts!B52").End(xlUp).Row
For n = lastrow To 2 Step -1
If Cells(n, 2) = "Grand Total" Or Cells(n, 2) = "0" _
Then Cells(n, 2).EntireRow.Delete
Next n
Range("Charts!b25:b56").Select
For Each Rng In Selection.Cells
If Rng.Interior.ColorIndex = 1 Then
Rng.EntireRow.Hidden = True
End If
Next Rng
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
No Name
 
Posts: n/a
Default Combined code not working

hi,
that line of code is trying to select a range on a sheet
named charts and it can't find charts.
go through the code and change "charts" to the sheet in
the new workbook.

-----Original Message-----
The following code (below) has been pieced together from

individual modules
and works when run from the worksheet titled "Charts!"

When I try to run it
from another sheet I get the following message : Runtime

error '1004'
Selection method of range class failed and the debug

highlights the
following line of code:

Range("Charts!b25:b56").Select




Sub Delete_CR()
Dim Rng As Range, rng1 As Range
On Error Resume Next
Set Rng = Range("Charts!B25:IV26").SpecialCells

(xlFormulas, xlErrors)
On Error GoTo 0
If Not Rng Is Nothing Then Rng.EntireColumn.Delete
Dim n As Long, lastrow As Long
lastrow = Range("Charts!B52").End(xlUp).Row
For n = lastrow To 2 Step -1
If Cells(n, 2) = "Grand Total" Or Cells(n, 2)

= "0" _
Then Cells(n, 2).EntireRow.Delete
Next n
Range("Charts!b25:b56").Select
For Each Rng In Selection.Cells
If Rng.Interior.ColorIndex = 1 Then
Rng.EntireRow.Hidden = True
End If
Next Rng
End Sub


.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 79
Default Combined code not working

Thanks for the reply. I don't think I explained my problem very well. The
code works okay from the "Charts" worksheet, but not from another worksheet
within the same workbook. I am not trying to run it from a new workbook.
When I run the macro from the work sheet titled "Chart" it works, but it
doesn't when I run it from another worksheet it the same workbook.

wrote in message
...
hi,
that line of code is trying to select a range on a sheet
named charts and it can't find charts.
go through the code and change "charts" to the sheet in
the new workbook.

-----Original Message-----
The following code (below) has been pieced together from

individual modules
and works when run from the worksheet titled "Charts!"

When I try to run it
from another sheet I get the following message : Runtime

error '1004'
Selection method of range class failed and the debug

highlights the
following line of code:

Range("Charts!b25:b56").Select




Sub Delete_CR()
Dim Rng As Range, rng1 As Range
On Error Resume Next
Set Rng = Range("Charts!B25:IV26").SpecialCells

(xlFormulas, xlErrors)
On Error GoTo 0
If Not Rng Is Nothing Then Rng.EntireColumn.Delete
Dim n As Long, lastrow As Long
lastrow = Range("Charts!B52").End(xlUp).Row
For n = lastrow To 2 Step -1
If Cells(n, 2) = "Grand Total" Or Cells(n, 2)

= "0" _
Then Cells(n, 2).EntireRow.Delete
Next n
Range("Charts!b25:b56").Select
For Each Rng In Selection.Cells
If Rng.Interior.ColorIndex = 1 Then
Rng.EntireRow.Hidden = True
End If
Next Rng
End Sub


.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Combined code not working

Joel,

To select a range, the sheet must be active.
Also, you generally do Not have to select something in order to
manipulate it with VBA code.

See if the following modified code does what you want...
'---------------------------------
Sub Delete_CR()
Dim Rng As Excel.Range
Dim rng1 As Excel.Range
Dim N As Long
Dim LastRow As Long

On Error Resume Next
Set Rng = Worksheets("Charts").Range("B25:IV26").SpecialCell s(xlFormulas, xlErrors)
On Error GoTo 0

If Not Rng Is Nothing Then Rng.EntireColumn.Delete
LastRow = Worksheets("Charts").Range("B52").End(xlUp).Row

For N = LastRow To 2 Step -1
With Worksheets("Charts").Cells(N, 2)
If .Value = "Grand Total" Or .Value = "0" Then
.EntireRow.Delete
End If
End With
Next N

Set Rng = Worksheets("Charts").Range("B25:B56")
For Each rng1 In Rng
If rng1.Interior.ColorIndex = 1 Then
rng1.EntireRow.Hidden = True
End If
Next ' rng1

Set Rng = Nothing
Set rng1 = Nothing
End Sub
'---------------------------------

Regards,
Jim Cone
San Francisco, CA


"Joel Mills" wrote in message
...
The following code (below) has been pieced together from individual modules
and works when run from the worksheet titled "Charts!" When I try to run it
from another sheet I get the following message : Runtime error '1004'
Selection method of range class failed and the debug highlights the
following line of code:

Range("Charts!b25:b56").Select
Sub Delete_CR()
Dim Rng As Range, rng1 As Range
On Error Resume Next
Set Rng = Range("Charts!B25:IV26").SpecialCells(xlFormulas, xlErrors)
On Error GoTo 0
If Not Rng Is Nothing Then Rng.EntireColumn.Delete
Dim n As Long, lastrow As Long
lastrow = Range("Charts!B52").End(xlUp).Row
For n = lastrow To 2 Step -1
If Cells(n, 2) = "Grand Total" Or Cells(n, 2) = "0" _
Then Cells(n, 2).EntireRow.Delete
Next n
Range("Charts!b25:b56").Select
For Each Rng In Selection.Cells
If Rng.Interior.ColorIndex = 1 Then
Rng.EntireRow.Hidden = True
End If
Next Rng
End Sub


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 79
Default Combined code not working

Jim,

Your revisions worked great. I am very new to using VBA and simple tried
copying several code strings into one module to simplify running a macro.
Once again thanks for your help.

"Jim Cone" wrote in message
...
Joel,

To select a range, the sheet must be active.
Also, you generally do Not have to select something in order to
manipulate it with VBA code.

See if the following modified code does what you want...
'---------------------------------
Sub Delete_CR()
Dim Rng As Excel.Range
Dim rng1 As Excel.Range
Dim N As Long
Dim LastRow As Long

On Error Resume Next
Set Rng = Worksheets("Charts").Range("B25:IV26").SpecialCell s(xlFormulas,

xlErrors)
On Error GoTo 0

If Not Rng Is Nothing Then Rng.EntireColumn.Delete
LastRow = Worksheets("Charts").Range("B52").End(xlUp).Row

For N = LastRow To 2 Step -1
With Worksheets("Charts").Cells(N, 2)
If .Value = "Grand Total" Or .Value = "0" Then
.EntireRow.Delete
End If
End With
Next N

Set Rng = Worksheets("Charts").Range("B25:B56")
For Each rng1 In Rng
If rng1.Interior.ColorIndex = 1 Then
rng1.EntireRow.Hidden = True
End If
Next ' rng1

Set Rng = Nothing
Set rng1 = Nothing
End Sub
'---------------------------------

Regards,
Jim Cone
San Francisco, CA


"Joel Mills" wrote in message
...
The following code (below) has been pieced together from individual

modules
and works when run from the worksheet titled "Charts!" When I try to run

it
from another sheet I get the following message : Runtime error '1004'
Selection method of range class failed and the debug highlights the
following line of code:

Range("Charts!b25:b56").Select
Sub Delete_CR()
Dim Rng As Range, rng1 As Range
On Error Resume Next
Set Rng = Range("Charts!B25:IV26").SpecialCells(xlFormulas, xlErrors)
On Error GoTo 0
If Not Rng Is Nothing Then Rng.EntireColumn.Delete
Dim n As Long, lastrow As Long
lastrow = Range("Charts!B52").End(xlUp).Row
For n = lastrow To 2 Step -1
If Cells(n, 2) = "Grand Total" Or Cells(n, 2) = "0" _
Then Cells(n, 2).EntireRow.Delete
Next n
Range("Charts!b25:b56").Select
For Each Rng In Selection.Cells
If Rng.Interior.ColorIndex = 1 Then
Rng.EntireRow.Hidden = True
End If
Next Rng
End Sub






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Combined code not working

Joel,
You are welcome.
Getting feedback on the solution offered is doubly appreciated.
Regards,
Jim Cone
San Francisco, CA

"Joel Mills" wrote in message ...
Jim,
Your revisions worked great. I am very new to using VBA and simple tried
copying several code strings into one module to simplify running a macro.
Once again thanks for your help.


-snip-
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
VB code is not working like it should Bob Reynolds[_3_] Excel Programming 8 July 19th 04 12:02 AM
Code not working Todd Huttenstine Excel Programming 1 June 10th 04 05:06 PM
Code not Working - Help please Brian Excel Programming 2 November 18th 03 10:58 PM
Code not working Bob Phillips[_5_] Excel Programming 5 August 14th 03 03:12 PM
For Each Code Not Working jacqui[_2_] Excel Programming 4 July 29th 03 02:44 AM


All times are GMT +1. The time now is 05:46 PM.

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

About Us

"It's about Microsoft Excel"