+ Reply to Thread
Results 1 to 5 of 5

VB Code to Fill zero in rest of the cells

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    VB Code to Fill zero in rest of the cells

    Hello:

    Please refer to attached file.

    I am using below code succesfully to get unique names from column B into Row 2 staring in cell E2 as shown.

    Sub SortEmployee()
    
    Range("E2").Resize(1, Columns.Count - 13).ClearContents
    If MsgBox("Do you want to run the macro", vbYesNo) = vbNo Then Exit Sub
    Application.ScreenUpdating = False
    With Range("B1", Cells(Rows.Count, 2).End(xlUp))
        .AdvancedFilter xlFilterInPlace, , , True
        .Offset(1).Copy ' cell C1 is header of column C
           [E2].PasteSpecial xlPasteValues, xlNone, False, True
           .AutoFilter: .AutoFilter
    End With
    With Selection
        .Sort Key1:=.Cells(1), Order1:=xlAscending, Orientation:=xlLeftToRight
    End With
    Application.ScreenUpdating = True
    
    End Sub
    What i want is to have cell j2:Z2 = 0 in this case.
    If i had names until P2, then Q2:Z2 = 0.

    Please help modify the above code to add this feature.

    Let me know if you have any questions.
    Thanks.
    Riz
    Attached Files Attached Files
    Last edited by rizmomin; 12-27-2014 at 07:17 PM.

  2. #2
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: VB Code to Fill zero in rest of the cells

    Try this....
    Sub ChangeBlankToZero()
    
    
    Dim r As Range, cell As Range
    
    Set r = Range("J2:Z2")
    
    For Each cell In r
        If cell = "" Then
            cell.Value = cell.Value + 0
        End If
    Next
    End Sub
    Change range to suit your needs
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  3. #3
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: VB Code to Fill zero in rest of the cells

    Hi gmr4evr1:

    It works but small problem.
    Please run the macro i have created in the uploaded file to see the result.
    Basically my code is going create a unique name list and it will paste at position starting cell E2.
    I could have a long list of names.
    If the list ends at H2 then zero should start from I2 till Z2.
    If the list ends at say S2 then zero should start from T2 till Z2.
    I hope this is clear and once again let me know if any further question and as always help is greatly appreciated.
    Riz

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: VB Code to Fill zero in rest of the cells

    hi rizmomin,

    Sub SortEmployee()
    
    Range("E2").Resize(1, Columns.Count - 13).ClearContents
    
    If MsgBox("Do you want to run the macro", vbYesNo) = vbNo Then Exit Sub
    
    Application.ScreenUpdating = False
    
    With Range("B1", Cells(Rows.Count, 2).End(xlUp))
        .AdvancedFilter xlFilterInPlace, , , True
        .Offset(1).Copy ' cell C1 is header of column C
           [E2].PasteSpecial xlPasteValues, xlNone, False, True
           .AutoFilter: .AutoFilter
    End With
    
    Selection.Sort Key1:=Selection.Cells(1), Order1:=xlAscending, Orientation:=xlLeftToRight
    
    If Range("z2") = "" Then Range("e2:z2").SpecialCells(xlCellTypeBlanks) = 0
    
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by watersev; 12-27-2014 at 07:48 PM.

  5. #5
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: VB Code to Fill zero in rest of the cells

    Hi watersev:

    Thanks a lot, this works as per my specs.
    Riz

+ 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] Code to fill down 2 cells to last row
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-31-2014, 12:19 PM
  2. Code to fill down cells
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-22-2014, 01:41 PM
  3. Replies: 5
    Last Post: 01-03-2014, 09:30 PM
  4. VBA code to fill rest of cell with dashes (-)
    By mikeburg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-17-2006, 07:58 PM
  5. Trying to find a way to fill in the rest of a row if it is a dupli
    By Robert Brown in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-29-2006, 01:00 PM

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