Consulting

Results 1 to 4 of 4

Thread: VBA clearing cells based on a selection

  1. #1

    VBA clearing cells based on a selection

    Based on a serial number entered at or below cell C8, a macro runs and prints results in columns D through G to the right of the input cell. The macro only runs if the active cell is at or below C8, so I want similar cell restrictions for the reset button.

    I was able to write a code that clears all information from columns C through G, but the user must clear rows one at a time. I want the user to select multiple cells in column C and clear all cells to the right of those. Additionally, the cells may initially be colored, so I need to include .Interior.Color = xlNone

    Since the user selection is unknown, I have been trouble defining a range. The selected range may be C8, or C10 through C20, or any other combination of cells.

    Any help would be great.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Lets see the code that you have. with the cursor in the code page, press Ctrl+A, then Ctrl+C. Then, in the Post editor menu, Click the # Icon, then Press Ctrl+V.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    I figured it out. I was looking for the lines Dim c As Range and For Each c In Selection

    Sub clear()
    Dim c As Range
    For Each c In Selection
    c.Offset(0, 1) = blank
    c.Offset(0, 2) = blank
    c.Offset(0, 3) = blank
    c.Offset(0, 4) = blank
    Next c
    End Sub

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Sub clear()
    Selection.Resize(1, 4). ClearContents
    End sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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