Consulting

Results 1 to 9 of 9

Thread: Clear Cells Except First Row

  1. #1
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location

    Question Clear Cells Except First Row

    Hi all,

    How do I clear all cells in activesheet except first row by macro?
    Thanks.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by sheeeng
    How do I clear all cells in activesheet except first row by macro?

    Dim rng
        Set rng = ActiveSheet.UsedRange
        Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
        rng.ClearContents

  3. #3
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Can you explain more bout below code? I don't understand offset. Thanks.

    rng.Offset(1, 0)

    If I want to dim range as something, is it should be dim rng as range?


    Dim rng

    What is
    UsedRange

    Thanks

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by sheeeng
    Can you explain more bout below code? I don't understand offset. Thanks.

    rng.Offset(1, 0)
    It just takes the range offset by 1 row, that its bypasses row 1.

    Quote Originally Posted by sheeeng
    If I want to dim range as something, is it should be dim rng as range?


    Dim rng
    absolutely

    Dim rng As Range

  5. #5
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    I had error here. This is because I put in userform terminate.
    So even the sheet is empty except 1st row, it runs. How to have alternative to check whether need to clear sheet?

    Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    In Debug, rng.Rows.Count = 0


    Solution here.

    If (rng.Rows.Count > 1) Then
    Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    End If
    What is used range?

    Thanks.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by sheeeng
    What is used range?
    The range of cells that have been used, a subset of the cells within the worksheet.

  7. #7
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Thanks. Can we display the value of UsedRange?

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by sheeeng
    Thanks. Can we display the value of UsedRange?
    No, it is an object. You can display its address

    MsgBox Activesheet.UsedRange.Address

  9. #9
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Thanks. Another solved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •