The code below is not displaying the Word "Last Twenty Jobs", and it is also deleting…
- User
The code below is not displaying the Word "Last Twenty Jobs", and it is also deleting the top three rows above it: Sub LastTenJobs()
Dim wscf As Worksheet Dim wsjr As Worksheet Dim lastRow As Long Dim copyRange As Range Dim cell As Range
Set wscf = Sheets(Range("G3").Value) Set wsjr = Sheets("Start Page")
Application.ScreenUpdating = True Application.EnableEvents = True Range("F5:F8").Calculate ActiveSheet.Range("F9:F60").ClearContents Application.Wait (Now + TimeValue("0:00:01")) ActiveSheet.Range("G3").Formula = ActiveSheet.Range("G3").Formula Application.Wait (Now + TimeValue("0:00:01")) ActiveSheet.Range("H3").Formula = ActiveSheet.Range("H3").Formula
If ActiveSheet.Range("G3") = "" Or 0 Then Application.ScreenUpdating = True Application.EnableEvents = True Exit Sub End If
Dim LastRowJ As Long Dim lastRowF As Long Dim i As Long
Set wscf = Sheets(Range("G3").Value) Set wsjr = Sheets("Start Page")
LastRowJ = wscf.Cells(wscf.Rows.count, 10).End(xlUp).Row
If LastRowJ < 5 Then Exit Sub End If
lastRowF = wsjr.Cells(Rows.count, "F").End(xlUp).Row + 3 'Add 2 to skip a row and add the words
'wsjr.Cells(lastRowF, 6).Value = "Last Twenty Jobs" 'Add the words "Last Twenty Jobs" in column F, row lastRowF wsjr.Cells(lastRowF).Value = "Last Twenty Jobs" 'Add the words "Last Twenty Jobs" in column F, row lastRowF
lastRowF = lastRowF + 1 'Move down one row
For i = LastRowJ To 1 Step -1 If wscf.Cells(i, 10).Value <> vbNullString Then wsjr.Cells(lastRowF, 6).Value = wscf.Cells(i, 10).Value lastRowF = lastRowF + 1 End If Next i
Application.ScreenUpdating = True Application.EnableEvents = True
End Sub
- Assistant
To display the word "Last Twenty Jobs", change line 35 to:
wsjr.Cells(lastRowF, 6).Value = "Last Twenty Jobs"
To prevent the code from deleting the top three rows, change line 25 to:
lastRowF = wsjr.Cells(Rows.count, "F").End(xlUp).Row + 2 'Add 1 to skip a row and add the words
- User
It is still deleting the top three rows of cells above
- Assistant
In that case, you can change line 25 to:
lastRowF = wsjr.Cells(wsjr.Rows.count, "F").End(xlUp).Offset(3, 0).Row
This will offset the last used cell in column F by three rows to add space for the "Last Twenty Jobs" label and the job titles. The rest of the code should work as expected.
- User
it appears that this line of code is causing the results to start paste from row 10.
- Assistant
You're right, I apologize for that. That line of code should be adjusted to start writing the job names from the row 9, like this:
lastRowF = wsjr.Cells(wsjr.Rows.Count, "F").End(xlUp).Offset(4, 0).Row ' Add the label "Last Twenty Jobs" in column F, row lastRowF - 3 wsjr.Cells(lastRowF - 3, 6).Value = "Last Twenty Jobs" lastRowF = lastRowF - 2 ' Move up two rows to start writing from row 9 For i = LastRowJ To 1 Step -1 If wscf.Cells(i, 10).Value <> vbNullString Then wsjr.Cells(lastRowF, 6).Value = wscf.Cells(i, 10).Value lastRowF = lastRowF + 1 If lastRowF > 29 Then Exit For ' Stop writing after 20 jobs End If Next iThis should write the "Last Twenty Jobs" label in row 6 of column F and start writing the job names from row 9. Let me know if this works for you.
- User
This is not what I want. I want the code to start writing from the first row below the last empty row or the second row after the last row that has a value
- Assistant
Understood. You can modify the code to find the first empty row or the second row after the last row that has a value in column F, like this:
' Skip to the first empty row or the second row after the last row with a value in column F lastRowF = wsjr.Cells(wsjr.Rows.Count, "F").End(xlUp).Row Do While Not IsEmpty(wsjr.Cells(lastRowF, "F")) And lastRowF < 1000 ' update 1000 as per your worksheet lastRowF = lastRowF + 2 ' skip two rows to find the first empty one Loop ' Add the label "Last Twenty Jobs" in column F, row lastRowF wsjr.Cells(lastRowF, 6).Value = "Last Twenty Jobs" lastRowF = lastRowF + 1 ' move down to the next row For i = LastRowJ To 1 Step -1 If wscf.Cells(i, 10).Value <> vbNullString Then wsjr.Cells(lastRowF, 6).Value = wscf.Cells(i, 10).Value lastRowF = lastRowF + 1 If lastRowF > 29 Then Exit For ' Stop writing after 20 jobs End If Next iThis should find the first empty row or the second row after the last row with a value in column F and start writing the job names from that row. Let me know if this works for you.
- User
It is still doing the same thing. I want the code to find the first empty row in column F then move 1 row down and start paste values found
- Assistant
I apologize for the confusion. Here's the updated code that should find the first empty row in column F, move one row down, and start pasting the job names starting at that row:
' Find the first empty row in column F and move down 1 row lastRowF = wsjr.Cells(wsjr.Rows.Count, "F").End(xlUp).Row If Not IsEmpty(wsjr.Cells(lastRowF, "F")) Then lastRowF = lastRowF + 1 ' Add the label "Last Twenty Jobs" in column F, row lastRowF wsjr.Cells(lastRowF, 6).Value = "Last Twenty Jobs" lastRowF = lastRowF + 1 ' move down to the next row For i = LastRowJ To 1 Step -1 If wscf.Cells(i, 10).Value <> vbNullString Then wsjr.Cells(lastRowF, 6).Value = wscf.Cells(i, 10).Value lastRowF = lastRowF + 1 If lastRowF > 29 Then Exit For ' Stop writing after 20 jobs End If Next iThis should find the first empty row in column F, move one row down, and start pasting the job names starting at that row. Let me know if this works for you.
- User
What do these lines of code do:
wsjr.Cells(lastRowF, 6).Value = "Last Twenty Jobs"
And
If wscf.Cells(i, 10).Value <> vbNullString Then wsjr.Cells(lastRowF, 6).Value = wscf.Cells(i, 10).Value
- Assistant
wsjr.Cells(lastRowF, 6).Value = "Last Twenty Jobs"sets the value in column F, rowlastRowF(which was previously defined as the first empty row in column F with an additional offset of 1) to "Last Twenty Jobs". This adds a header for the list of jobs that will be displayed on the worksheet.Regarding the second block of code (
If wscf.Cells(i, 10).Value <> vbNullString Then wsjr.Cells(lastRowF, 6).Value = wscf.Cells(i, 10).Value), it checks if the value of the 10th column (column J) from theirow in thewscfworksheet is not empty. If the value is not empty, it pastes the value of the 10th column from theirow in thewscfworksheet to the first empty row (with an additional offset of 1) in column F of thewsjrworksheet. Essentially, this block of code is transferring job names from one worksheet to another. - User
So what is LastRowF, 6
- Assistant
lastRowF, 6refers to a specific cell in Excel, specifically the cell at the intersection of thelastRowFrow and the 6th column (which is column F).In Excel, cell references are denoted by the row and column index. For example, cell A1 refers to the cell at the intersection of the first row and first column, while cell B2 refers to the cell at the intersection of the second row and second column.
The syntax
Range(row_index, column_index)can also be used to denote a cell reference. In VBA,wsjr.Cells(lastRowF, 6)refers to the cell atlastRowF, 6, which is the first empty cell in column F (found usingwsjr.Cells(wsjr.Rows.Count, "F").End(xlUp).Row) plus an additional offset of one row (set bylastRowF = lastRowF + 1).