dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,362
- Office Version
- 365
- 2016
- Platform
- Windows
I have a spreadsheet that is populated from another sheet. Here is a screen shot of it https://www.screencast.com/t/6YANfjeXWzov. As each line is entered and copied across, the code places the new line under the previous line. What is the vba code to enter a formula in columns H and I for each row that I copy across. The formula for the cell H4, must be G4*0.1 and the formula for I4 must be G4+H4. Obviously the references need to change with the row that the new entry is being pasted into. So the second entry into the worksheet must be on row 5 instead of 4.
Here is my code to copy to the sheet.
Here is my code to copy to the sheet.
Code:
Sub cmdCopy2()
Worksheets("home").Unprotect Password:="costings"
Application.ScreenUpdating = False
Dim Lastrow As Long
Lastrow = Worksheets("All Costings").Cells(Rows.Count, "B").End(xlUp).Row + 1
Worksheets("Home").Range("A5:F5").copy
'work with cell at intersection of LastRow and column A of All Costings worksheet
With Worksheets("All Costings").Cells(Lastrow, 1)
'paste values
.PasteSpecial Paste:=xlPasteValues
'format date
.Columns("A").NumberFormat = "dd/mm/yyyy"
'left align the date cell in column A
.HorizontalAlignment = xlLeft
End With
'assign number of first empty row in column B of Combo to Lastrow
'Lastrow = Sheets("All costings").Cells(Rows.Count, "A").End(xlUp).Row
'copy value in cell K5 of home worksheet
Worksheets("Home").Range("K5").copy
'paste value in cell at intersection of Lastrow and column E of Combo
Worksheets("All costings").Cells(Lastrow, 7).PasteSpecial Paste:=xlPasteValues
Worksheets("Home").Range("L5").copy
Worksheets("all costings").Cells(Lastrow, 8).PasteSpecial Paste:=xlPasteValues
Worksheets("Home").Range("M5").copy
Worksheets("all costings").Cells(Lastrow, 9).PasteSpecial Paste:=xlPasteValues
If Worksheets("home").Range("E5") = "Activities" Then
0
Else
'I tried to enter something here but I had no idea
Worksheets("All Costings").Range("Lastrow, 8").Formula = "G",Lastrow * .1
End If
'copy value in cell J5 of Home worksheet
' Worksheets("Home").Range("j5").copy
'paste value in cell at intersection of Lastrow and column E of Combo
' Worksheets(Combo).Cells(Lastrow, 5).PasteSpecial Paste:=xlPasteValues
'format values in columns G to I of Combo
Worksheets("all costings").Columns("G:I").NumberFormat = "$#,##0.00"
'format cells to be in ascending date order
Call SortDates
'cancel Cut or Copy mode
Application.CutCopyMode = False
'turn screen updating on
Application.ScreenUpdating = True
'Worksheets("home").Protect Password:="costings"
End Sub