Forum Discussion
VBA Needed for my data cleaning problem
- 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
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
- Ben BeneventoJul 20, 2018Copper Contributor
Thank you Lowell, work perfectly.