dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,375
- Office Version
- 365
- 2016
- Platform
- Windows
I need some help. I am still learning to code and I don't yet fully understand vba. I have the following code and it is keeps giving me errors:
The code needs to cycle through rows in a table called tblCosting. The table is designed to record costings. Here are some screen shots of the table:
https://www.screencast.com/t/U1SqbfXoS
https://www.screencast.com/t/tKcbK8r8
The table is located on a sheet called Home and currently has two rows of demo data. Data is entered up to column N and the rest is calculated automatically. The 3 columns on the end will also have data entered into them. Columns R to AC will be hidden from view as they are just used for calculation purposes. The three columns on the end will be visible however. There are sheets for every month of the year. When x number of rows have been entered, my supervisor wants to be able to press a button and have them all transferred to the relevant sheet depending on the date of the costing, for instance, if one row has the date in July 2018, that particular row will get transferred to the July 2018 sheet.
Only some of the rows are transferred and here is a screenshot of the sheets (they are all the same) where the information goes.
https://www.screencast.com/t/QOFZb2yc4
Can someone help me with the code please?
Thanks
Code:
Sub cmdCopyr()
Application.ScreenUpdating = False
'declare variables
Dim TableLastRow As Long
Dim LastRow As Long
Dim TableNumberRows As Long
Dim iCounter As Long 'counter of for..next loop
Dim tblRows As Integer 'number of rows in tblCosting
Dim tblrow As ListRow 'specific row in tblCosting
Dim Combo As String 'Combo worksheet name
Dim sht As Worksheet
Dim Start As Long 'number of first row in tblCosting
Dim Costing As ListObject
'assign values to variables
Set sht = Worksheets("home")
TableLastRow = Split(Sheets("Home").ListObjects("tblCosting").DataBodyRange.Address, "$")(4) 'last row in tblCosting of home sheet
Combo = Worksheets("Home").Range("Y5") 'concaternation of cells to get name of worksheet to put data
LastRow = Worksheets(Combo).Cells(Rows.Count, "A").End(xlUp).Row + 1 'number of first empty row in column A of Combo
'Costing =
TableNumberRows = ListObjects("tblCosting").Range.Rows.Count
Start = "5"
For iCounter = Start To TableLastRow Step 1
ListObjects("tblCosting").Range.Cells(iCounter, 1).Resize(, 10).copy
With Worksheets(Combo).Cells(LastRow, iCounter)
.PasteSpecial Paste:=xlPasteValues
.Columns("A").NumberFormat = "dd/mm/yyyy"
End With
ListObjects("tblCosting").Range.Cells(iCounter, 1).copy
Worksheets(Combo).Cells(LastRow, 11).PasteSpecial Paste:=xlPasteValues
ListObjects("tblCosting").Range.Cells(iCounter, 30).Resize(, 32).copy
Worksheets(Combo).Cells(LastRow, 14).PasteSpecial Paste:=xlPasteValues
Next iCounter
'Check if activities is selected in column I
If Worksheets("home").Range("I5") = "Activities" Then
Worksheets(Combo).Range("K" & LastRow).Value = Worksheets("home").Range("AC" & LastRow) 'copy formula to total column if activities are selected
'Worksheets(Combo).Range("I" & Lastrow).ClearContents 'clear GST component if activities are selected
Else
Worksheets(Combo).Range("L" & LastRow).Formula = "=K" & LastRow & "*.1" 'if activities are not selected, GST formula will be added in to column H
Worksheets(Combo).Range("M" & LastRow).Formula = "=L" & LastRow & "+K" & LastRow 'if activities are not selected, add formula in to column I that will sum columns G + H
End If
Worksheets(Combo).Columns("H:J").NumberFormat = "$#,##0.00" 'format values in columns G to I of Combo
Call SortDates 'format cells to be in ascending date order
Application.CutCopyMode = False 'cancel Cut or Copy mode
Application.ScreenUpdating = True
End Sub
The code needs to cycle through rows in a table called tblCosting. The table is designed to record costings. Here are some screen shots of the table:
https://www.screencast.com/t/U1SqbfXoS
https://www.screencast.com/t/tKcbK8r8
The table is located on a sheet called Home and currently has two rows of demo data. Data is entered up to column N and the rest is calculated automatically. The 3 columns on the end will also have data entered into them. Columns R to AC will be hidden from view as they are just used for calculation purposes. The three columns on the end will be visible however. There are sheets for every month of the year. When x number of rows have been entered, my supervisor wants to be able to press a button and have them all transferred to the relevant sheet depending on the date of the costing, for instance, if one row has the date in July 2018, that particular row will get transferred to the July 2018 sheet.
Only some of the rows are transferred and here is a screenshot of the sheets (they are all the same) where the information goes.
https://www.screencast.com/t/QOFZb2yc4
Can someone help me with the code please?
Thanks