Macro to insert rows based on user selection

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.



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",

    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
        Exit Sub

    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.
        Exit Sub


    ' 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

    Range(Selection, Selection.End(xlUp).Offset(1, 0)).Select

1/21/2010 11:48:46 AM
excel 39880 articles. 0 followers. Follow

0 Replies

Similar Articles

[PageSpeed] 23


Similar Artilces:

deleting rows #4
In microsoft encell 2000 when i delete a row (say row 5) the row disapears but so does the row number then all other rows are moved up is there a way to delete the row and keep the number 5? The row number shouldn't disappear unless you are hiding the row, not deleting it -- HTH Nick Hodge Microsoft MVP - Excel Southampton, England "Doctorpc631" <> wrote in message news:e4FaNYV4EHA.1260@TK2MSFTNGP12.phx.gbl... > In microsoft encell 2000 when i delete a row (say row 5) the row > disapears > but s...

Macro warning with no macros
I have a workbook that when I try to open it I get the warning tellin me it contains macros and asking whether I want to Enable or Disabl them. I put no macros, no VB scripts, nothing at all in the workboo other than standard Excel formulas and some charts. I developed it i Excel 2002, SP-2, but have moved it back and forth between tw computers, the other which has Excel 97. Regardless of whether Enable or Disable or change the security settings so as not to get th warning, once the workbook is open even if I go t Tools-Macro-Macros... it lists no macros. How can I find out what's cau...

when inserting a chart excel is saying i cannot add any new fonts
When I attempt to insert a chart i am getting a message which says,"no new fonts may be added to this workbook". I am adding it to a billing process and I did the first 100 or so before it began this doing this, I am not adding any new fonts but I am getting this message, any suggestions on what is the cause so I can overcome it? I've described the problem and fix here: Error: "No more new fonts may be applied in this workbook" - Jon ------- Jon Peltier Peltier Technical Services, Inc. http://peltier...

Can anybody tell me how to kick off a macro automatically as file is opened? Regards ...

Copying a variable nuimber of rows
I want to copy the most recent 10 rows (excluding the header row) from worksheet A to worksheet B. My problem is how do I define a variable number of rows if there are fewer than 10 lines completed? I do not want to copy any blank lines. I am fairly new to VBA and am struggling to work out the code for this so any help will be gratefully appreciated. John Try With Activesheet LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row if LastRow > 10 Then FirstRow = LastRow - 9 Else FirstRow = 1 EndIf ...

inserting a picture
hi I need your expertise regarding inserting a picture in word 2007. When I hit the office button and go to open then my pictures, pick the picture i want and then click open. After that it brings up the convert file dialogue box and i cant bring up the picture. Can you help me with this problem? Thank you , Joe. Hi, I am not that sure why that problem is happening but i believe that it is having problem importing pictures that are in another format. I want to know a few things: Where did you get the picture? What is its file extension? Make sure that your pictu...

Version: 2008 Operating System: Mac OS X 10.4 (Tiger) Processor: intel Hi, I'm thinking of buying Mac: Office 2008, but before I do I want to check that I will be able to download Scritpsmart collection of templates without any glitches: <> Does anyone know if I can do this? Regards, SP. In article <ee98ff5.-1@webcrossing.caR9absDaxw>, wrote: > Version: 2008 > Operating System: Mac OS X 10.4 (Tiger) > Processor: intel > > Hi, > > I'm thinking of buying Mac: Of...

User removed Anonymous permission from to get it back
A user has deleted the Anonymous permissions from their calendar and I am unable to find a way to get it back. Does anyone know what can be done to add this back to their calendar. TIA In news:Xns9861A9314BAB8bobarcabca@, Fuzzy Logic <bob@arc.ab.caREMOVETHIS> typed: > A user has deleted the Anonymous permissions from their calendar and > I am unable to find a way to get it back. Does anyone know what can > be done to add this back to their calendar. TIA In that user's Outlook, right-click on Calendar, properties, permissions tab. Add Anonymous, and do whate...

Attached Toolbars Macros
Excel 2003 I have a custom tool bar, this tool bar has several items, each item is pointing to a VBA code. This tool bar is attached to the worksheet. I've copied this spreadsheet to several computers. After I copied the spreadsheet I decided to rename the spreadsheet, after I renamed the spreadsheet the Toolbar Item's macros were still pointing to the old name, I reasigned each tool bar item to the current spreadsheet, saved the spreadsheet and everything was working on my computer, but when I re-copied the new spreadsheet to other computers and I clicked on the Toolbar Item...

Changing Rows to Columns
Is it possible to easily rotate a spreadsheet or part of a spreadshet, to change the data in rows to columns? Do to a sorting problem in the charts I create, it seems I have to sort the data in the spreadsheet first. Any help would be appreciated. Hi, Sure is a way. select & copy your data. then select the cell where you want to re-paste; right-click and select paste special; in the dialog box check "transpose". Ok. jeff >-----Original Message----- >Is it possible to easily rotate a spreadsheet or part of a >spreadshet, to change the data in rows to columns? D...

Graphics Insert into Publisher Doc.
When ever I insert a graphic into my publisher docs. they appear as a series of dashes around the perimeter of the graphic & across the diagonals of the graphic not the actual graphic I am trying to work with. Can anyone advise of a fix as this is driving me insane. Regards, Longa Do you have detailed display enabled? View, pictures, detailed display... Otherwise you may need to update your graphics/video driver. Slide the acceleration down on your adapter, control panel, display folder, settings tab, advanced button, troubleshoot tab. If your images appear, then go to the manufacturer...

