The following code does pretty much what I want it to doa. My only requirement is that…
- User
The following code does pretty much what I want it to doa. My only requirement is that it pastes only the last 20 values found in column 10: 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("F25:F28").Calculate ActiveSheet.Range("F5:F24").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 variables Set wscf = Sheets(Range("G3").Value) Set wsjr = Sheets("Start Page") 'Find the last row of column J in wscf lastRowJ = wscf.Cells(wscf.Rows.count, 10).End(xlUp).Row 'If the last row of column J is less than 5, then there is no data to copy If lastRowJ < 5 Then Exit Sub End If 'Copy data from the last row of column J to column F in wsjr lastRowF = 5 'For i = lastRowJ To lastRowJ - 20 + 1 Step -1 For i = lastRowJ To 1 Step -1 'If wscf.Cells(i, 10).Value <> 0 Then 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
Call IncompleteJobs
End Sub
- Assistant
To only paste the last 20 values found in column 10, you can replace the following line:
'For i = lastRowJ To lastRowJ - 20 + 1 Step -1 For i = lastRowJ To 1 Step -1
with:
For i = WorksheetFunction.Max(lastRowJ - 19, 1) To lastRowJ
This will ensure that only the last 20 values in column 10 are copied over to column F in wsjr.
- User
your code counts and presents thee values from the top of column J downwards. I need it to count from the bottom of column J and present the values upwards
- Assistant
To count from the bottom of column J and present the values upwards, you can replace the entire section of code that starts with
For i = lastRowJ To 1 Step -1with the following code:Dim arr() As Variant Dim pasteRange As Range Dim k As Long 'Check if there are more than 20 rows in column J If lastRowJ > 20 Then 'Copy the last 20 rows of column J to an array arr = wscf.Range("J" & lastRowJ - 19 & ":J" & lastRowJ).Value 'Clear the existing contents in column F wsjr.Range("F5:F24").ClearContents 'Paste the values from the array to column F starting from cell F24 Set pasteRange = wsjr.Range("F24:F" & 24 + UBound(arr, 1)) pasteRange.Value = WorksheetFunction.Transpose(arr) Else 'If there are less than or equal to 20 rows in column J, copy all the rows to column F from the last row upwards wsjr.Range("F24").Value = wscf.Range("J" & lastRowJ).Value For k = lastRowJ - 1 To 1 Step -1 If wscf.Range("J" & k).Value <> "" Then wsjr.Range("F" & k + 19 - lastRowJ).Value = wscf.Range("J" & k).Value End If 'Exit the loop if 20 rows have been copied to column F If k = lastRowJ - 19 Then Exit For Next k End If - User
Worksheet Failed Error on this line of code: wsjr.Range("F24").Value = wscf.Range("J" & LastRowJ).Value
- Assistant
The worksheet failed error can occur when the specified worksheet or range cannot be found. In this case, it is likely that either the
wsjrorwscfworksheet is not defined, or the range"J" & LastRowJdoes not exist in thewscfworksheet.You can try adding some error handling code to handle this. For example, you can add the following lines of code at the beginning of the
LastTenJobs()sub:On Error GoTo ErrorHandler '...existing code... Exit Sub ErrorHandler: MsgBox "An error has occurred. Error details: " & Err.Description, vbExclamation, "Error"This will create an error handler that displays a message box with the error details if an error occurs, and then exits the sub.
Alternatively, you can use the
Ifstatement to check if the worksheet and range exist before attempting to access them:If Not wscf Is Nothing And Not IsError(Application.Match("J" & lastRowJ, wscf.Rows(1), 0)) Then wsjr.Range("F24").Value = wscf.Range("J" & lastRowJ).Value '...existing code... Else MsgBox "The specified worksheet or range does not exist.", vbExclamation, "Error" Exit Sub End If
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.