Joneskvist
Joneskvist

Reputation: 137

Issues clearing cells in Excel Sheet

The following code is suppose to clear the value of the cells from A15:H in Excel:

'Set TargetSheet and clear the previous contents
targetSheet = "Lägg in Ärende"
tRow = 15
lastRow = Sheets(targetSheet).Range("A" & Rows.Count).End(xlUp).Row
Sheets(targetSheet).Range("A15:H" & lastRow).ClearContents

It works correctly when there is existing values in cells A15:H.

But when the cells are empty in A15:H and you still run the macro then all the cells in A2, A9 and A14:H14 gets erased. Does anybody have an idea why?

Upvotes: 0

Views: 59

Answers (1)

Liniel
Liniel

Reputation: 729

That will help you.

targetSheet = "Lägg in Ärende"
lastRow = Sheets(targetSheet).Range("A" & Rows.Count).End(xlUp).Row

IF lastRow<15 THEN lastRow=15
Sheets(targetSheet).Range("A15:H" & lastRow).ClearContents

Upvotes: 3

Related Questions