Run macro automatically.
How do I make a macro run automatically when a worksheet it is attached to is loaded? right click on the sheet tab>view code>left window worksheet>right window activate -- Don Guillett SalesAid Software "Excel macro" <Excel> wrote in message > How do I make a macro run automatically when a worksheet it is attached to is > loaded? I am unable to find "activate " when I right click on worksheet. ( i assume that you are referring to the windo...

Terminal Server based Outlook 2007 printing hieroglyphics
We are running terminal services(Server 2008 R2) with Outlook 2007 installed (SP2). No other Office components are installed. When printing emails, (all other printing is fine), the text becomes compressed and looks like hieroglyphics. 1. I have tried removing all printer drivers and re-installing the drivers with no luck. 2. I have also applied the lastest patches and Service packs to the server and Outlook. 3. I have tried using different types of drivers with each printer ie. PS, MS, PCL5e, PCL6 etc with no luck. Any help would be appreciated as users are getting very fr...

Help with macro/and or formula?
Perhaps this is a silly question, but i just can't figure out how t format this worksheet. The problem is that a lot of the cells hav numbers in them with negative signs in the back instead of in the fron (ie. 23-) and i was wondering if anyone knew of a macro/formula i coul use to quickly fix this problem for all the cells. I would b extremely grateful. Thanks -- brefed1 ----------------------------------------------------------------------- brefed15's Profile: View this thread:

Protected sheets and running macros
i have a sheet with a macro ran by a normal button at the top. Howeve when i protect the worksheet where ever i click on the sheet the macr executes. Can any one offer any advice on how to stop this? Thanks Robert -- Message posted from hi, i am not able to reproduce the problem, hence i may not be able t provide a panacea for that issue. here is what i suggest. i have a sheet that has a macro that is run when a control toolbo button (that is, not a form button) is clicked. this sheet is protected based on the user logged in. what i have don is to protect t...

Cannot insert new rows
I am running excel 2007 and I have 23 worksheets in a workbook. I have no links or hidden rows or colums. My biggest sheet is A1: AW8096. I use VB macros to add lines and move data from one set of workbooks to this one. I have built several sheets using this proceedure. All of a sudden I get the error "Cannot shift objects off sheet" when I run a routine I have 50,000 times. I have checked to make sure there are no hidden comments, row or columns. I have deleted most of the worksheets and I still get the error. I have made copies of the workbook and used different file ...

Selecting a printer
I have a small macro that is attached to a print button on sheet 1 that prints the graphs on sheet 2. My problem is that I wish to print to a printer that is not the default printer. Can the printer be selected automatically when the macro runs. This is what I have in the macro at present and the name of the printer is " Sub Print_Graphs() Sheets("Graphs").Select ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True Sheets("Data Sheet").Select Range("B18").Select End Sub try this WITHOUT selecting the sheet Application.Act...

SQL Statement Insert Into
I need to append data from one table1 to table2 using the field in table3 as the criteria. This field in question is 'account number'. How can I write a sql statement to accomplish this? Troy, What binds the tables? In other words, how are they related to each other? Unless the account number is the same for every entity in Table2? -- Gina Whipp 2010 Microsoft MVP (Access) "I feel I have been denied critical, need to know, information!" - Tremors II "Troy" <Troy@discussions.mic...

Macro Freezes Reading Pane
I have a macro that Sends/Receives Tasks from Project Server into Outlook Tasks. Everything is working fine, except one weird thing that happens. I have the Timer set on the macro and it starts as soon as Outlook starts. The macro gets the tasks from ProjectServer every hour. As soon as the macro starts running the Reading pane freezes in the Inbox. If i stop the macro then Reading pane is back to normal. Can anyone please tell me if there is something I can do in VBA to fix this annoying issue. Thanks a lot ...

Macro to remove rows?
Hi, I receive thousands of rows of data from an extract that comes from a database. The data extract automatically generates page headers that need to be removed, and I would love to automate this process. The headers do have a pattern to them, but I'm not clever enough to figure out a good way to use that to write a macro. The first row of the header starts with hyphens ------------------------------- Then there's a variable number of rows of header data to delete followed by another row of hyphens ------------------------------- Then 2 additional rows that must be deleted. The nu...

Calling on a macro within a macro!
Hey All!! Quick Question - How do you call up a macro within a current macro? I have one big macro and I need it to jump to several other macros when it runs! Does it matter where the other macros are stored or is it better to have them all in one module? Help appreciated Thnx! Hi Mellowe, '=============>> Public Sub Main() 'Your code Call One 'your code End Sub '<<============= '=============>> Public Sub One() MsgBox "Hi from One" End Sub '<<============= > Does it matter where the other macros are stored or ...

Turn off auto adjust of row heights
I have a complex spreadsheet for my catalogue of products. I have set row heights, fonts and sizes of texts for different rows. Every now and then my row heights are automatically changed by EXCEL. I have turned off word wrap I have locked/protected my sheet. How can I stop EXCEL automatically readjusting row heights at its discretion. Thanks ...

shortcut keys to macros
Hi, Is it at all possible to assign shortcut keys to macros in Outlook? Thx, Hans ...

Custom OWA interface per user or group
Is it possible to change features available to users on an OWA page based on group membership or a per user basis. What I am trying to accomplish is to limit certain users ability to do things or see things on the OWA page. For 90% of my users I want them to see and be able to use everything the Premium OWA site has to offer with no restrictions. But for some users, I want to remove the Options page link, remove the delete button, etc... Things like this. Also, how can I prevent these users from deleting items in their own mailbox. These are low grade users and I don't want them to...

insert category Id into Items table
Can some please help me how to insert the ID from Category and Department tables into the Items table. I have the does some one have the correct SQL for this Thank you jm can u explain it further? "jm" wrote: > Can some please help me how to insert the ID from Category and Department > tables into the Items table. I have the does some one have the correct SQL > for this > Thank you > jm ...