+ Reply to Thread
Results 1 to 12 of 12

Apply to selected columns

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-18-2009
    Location
    Winnipeg
    MS-Off Ver
    Excel 2003
    Posts
    203

    Apply to selected columns

    I'm using the following code to fill in blanks in a table. The table has anywhere from 1-5 columns with blanks I need to fill. Currently the code only fills in one column at a time. How do I change it so I can select a number of columns and only run the macro once?

    Sub FillColBlanks()
    'by Dave Peterson  2004-01-06
    'fill blank cells in column with value above
    'http://www.contextures.com/xlDataEntry02.html
    Dim wks As Worksheet
    Dim rng As Range
    Dim LastRow As Long
    Dim col As Long
    
    Set wks = ActiveSheet
    With wks
       col = activecell.column
       'or
       'col = .range("b1").column
    
       Set rng = .UsedRange  'try to reset the lastcell
       LastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row
       Set rng = Nothing
       On Error Resume Next
       Set rng = .Range(.Cells(2, col), .Cells(LastRow, col)) _
                      .Cells.SpecialCells(xlCellTypeBlanks)
       On Error GoTo 0
    
       If rng Is Nothing Then
           MsgBox "No blanks found"
           Exit Sub
       Else
           rng.FormulaR1C1 = "=R[-1]C"
       End If
    
       'replace formulas with values
       With .Cells(1, col).EntireColumn
           .Value = .Value
       End With
    
    End With
    
    End Sub

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Apply to selected columns

    Where is the table located?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    11-18-2009
    Location
    Winnipeg
    MS-Off Ver
    Excel 2003
    Posts
    203

    Re: Apply to selected columns

    It's on one worksheet, anywhere from 3-8 columns and up to 500 rows. Not sure if that answers your question...

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Apply to selected columns

    Is it the entire used range of the sheet?

  5. #5
    Forum Contributor
    Join Date
    11-18-2009
    Location
    Winnipeg
    MS-Off Ver
    Excel 2003
    Posts
    203

    Re: Apply to selected columns

    Quote Originally Posted by shg View Post
    Is it the entire used range of the sheet?
    No...it's always the first column, and usually the second and third...sometimes others, but the last 2 columns don't have any blanks.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Apply to selected columns

    My question is, how is the code supposed to know the extents of the table?

    You can apply the method to the selection, or the current region of the selection, or the current region of some particular cell that's always in the table, or to a named range, or to the used range, or to ....

  7. #7
    Registered User
    Join Date
    08-18-2010
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Apply to selected columns

    Much easier way than a macro.
    Highlight your whole area
    -Edit-GoTo (or Control G)
    -Special
    -choose Blanks (all blanks are highlighted)
    Then put in what ever you want in the blanks (n/a, or whatever) and instead of pressing enter press Control Enter which will fill all blanks.

  8. #8
    Forum Contributor
    Join Date
    11-18-2009
    Location
    Winnipeg
    MS-Off Ver
    Excel 2003
    Posts
    203

    Re: Apply to selected columns

    Quote Originally Posted by kzmr1234 View Post
    Much easier way than a macro.
    Highlight your whole area
    -Edit-GoTo (or Control G)
    -Special
    -choose Blanks (all blanks are highlighted)
    Then put in what ever you want in the blanks (n/a, or whatever) and instead of pressing enter press Control Enter which will fill all blanks.
    That's what I have been doing...but when I try to train that to my folks who are not so comfortable with computers, they don't understand. I was thinking it might be easier if I had a button with the macro attached to it and all they'd have to do is highlight the columns they wanted and click the button...

  9. #9
    Forum Contributor
    Join Date
    11-18-2009
    Location
    Winnipeg
    MS-Off Ver
    Excel 2003
    Posts
    203

    Re: Apply to selected columns

    Ok so i just recorded a macro of the Edit > GoTo function...I don't know if that's the same thing as the macro I had copied, but it seems to work....

  10. #10
    Registered User
    Join Date
    08-18-2010
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Apply to selected columns

    As long as the cells were highlighted before the macro was run...and...your people know to highlight their cells in the area before running the macro.

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Apply to selected columns

    Try this:
    Sub FillBlanksInSelection()
        Dim rBlnk       As Range
        Dim rArea       As Range
    
        On Error Resume Next
        Set rBlnk = ActiveWindow.RangeSelection.SpecialCells(xlCellTypeBlanks)
        If Err.Number Then
            MsgBox "No blanks found"
            Err.Clear
        Else
            If Not Intersect(rBlnk, Rows(1)) Is Nothing Then
                MsgBox "Can't handle blanks in Row 1!"
            Else
                rBlnk.FormulaR1C1 = "=r[-1]c"
                For Each rArea In rBlnk.Areas
                    rArea.Value = rArea.Value
                Next rArea
            End If
        End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1