-
Hi Larry,
Cannot use the file you zipped, it was a .RAR file and not an .xls file.
If the sheet's protected it has to be unprotected for the code to work. (as in the example below)
This is "Worksheet Event" code, the event in this case being that a new cell has been selected. Your VBA Help files are a good source of information about this.
LastCol is the column that is the 'trigger' column, i.e. as soon as a cell is selected anywhere in that column, the code is triggered and a new row - with the code and all formats of the row above - is inserted directly below the selected cell.
I see from the image in your word attachment that your data goes from column B to column K. Now, assuming that you want to make manual entries in every cell in the row you're working on, the LastCol in that case will be the next column, column L.
However I see that column J has the header "Plus Tax" and column K has the header "Total" so I'm guessing that there are probably formulas for cells in both those columns. If that's the case, then the LastCol is column J. Adjust it to suit so that the code is triggered as soon as you hit enter after making your last manual entry in the row.
Example workbook attached...
[vba]Option Explicit
'
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'
Dim Cell As Range
'**************************
'put your own last column below
Const LastCol As String = "$L"
'**************************
'
ActiveSheet.Unprotect password:=""
UsedRange.Locked = False
'
'//if reached the last entry in this row
If Left(Target.Address, 2) = LastCol Then
'
'//insert a new row
Rows(Target.Row + 1).Insert Shift:=xlDown
'
'//copy the row
Rows(Target.Row).Copy
'
'//paste the formats in the new row
Rows(Target.Row + 1).PasteSpecial xlPasteFormats
'
'//copy the formulas to the new row
For Each Cell In Range("B" & Target.Row, LastCol & Target.Row)
If Cell.HasFormula Then Cell.Offset(1, 0) = Cell.FormulaR1C1
Next
'
'//select column A in the new row for next entry
Intersect(Target.EntireRow, Columns(2)).Offset(1, 0).Select
End If
'
ActiveSheet.Protect password:=""
'
End Sub[/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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules