Macros in Excel Spread sheets?

I want to write a macro where I can insert a line below the current line, format the cells to merge, bring the text to the top, and wrap the text. I know how to write the macro, however, how do I get the macro to work on any line in the workbook, not just the line I created it on?

2008-03-10T18:57:33Z

Keyboard Shortcut: Ctrl+m
'
Rows("4:4").Select
Selection.Insert Shift:=xlDown
Range("B:D").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.mergecells = False



How should I change it to make it work? What exactly do I need to type in?

I don't want the rows to be restricted to "4:4", just wherever I hit the command.

I figured out how to get it to restrict to only the columns I want, but I can't figure out how to get it to stay in the row that the unmerged, unwrapped cell started in.

Anonymous2008-03-10T12:49:02Z

Favorite Answer

Don't use absolute cell addresses. (Record a macro while you do what you want, and look at the code that gets saved.)

Something like this may help you:

Dim i
For i = 147 To 1 Step -2
Rows(i).Select
Selection.Insert <whatever>
Next i

If you just want it to work on one row, select the row first, then run the macro, written to have its effect on the selection.

Anonymous2016-04-06T11:23:20Z

You don't even need a macro. Format the cells as custom and then you can have three different formats, e.g. #,##0.00;[Red]-#,##0.00; This tells it to format with 2 decimal places for all values, negative values in red, and zeroes left blank.