PDA

View Full Version : Delete everything on sheet except?



doctortt
04-16-2011, 04:53 PM
How do you write the codes to delete everything on sheet2 except certain cells (i.e. A1, B1, C1)???

mikerickson
04-16-2011, 05:43 PM
This is one way.
Dim ArrayOfRanges as Variant
Dim ArrayOfValues as Variant
Dim i As Long

With ThisWorkbook.Sheets("Sheet1"): Rem adjust
ArrayOfRanges = Array(.Range("A1"), .Range("B4"):Rem adjust, use continous ranges only
ReDim ArrayOfValues(0 to UBound(ArrayOfRanges))

For i = 0 to UBound(ArrayOfRanges)
ArrayOfValues(i) = ArrayOfRanges(i).Value
Next i

.UsedRange.Clear

For i = 0 to UBound(ArrayOfRanges)
ArrayOfRanges(i).Value = ArrayOfValues(i)
Next i
End With

doctortt
04-18-2011, 12:12 PM
thanks it works.

just a noob question. What does rem adjust mean?

mikerickson
04-18-2011, 01:00 PM
Rem is a key word meaning that what follows is a comment. The modern usage is an apostrophy '

That is a reminder that the indicated line should be adjusted to meet your situation, for example if you are working on DataSheet rather than Sheet1.

Similarly the next adjustment would be for you to indicate which cells you are interested in rather than the two that were used in the example code.