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]
 

Some videos you may like

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,077
how is that possible?

it says rows("2:" & LastRow)

so it shouldn't touch 1

EDIT * i see what you mean now*
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows
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.:)
 

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,077
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
 

counihan3

Active Member
Joined
Mar 9, 2007
Messages
458
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.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows
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.:)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,721
Messages
5,597,752
Members
414,171
Latest member
12Rev79

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
Top