Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default writing excel macros

First, I pasted too much:

call Sub DeleteUnused
should be
call DeleteUnused

====
And I've used that code lots of times without error.

But remember that since it's deleting rows, it can only work against an
unprotected worksheet.

Any chance that your sheet is protected?

This minor tweak will stop the error if a worksheet is protected.

Option Explicit
Sub DeleteUnused()

Dim myLastRow As Long
Dim myLastCol As Long
Dim wks As Worksheet
Dim dummyRng As Range

For Each wks In ActiveWorkbook.Worksheets
With wks
If wks.ProtectContents = True _
Or wks.ProtectDrawingObjects = True _
Or wks.ProtectScenarios = True Then
'do nothing
Else
myLastRow = 0
myLastCol = 0
Set dummyRng = .UsedRange
On Error Resume Next
myLastRow = _
.Cells.Find("*", after:=.Cells(1), _
LookIn:=xlFormulas, lookat:=xlWhole, _
searchdirection:=xlPrevious, _
searchorder:=xlByRows).Row
myLastCol = _
.Cells.Find("*", after:=.Cells(1), _
LookIn:=xlFormulas, lookat:=xlWhole, _
searchdirection:=xlPrevious, _
searchorder:=xlByColumns).Column
On Error GoTo 0

If myLastRow * myLastCol = 0 Then
.Columns.Delete
Else
.Range(.Cells(myLastRow + 1, 1), _
.Cells(.Rows.Count, 1)).EntireRow.Delete
.Range(.Cells(1, myLastCol + 1), _
.Cells(1, .Columns.Count)).EntireColumn.Delete
End If
End If
End With
Next wks

End Sub


TMiGNa wrote:

I have the code installed, but when I try to run it, I get the message
"run-time error '1004': Application-defined or object-defined error" and the
debugger highlights this part of the code. Do I need to add or remove
something? Thanks for your patience with me.

Else
.Range(.Cells(myLastRow + 1, 1), _
.Cells(.Rows.Count, 1)).EntireRow.Delete

"Dave Peterson" wrote:

The subroutine "Sub DeleteUnused()" on Debra's site actually tries to reset the
last used cell of all the worksheets in the active workbook.

So you could make that just another procedure in your project (at the bottom of
the module -- or in a whole different module).

Then just add:

call Sub DeleteUnused

to your existing code (as long as the workbook to "clean" is active).

So you can do it beforehand -- right at the start???.

Or do it right after you copy to the new workbook and just clean up that
workbook with a single sheet.

=======
Personally???

I have this same kind of macro in my personal.xl* file. I have it assigned to a
short cut key (ctrl-t isn't used by excel, so I use that).

I find it useful for lots and lots of workbooks and just run it when I want. In
my case, I'd run my personal.xl* version (via the shortcut key), then run your
existing code.

If I have to share with others, I'd embed it into the routine (with "call Sub
DeleteUnused").




TMiGNa wrote:

Ah - see it. So here's where my newbie questions come in, so please be
patient with me.... would a plug that code in as a new macro, or add it to
the already existing macro? If the later, then how should it be added to that
code? My guess is as a new macro, but can a document utilise multiple macros?
Please forgive my lack of knowledge. Thanks!

"Dave Peterson" wrote:

And there was code in that link to Debra's site that could be used to reset the
last used cell.



--

Dave Peterson


--

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
writing macros in excel sheet Richard Buttrey Excel Worksheet Functions 7 May 10th 06 01:04 AM
writing macros in excel jaffar Excel Programming 1 May 5th 06 02:06 PM
Writing Excel Macros in VB.NET/C# [email protected] Excel Programming 0 January 19th 06 04:02 AM
Need help writing basic macros in EXCEL.. Macro Help Excel Discussion (Misc queries) 1 March 8th 05 02:28 PM
Writing Macros in Excel Shannon Excel Programming 6 August 13th 04 07:58 PM


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