Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 30

Thread: How to keep Inserting Rows Command from moving other cells in a sheet

  1. #1
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location

    How to keep Inserting Rows Command from moving other cells in a sheet

    I have 2 command buttons on excel 2010.
    everytime i click on the commandbuttons for each the other tables and cells seems be to effected. like everything is out of place. is there a vba for my vba codes to keep the other cells still while the user select the new row button.

    the codes i have for my command buttons:


    Private Sub CommandButton2_Click()
    Sheets("Sheet1").Range("A32").Select
    ActiveCell.EntireRow.Insert Shift:=xlDown
    
    
    Sheets("Sheet1").Range("A32:D32").Select
    Selection.Borders.Weight = xlThin
    End Sub
    
    
    Private Sub CommandButton1_Click()
    Sheets("Sheet1").Range("A15").Select
    ActiveCell.EntireRow.Insert Shift:=xlDown
    
    
    Sheets("Sheet1").Range("A15:D15").Select
    Selection.Borders.Weight = xlThin
    
    
    End Sub
    Last edited by Paul_Hossler; 05-11-2018 at 02:16 PM. Reason: Added CODE tags - use the # icon to insert them

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    That's what InsertRows does -- pushs everything down

    You could save the row number above the data you do not want to move, insert the row with your macro, and then delete the saved row number
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    Quote Originally Posted by Paul_Hossler View Post
    That's what InsertRows does -- pushs everything down

    You could save the row number above the data you do not want to move, insert the row with your macro, and then delete the saved row number
    how can insert the rows with macros, how can i do that??

  4. #4
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    i was trying to get my rows to look like this. everytime i click new row. it moves everthing else
    Attached Images Attached Images

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    It looks like your 2 CommandButtons insert a new row 15 and a new row 32, but you don't want anything pushed down???

    This inserts a new row at 15, and deletes the old row at 18 which had been 17


    Option Explicit
    
    Private Sub CommandButton1_Click()
        Me.Rows("15:15").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Me.Rows("18:18").Delete Shift:=xlUp
    End Sub

    If you want anything other that that, you'll need to explain a lot more with an attachment and examples
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    i was saying i have a couple of other stuff on the sheet. its moving everything down and messing up my optionbuttons and causing them not to work. i was trying to see if there is someway i can have set to a limited of rows when u click on the command button.
    Last edited by Ladyj205; 05-14-2018 at 12:19 PM.

  7. #7
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    is there a way to keep the row instead of deleting ----->>> Me.Rows("18:18").Delete Shift:=xlUp

    because everytime i click the command button and it overrides and deletes my other tables thats under it.

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by Ladyj205 View Post
    is there a way to keep the row instead of deleting ----->>> Me.Rows("18:18").Delete Shift:=xlUp

    because everytime i click the command button and it overrides and deletes my other tables thats under it.
    I can't figure out what you're wanting to do.

    Post an example workbook with a description of what you'd like to end up with
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Set your Option Buttons and other controls to NOT move with cells

    Guessing about your columns
    Range("B17:C17").Insert w/Shift Down
    Range("B18:C18").Delete w/Shift Up
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  10. #10
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    Quote Originally Posted by Ladyj205 View Post
    i was saying i have a couple of other stuff on the sheet. its moving everything down and messing up my optionbuttons and causing them not to work. i was trying to see if there is someway i can have set to a limited of rows when u click on the command button.
    Quote Originally Posted by SamT View Post
    Set your Option Buttons and other controls to NOT move with cells

    Guessing about your columns
    Range("B17:C17").Insert w/Shift Down
    Range("B18:C18").Delete w/Shift Up
    so it will be like:

    Private Sub CommandButton1_Click()
    Range("38:38").Insert Shift:=xlDown,
    Range("46:46").Delete Shift:=xlUp
    End Sub

  11. #11
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    Quote Originally Posted by Paul_Hossler View Post
    I can't figure out what you're wanting to do.

    Post an example workbook with a description of what you'd like to end up with
    all i was saying was adding rows affects other tables and they were added to the wrong section. you on the right path with me. i was trying to figure how prevent the clashing with my other materials and deleting on my sheet. everytime the new row button has been click. i wanted the user to click the new row button as much they need without messing with other material. sorry for being difficult.


    i insert what problem i keep running into

    thank u
    Attached Images Attached Images
    Last edited by Ladyj205; 05-17-2018 at 10:03 AM. Reason: grammar/typos

  12. #12
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    so it will be like:

    Private Sub CommandButton1_Click()
    Range("38:38").Insert Shift:=xlDown,
    Range("46:46").Delete Shift:=xlUp
    End Sub
    That is the same as
    Private Sub CommandButton1_Click()
    Rows(38).Insert
    Rows(46).Delete
    End Sub
    Insert and Delete Rows is always Shift down and shift up, respectively. How can be otherwise?

    I was trying to show you how to Insert and Delete only part of a row. You only need to specify the Shift direction when working on part of a Row.

    We have no idea what your worksheet looks like, so we can not give you any precise advice. Your pretty pictures don't carry any usable information at all.

    You can upload a workbook by clicking the Go Advanced button and the Manage Attachments button on that page
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  13. #13
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by Ladyj205 View Post
    i insert what problem i keep running into

    thank u
    It would be a lot easier to suggest options if you would attach a workbook like people have suggested
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  14. #14
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    sorry . i hope this helps
    Attached Files Attached Files

  15. #15
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    OK, using your workbook, I clicked [New Row] two times ...

    Private Sub CommandButton1_Click()
    Me.Rows("16:16").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Me.Rows("26:26").Delete Shift:=xlUp
    End Sub
    ... and it inserted a new row 16 and deleted row 26 and then another new row 16 and then deleted row 26 again

    That all seems reasonable to me -- it did what you asked it to do


    Capture.JPG


    Now I do see 4 empty Group boxes on the worksheet and a lot of Option Button code ….

    Private Sub OptionButton_1_0_Click()
      If OptionButton_1_0 Then
      Range("D60").Value = 0
      End If
    End Sub
    
    Private Sub OptionButton_1_1_Click()
      If OptionButton_1_1 Then
       Range("D60").Value = 25
      End If
    End Sub
    … so I suspect that there are some things on your worksheet that were not in your attachment???
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  16. #16
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    yes it was somethings on my worksheet not mention. i was trying to re-do the whole thing. i forgot to take it out. sorry if there anyway to overide the new rows to keep it from deleting other rows.

  17. #17
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by Ladyj205 View Post
    yes it was somethings on my worksheet not mention. i was trying to re-do the whole thing. i forgot to take it out. sorry if there anyway to overide the new rows to keep it from deleting other rows.
    Don't know what 'override the new rows' means

    If you insert a row like that, then it will push the other rows down, as well as any controls unless you mark each control to "Don't Move or Resize with Cells" like SamT said in #9

    Capture.JPG
    

    If you just want to INSERT a new row 16 without deleting, just remove the second like


    Private Sub CommandButton1_Click()
    
    Me.Rows("16:16").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Me.Rows("26:26").Delete Shift:=xlUp
    
    End Sub

    If you want to REPLACE row 16 then maybe


    Private Sub CommandButton1_Click()
    
    Me.Rows("16:16").Delete Shift:=xlUp
    Me.Rows("15:15").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  18. #18
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    is there a way i can hide a number of rows if the user wanted to use a lot of rows.

  19. #19
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Yes


    rows("10:20").hidden=true
    
    or
    
    rows(30).resize(15).hidden = true
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  20. #20
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location
    Hi Paul,

    Everytime i insert a new row. my table with optionbuttons change and it places the value elsewhere.

    is there a way i can have my table to look like this when there are new rows insert. im still pulling hair but not as much.

    Attached Images Attached Images

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •