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

tinactt

New Member
Joined
Jan 6, 2014
Messages
5
Hello. I am new to macro.

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


11
01
33
01
01
01
55
01
01
01
01
01

<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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
put this function into B1 and then copy it down column B. =IF($A1>0,$A1,1)
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,210
Members
448,554
Latest member
Gleisner2

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?

Disable AdBlock

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

Disable AdBlock Plus

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
Back
Top