Hi
1. You have used cell as a variable, so need to declare it. Cells is a
vba term so it might be better to use mycell for example
Dim mycell as Range
2. Your loop will not change the ActiceCell so once your loop puts a
value in it the loop will ignore it after that.
3. Your range newnames has 4 cells in it. Your code line
Worksheets("Sheet3").ActiveCell =3D newnames(n)
will fill the ActiveCell with data from newnames when n is 1, 2, 3 or
4 (the data in A1 to A4). Your loop, however, is cycling through the
range A1 to l9 one cell at a time (across rows then down) which is 12
times 9 =3D 108 cells. Your If condition inside this 108 cycle loop is
incrementing n, so once n goes above 4 you will get an error as
newnames(n) will not make sense. I suspect you want
Worksheets("Sheet3").ActiveCell =3D newnames(n)
inside your inner if..then..else?
A tentative guess at the code you really want is:
Private Sub CommandButton1_Click()
Dim oldnames As Range
Dim newnames As Range
Dim myCell as Range
Dim x As Integer
Dim n As Integer
Dim i As String
Dim k As Integer
Set newnames =3D Worksheets("Sheet2").Range("A1:A4")
n =3D 1
Worksheets("Sheet3").Activate
For Each myCell In Worksheets("Sheet3").Range("A1:I9")
myCell.Activate
If Trim(ActiveCell.Value) =3D "" Then
If (n <=3D 2) Then
Worksheets("Sheet3").ActiveCell.Value =3D newnames(n)
n =3D n + 1
Else
n =3D 1
End If
End If
Next myCell
UserForm1.Hide
End Sub
This will only allow n to take the values 1 or 2 however, which may
still not be what you really want.
regards
Paul