Consulting

Page 3 of 4 FirstFirst 1 2 3 4 LastLast
Results 41 to 60 of 63

Thread: Solved: Insert Row Macro/VBA

  1. #41
    Ok, so, in the code below, (from your erlier post), this can only affect the last cell that can be selected or "active" because it is in the Private Sub - "SelectionChange". Correct??

    [vba] '//if reached the last entry in this row
    If Left(Target.Address, 2) = LastCol Then
    Application.EnableEvents = False
    '
    [/vba]

    If so, can Private Sub Change be used directly to ;

    [vba]Private Sub Worksheet_Change(ByVal Target As Range)

    '
    Dim Cell As Range
    '
    Const LastCol As String = "$M"
    '**************************
    '
    If Target.Row.Count = 0.00 Then Exit Sub
    '
    If Changed = True And Target.Row = Range("B":"M" & Rows.Count).End(xlUp).Row Then
    '
    On Error Goto Finish
    ActiveSheet.Unprotect password:=""
    Application.EnableEvents = False
    '
    '//insert new row
    Rows(Target.Row + 1).Insert Shift:=xlDown
    '
    '//copy row
    Rows(Target.Row).Copy
    '
    '//paste formats in the new row
    Rows(Target.Row + 1).PasteSpecial xlPasteFormats
    Rows(Target.Row + 1).Borders(xlEdgeTop).LineStyle = xlNone
    '
    With Range("M" & Target.Row + 1).Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .Weight = xlThin
    End With
    '
    '//copy formulas to the new row
    For Each Cell In Range("B" & Target.Row, "M" & Target.Row)
    If Cell.HasFormula Then Cell.Offset(1, 0) = Cell.FormulaR1C1
    Next
    '
    Range("B" & Target.Row + 1).Select
    Changed = False
    End If
    '
    End If
    Finish:
    Application.EnableEvents = True
    ActiveSheet.Protect password:=""

    End Sub [/vba]
    Last edited by LarryLaser; 04-27-2006 at 02:50 PM.
    LarryLaser

    neogeek in training

  2. #42
    Remember that the Last 3 cells (col's K,L and M) are locked and hidden.
    LarryLaser

    neogeek in training

  3. #43
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    No, the change and selection change take place at different times... try the last code I just posted.
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  4. #44
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    That works great John
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #45
    I did, it still functions the same way, I am trying to get it to only fire IF
    the last column, of Range B:M, and cell "M" is not <> 0.00, Then
    LarryLaser

    neogeek in training

  6. #46
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    If I read your intent correctly, I think this will give you what you want.
    In the last code I posted, change
    [vba]'//if past the last manual entry in this row
    If Changed = True And Target.Row = Range("B" & Rows.Count).End(xlUp).Row And Target.HasFormula Then[/vba]
    To:
    [vba]'//if past the last manual entry in this row
    If Changed = True _
    And Target.Row = Range("B" & Rows.Count).End(xlUp).Row _
    And Target.HasFormula _
    And Range(LastFormulaCol & Target.Row) <> 0 Then[/vba]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  7. #47
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Take a look at this one, same sheet as above Larry....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #48
    Hey John
    Sorry man I get nothing. I can't see why not either It looks like it should but it doesn't do any thing.

    Steve
    I tried yours, it still does the same as the problem John and I had back on test 2. all cell protection is lost.
    LarryLaser

    neogeek in training

  9. #49
    Hey Steve
    Once I reset the protection then no change on new data entry.
    LarryLaser

    neogeek in training

  10. #50
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by LarryLaser
    Hey John
    Sorry man I get nothing. I can't see why not either It looks like it should but it doesn't do any thing...
    Well it should, it works fine on my machine. The arguments are now such that the row selected must

    i) be the next empty row, and
    ii) Column B (the State tax setting) must then have an entry made in it, and
    iii) a monetary amount must also be entered somewhere in the row (to make the total in column M > 0)

    Then, if a cell with a formula on the same row is selected the rest of the code is triggered and a new row inserted.
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  11. #51
    Hey John

    Quote Originally Posted by johnske
    "Then, if a cell with a formula on the same row is "SELECTED" the rest of the code is triggered and a new row inserted.
    The cell's that have formula's in them are all protected and can not be selected.
    LarryLaser

    neogeek in training

  12. #52
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by LarryLaser
    Hey John



    The cell's that have formula's in them are all protected and can not be selected.
    You can scroll across to them can't you (they are then selected). Sheet protection doesn't stop you 'selecting' a cell, it only stops you changing the selected cell if it's protected.
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  13. #53
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I'm with John on this one, the cells are protected.....? you can click on the cell but you can't see the formula or change the cell....

    works fine on my machine also.?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  14. #54
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Larry,
    try downloading the attachment from post 47 again, when you open it put a number in the left column(tax) then tab across the column and add an expense as you go, then tab on across the sheet.......

    row is added and sheet is protected. Then you can select that row again and tab across it and nothing happens......isn't that what you were after?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  15. #55
    Hey Steve and John
    If the cells have no protection you can select any cell anywhere, including areas off the sheet. The sheet then loose ist containment. I want it to only have data entry ranges to be selectable.
    LarryLaser

    neogeek in training

  16. #56
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I may be wrong but I think your out of luck there Larry.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  17. #57
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    what do you mean by lose its containment?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  18. #58
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    The only way you could have more control in my opinion would be to use a userform......might be a good idea for your purpose.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  19. #59
    That's a future function I plan on adding to the front page (Sheet1). but I am not ready to get there yet.

    Thanks for your help Steve

    You Too John

    Larry
    LarryLaser

    neogeek in training

  20. #60
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Here's a simple data entry form to get you started....look it over and there are a million things you can do with it.

    I just noticed it doesn't have option explicit on the code page for the form so there is probably a variable you will need to dim eventually but it will give you some ideas. Spread sheet can just be used to store your data, even be hidden and everything can be done with the form.....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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