# how to repeat the number according to the previous cell using macro

#### tinactt

##### New Member
Hello. I am new to macro.

I am trying to convert the following from column 1 to column 2.

 1 1 0 1 3 3 0 1 0 1 0 1 5 5 0 1 0 1 0 1 0 1 0 1

<TBODY>
</TBODY>

In the left column, the numbers who are greater than 0 can have the same number of rows of zero.

Is there anyway I can do it to convert Column 1 to column 2.

Thank you so much for your help.

### Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

#### OTOTO

##### Board Regular
put this function into B1 and then copy it down column B. =IF(\$A1>0,\$A1,1)

#### farmerscott

##### Well-known Member
Hi Tina,

welcome to to the forum.

I can't quite follow your comment - "the numbers who are greater than 0 can have the same number of rows of zero", but try the following formula

=IF(A1>0,A1,A1+1). It will convert col 1 to col 2 based on the pattern of your numbers.

FarmerScott

#### OTOTO

##### Board Regular
Hi Tina,

welcome to to the forum.

I can't quite follow your comment - "the numbers who are greater than 0 can have the same number of rows of zero", but try the following formula

=IF(A1>0,A1,A1+1). It will convert col 1 to col 2 based on the pattern of your numbers.

FarmerScott

That's where the question got me at first. I believe that the poster is starting with a series of whole numbers greater than zero. The intent, I believe, is to insert X # of rows beneath each row where X = the value in the cell.

If this is the intent, and the numbers only go up to "5" then a macro can be written with a shortcut that you could use (providing the list isn't terribly long). here just paste this macro in and use control + z with the cell highlighted.
Then replace the empty cells in column A with "0" and then paste the function =IF(\$A1>0,\$A1,1) into column B

Sub Macro()
'
' Macro Macro
'
' Keyboard Shortcut: Ctrl+z
'
If ActiveCell = "1" Then
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If

If ActiveCell = "2" Then
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If

If ActiveCell = "3" Then
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If

If ActiveCell = "4" Then
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If

If ActiveCell = "5" Then
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If
End Sub

#### farmerscott

##### Well-known Member
OTOTO,

I see what you mean.

FarmerScott

#### tinactt

##### New Member
Thank you OTOTO and FarmerScott.

Replies
0
Views
149
Replies
6
Views
444
Replies
1
Views
142
Replies
1
Views
198
Replies
3
Views
381

1,171,686
Messages
5,876,897
Members
433,218
Latest member
JReeves24

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back