Forum Discussion

Ben Benevento's avatar
Ben Benevento
Copper Contributor
Jul 20, 2018
Solved

VBA Needed for my data cleaning problem

I have a dirty data set and would like to clean it with a VBA script.   My data looks like this (also attached);   Employee         Category                                            Hours  Amou...
  • Lowell Specht's avatar
    Jul 20, 2018

    There are many ways to do a task in Excel VBA and I'm sure others will give you more efficient ways to do this but this should work.  You can add additional code if you want to remove the assumptions.

     

    Sub FillNames()
        Dim i As Long
        Dim sName As String
        Dim nLastRow As Long
        
        ActiveSheet.UsedRange                   'reset the used range
        nLastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row  'assumes no other data below last person's data
        sName = ""
        
        For i = 1 To nLastRow               'assumes there will always be a name in row 1, column A
            If Cells(i, 1).Value = "" Then  'assumes Employee column is A which is column 1
                Cells(i, 1).Value = sName   'sets the current name in the empty cell
            Else
                sName = Cells(i, 1).Value   'assigns the current row's name
            End If
        Next i
    End Sub

Resources