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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

OTOTO

Board Regular
Joined
Dec 23, 2013
Messages
209
put this function into B1 and then copy it down column B. =IF($A1>0,$A1,1)
 

farmerscott

Well-known Member
Joined
Jan 26, 2013
Messages
813
Office Version
  1. 365
Platform
  1. Windows
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
Joined
Dec 23, 2013
Messages
209
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
Joined
Jan 26, 2013
Messages
813
Office Version
  1. 365
Platform
  1. Windows
OTOTO,

I see what you mean.

FarmerScott
 

Forum statistics

Threads
1,137,301
Messages
5,680,705
Members
419,929
Latest member
Atlas Quinn

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
Top