Welcome to Windows Help!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Macro to insert rows based on user selection

 
   Windows Help (Home) -> Microsoft Excel RSS
Next:  Suming or dividing elements from different tables  
Author Message
Mike Magill

External


Since: Jan 21, 2010
Posts: 1



(Msg. 1) Posted: Thu Jan 21, 2010 3:48 am
Post subject: Macro to insert rows based on user selection
Archived from groups: microsoft>public>excel (more info?)

I have a macro that allows a user to insert one or more rows based on
a user input box. This allows me to control which formulae get copied
into the new cells.

The macro works fine UNLESS the user scrolls around the screen before
making their selection. How can I resolve this?

This is the key part of the script.

Thanks,

Mike


Sub Row_Insertion()
'
' This macro inserts a user-specified number of rows
' and ensures that the relevant formulae are copied
' into the new rows.

Range("I3").Select ' Makes I3 the active cell and
Set rng = Nothing ' clears any selection made by the user

Application.ScreenUpdating = True ' Allows the screen to refresh
while the user is selecting a range

On Error Resume Next ' This prevents the macro from stopping if
an error occurs

'This Input Box requires the user to select the row(s) where they
want rows to be inserted

Set rng = Application.InputBox(prompt:="Select the row number(s)
at the point at which you wish to insert rows. " & vbNewLine &
vbNewLine & _
"Click on OK and the rows will be inserted " & _
"immediately above that point.", Title:="Inserting a row",
Type:=Cool

Application.ScreenUpdating = False ' Stops the screen refreshing
while the macro is running

' If no range is selected by the user protect the worksheet and
end the macro

If rng Is Nothing Then
Range("i3").Select
Exit Sub

Else
End If

rng.Select ' select the range chosen by the user

If Not Intersect(ActiveCell, Range("A1:IV5")) Is Nothing Then '
Check to see if the user has selected in the
MsgBox "You cannot insert a row in this area!" '
header area (rows 1-6) and end macro if so.
Range("i3").Select
Exit Sub

Else

' If a valid selection has been made insert the appropriate number
of rows and then
' copy the relevant formulae into the inserted rows. The formulae
copying is done one column
' at a time. Hence the multiple copy/paste commands below.

Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromRightOrBelow

Selection.End(xlToLeft).Select
Selection.End(xlDown).Select
Selection.Copy
Range(Selection, Selection.End(xlUp).Offset(1, 0)).Select
ActiveSheet.Paste

etc.etc.

 >> Stay informed about: Macro to insert rows based on user selection 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Filter rows/transactions to another sheet. - Hi I have a sheet with 6500 transactions in rows. Each row contains an account no, amount, etc I would like to have Excel automatically copy all the rows/transactions on each account to a separate sheet in the same workbook (one account per sheet)....

Excel 2008 hanges when deleting cells or rows -

Excel 2007 - Send a selection as a mail Message Body - Has the feature "Send a selection as an e-mail message body" been removed from Excel 2007? I can see how to send the whole worksheet as an e-mail message body but not a selection

Change 3rd cell colour based on cell 1 >= cell2 in range - Hi all - I have looked through the group for a solution to this particular problem but i think my inexperience is possibly stopping me from seeing the solution .. so apologies if it has already been answered many times previously. My problem is this : ...

Update one workbook based on another workbook from a part .. - Hello, I have a Excel workbook call "Parts Catalog" that shows the following example: Description Part# Amount Book 1458 23.00 Pen 3459 125.00 Note Pad 8943 ...
   Windows Help (Home) -> Microsoft Excel All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]