Macro Problem

counihan3

Active Member
Joined
Mar 9, 2007
Messages
458
I am using this public function to clean my worksheet of unneeded data after a macro has run. Problem is that if there are no results for it to clean it removes row 1. Is there a line of code I can add so that it will never clear out row 1?

Code:
Public Function CleanSheet()

LastRow = Range("A" & Rows.Count).End(xlUp).Row
Rows("2:" & LastRow).Select
Selection.ClearContents
Range("A1").Select

End Function
[/quote]
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
how is that possible?

it says rows("2:" & LastRow)

so it shouldn't touch 1

EDIT * i see what you mean now*
 
Upvote 0
Try this.
Code:
If LastRow>1 Then
     Rows("2:"& LastRow).ClearContents
End If

Or this.
Code:
Rows("2").Resize(LastRow)
By the way why are you using a function.:eek:

QuietRiot

If LastRow is 1 then the range is Rows("2:1").

So row 1 will also get cleared.

Kind of backward but that's the way it works.:)
 
Upvote 0
also why is it a function?

Code:
Sub CleanSheet()

LastRow = Range("A" & Rows.Count).End(xlUp).Row
If LastRow>1 Then 
     Rows("2:"& LastRow).ClearContents 
End If 

End Sub
 
Upvote 0
Its a function becuase it is being called into macros as they run, if there is a better way to do it I would like to learn how.
 
Upvote 0
Well there's not really a 'better' way.

But the main purpose of functions is to return values not do something like this, where you don't return anything.

You could just easily change Function to Sub and the code should probably still work.:)
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,217
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top