0

How to clear an Excel array ( with unknown shape) from VBA ? Given that I know that the array commences from "A1" for example. So I need to get the shape , select and clear.

4
  • Do you mean a Range of cells or an internal VBA array ?? Commented Mar 20, 2014 at 10:55
  • Not internal VBA array, but Excel array ( the one that displays "you cannot change part of an array" when you try to delete one single cell instead of the whole array ). Does this make sens ? Commented Mar 20, 2014 at 11:03
  • It makes sense............. see my answer Commented Mar 20, 2014 at 11:06
  • ANd how would I know what is the shape of "MyRange" ? My problem is that I don't know how to select it in order to clear its contents Commented Mar 20, 2014 at 11:09

2 Answers 2

2

If you mean a range of cells:

MyRange.Clear

or

MyRange.ClearContents

EDIT#1

To clear a range of cells requires two parts:

  1. Set the Range
  2. apply a Clear method

If you have a block of formulas starting in A1 then your posted code will work in a fashion similar to:

Sub ClearCells()
    Dim RangeToClear As Range
    '
    'the first part
    '
    d1 = Range("A1").End(xlToRight).Column
    d2 = Range("A1").End(xlDown).Row
    Set RangeToClear = ActiveSheet.Range(Cells(1, 1), Cells(d1, d2))
    '
    'the second part
    '
    RangeToClear.ClearContents
End Sub

will work............I meant the the second part requires only one statement.

Sign up to request clarification or add additional context in comments.

5 Comments

Common I wouldnt have asked such a simple thing, I have an excel array formula with unknown shape !
It does not matter what the shape is!! You can clear all the cells at the same time with one command! You don't have to loop over rows or columns!
one command ? how would vba know what cells to delete from the excel sheet ? it is not very clear what MyRange is in your answer.
Ya my problem was the the first part, I was wondering if there was a better way to do this.
Now I understand....if the cells are part of an array formula block then use Range("A1").CurrentArray.ClearContents
0

A quick and dirty solution is to use :

d1 =Range("A1").End(xlToRight).Column 
d2 =Range("A1").End(xlDown).row 
ActiveSheet.Range(Cells(1, 1), Cells(LastRowData, LastColData)).ClearContents

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.