+ Reply to Thread
Results 1 to 8 of 8

Modify Code to Include More Columns

Hybrid View

nirvehex Modify Code to Include More... 01-05-2015, 04:14 PM
mrice Re: Modify Code to Include... 01-05-2015, 04:28 PM
nirvehex Re: Modify Code to Include... 01-05-2015, 04:36 PM
mrice Re: Modify Code to Include... 01-05-2015, 04:40 PM
nirvehex Re: Modify Code to Include... 01-05-2015, 04:44 PM
mrice Re: Modify Code to Include... 01-05-2015, 04:48 PM
nirvehex Re: Modify Code to Include... 01-05-2015, 04:51 PM
nirvehex Re: Modify Code to Include... 01-05-2015, 05:22 PM
  1. #1
    Registered User
    Join Date
    09-07-2008
    Location
    Middletown
    Posts
    33

    Modify Code to Include More Columns

    Hey guys,

    I found a code that does what I'm trying to do, but I need to modify it to include more columns.

    Code:
    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 = .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
    Any idea how to (1) include more columns then only "b" and are non sequential? (ex. B, C, E, G, I, J) and (2) how to change the code to use Table Header names instead of column letters? Thank you!

    This is cross posted at: http://www.mrexcel.com/forum/excel-q...e-columns.html

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Modify Code to Include More Columns

    Maybe

    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
    
    Dim MyArray()
    
    MyArray = Array("HeadingB", "HeadingC", "HeadingE", "HeadingG", "HeadingI", "HeadingJ")
    
    Set wks = ActiveSheet
    With wks
       For Each MyColumn In MyArray
       col = .Rows(1).Find(MyColumn, , xlValues, xlWhole).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
        
        Next MyColumn
    
    End With
    
    
    End Sub

  3. #3
    Registered User
    Join Date
    09-07-2008
    Location
    Middletown
    Posts
    33

    Re: Modify Code to Include More Columns

    Thank you Martin.

    However, it errors out at:

    col = .Rows(1).Find(MyColumn, , xlValues, xlWhole).Column
    Should MyColumn be declared some where?

  4. #4
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Modify Code to Include More Columns

    Try dim -ing it and MyArray as strings.

  5. #5
    Registered User
    Join Date
    09-07-2008
    Location
    Middletown
    Posts
    33

    Re: Modify Code to Include More Columns

    Still errors out on the same line. Says "For Each control variable on arrays must be Variant"

    Any ideas?

  6. #6
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Modify Code to Include More Columns

    OK - try

    Dim MyArray() as Variant
    Dim MyColumn as Variant
    Not sure what 'errors out' means - is it a compile error or a runtime error?

  7. #7
    Registered User
    Join Date
    09-07-2008
    Location
    Middletown
    Posts
    33

    Re: Modify Code to Include More Columns

    I tried that too It is a compile error. I'm wondering do I have to set MyColumn= to anything?

  8. #8
    Registered User
    Join Date
    09-07-2008
    Location
    Middletown
    Posts
    33

    Re: Modify Code to Include More Columns

    Nevermind. I got it. I went another route and set multiple rng's and then combined them using the Union function!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro Code - Modify to include > or = to
    By SVTF in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-22-2014, 05:18 PM
  2. [SOLVED] Modify Macro Code to include an extra Pop Up with different trigger
    By SVTF in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-15-2014, 11:49 AM
  3. Help needed to modify the code to include the logic
    By reddys0909 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-17-2013, 09:28 PM
  4. Modify code to include header formatting
    By msmick in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-11-2011, 07:41 PM
  5. Need help to modify code to include more than 1 Column
    By coreytroy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-15-2007, 02:08 AM

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