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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use \$ signs: \$V\$2:\$Z\$99 will always point to V2:Z99, even after copying

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
5
Views
208
Replies
11
Views
170
Replies
3
Views
68
Replies
6
Views
152
Replies
9
Views
257

1,141,309
Messages
5,705,672
Members
421,404
Latest member
Mikecollo

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.
Allow Ads at MrExcel

Which adblocker are you using?

Follow these easy steps to disable AdBlock

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

Follow these easy steps to disable AdBlock Plus

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