I have an Excel dataset that has a string in A1, and other values in B1, B2, and B3 that relate to A1; and so on down the page. Sometimes there are more than three cells that relate to the other string (unpredictable). In this example, cells A2 and A3 are blank. I want to create a macro that will fill A2 and A3 (etc) with the contents of A1.
In the example below I am using [] to help format it as Excel cells. I want to go from:
[SMITH, John] [Home]
[Mobile]
[Work]
[DOE, John] [Home]
[Mobile]
to
[SMITH, John] [Home]
[SMITH, John] [Mobile]
[SMITH, John] [Work]
[DOE, John] [Home]
[DOE, John] [Mobile]
I want the macro to repeat this for varying iterations, sometimes I have 1000 lines to adjust manually. Tweaking the software that outputs the data is not an option.
The code I have is as follows:
Sub rname()
Dim cellvar As String
Dim i As Integer
cellvar = ActiveCell
i = 0
While i < 50
If ActiveCell.Offset(1,0) = "" Then
ActiveCell.Offset(1,0) = cellvar
i = i + 1
ElseIf ActiveCell.Offset(1,0) = "*" Then
ActiveCell.Offset(1,0).Activate
i = i + 1
End If
Wend
End Sub
The above code adds text to the cell below the active cell once and then stops responding. The following code runs once and doesn't stop responding - I can run it again, but it doesn't automatically move down a row.
Sub repeat_name()
Dim cellvar As String
Dim i As Integer
cellvar = ActiveCell
i = 1
For i = 1 To 50
If ActiveCell.Offset(1, 0) = "" Then
ActiveCell.Offset(1, 0) = cellvar
End If
If ActiveCell.Offset(1, 0) = "*" Then
ActiveCell.Offset(1, 0).Select.Activate 'I have tried .Offset(2,0)too
End If
i = i + 1
Next
End Sub
I am stumped here. Does anyone have any thoughts or suggestions?
Try this:
try this
Others have given working solutions, I'll just outline the problems with your code.
cellvar = ActiveCell
assigns the value of the active cell to cellvar but cellvar won't change if ActiveCell changes so you'll just copy [SMITH, John] for all other people. You'd have to reassign cellvar.If ActiveCell.Offset(1, 0) = "*" Then
This checks if the cell contains an asterisk. Instead useNot ActiveCell.Offset(1, 0) = ""
,ActiveCell.Offset(1, 0) <> ""
,Not isEmpty(ActiveCell.Offset(1, 0))
or justElse
(which would be the preferred version here since it doesn't require further calculations).Edit:
"*"
Can be used as a wildcard with theLike
operator as inIf ActiveCell.Offset(1, 0) Like "*" Then
but this would also be true for the empty string. To be sure that there is at least one sign you'd have to use"?*"
instead. The question mark stands for exactly one character and the asterisk for 0 or more. To check if a cell is empty I would recommend one of the above ways though.In you first sub this means that if the cell anything but "*", i will not be incremented and you end in an endless loop. In the second function, it means that the the active cell will not be changed and neither "" not "*" will be detected for the rest of the loop.
In the second sub, you don't need
i=i+1
, thefor
loop does that for you. This would mean that you increment i by 2 every iteration.ActiveCell.Offset(1, 0).Select.Activate
Here the "select" is too muchHere are the subs with minimal changes:
second sub:
Note that this is just to explain the problems with your code, I still recommend to use one of the other solutions here.
try this:
Try it as,
Before fillBlanks procedure After fillBlanks procedure
How about this: