Reputation: 159
I need help with an "if range is nothing then" part of my code.
Let's say I have 4 worksheets:
Project 1 worksheet - should be skipped, filtered range is nothing, project number not in raw file
Project 2 worksheet - code needs to run, fitlered range is not nothing, project number exists
Project 3 worksheet - should be skipped, filtered range is nothing, project number not in raw file
Project 4 worksheet - code needs to run, fitlered range is not nothing, project number exists
Project 3 is not being skipped, the not nothing range of the last worksheet (Project 2) is being read. Kindly advise.
Code:
' dim declarations...
' set assignments....
If Worksheets("Raw").AutoFilterMode = True Then
Worksheets("Raw").AutoFilterMode = False
End If
With ws_raw
int_last_row_of_raw = .Cells(.Rows.Count, 1).End(xlUp).Row
int_last_col_of_raw = 16
Set rng_raw = .Range("A2", .Cells(int_last_row_of_raw, int_last_col_of_raw))
End With
With ws_raw.Sort
.SortFields.Clear
.SortFields.Add2 Key:=Range("A2"), Order:=xlAscending
.SortFields.Add2 Key:=Range("L2"), Order:=xlDescending
.SetRange rng_raw
.Header = xlYes
.Apply
End With
For Each ws In ThisWorkbook.Worksheets
If Not ws Is ws_raw _
And Not ws Is ws_master_tracker _
And Not ws Is ws_title_page _
And Not ws Is ws_sample _
And Not ws Is ws_closing _
And Not ws Is ws_ref _
And Not ws Is ws_pdf_template _
And Not ws.Visible = xlSheetHidden Then
project_name = ws.Range("E3").Value
With rng_raw
.AutoFilter 1, project_name
End With
On Error Resume Next
Set rng_filtered_raw = ws_raw.Range("J3", ws_raw.Cells(int_last_row_of_raw, int_last_col_of_raw)).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
' not doing its thing on project 3 worksheet
If rng_filtered_raw Is Nothing Then
If project_name = "" Then
project_name = "[BLANK]"
End If
not_found_project_numbers = not_found_project_numbers & ws.Name & " : " & project_name & vbNewLine
MsgBox ("skipped, project number not found, moving to the next ws")
Else
int_last_row_of_ws = 46
For int_current_row_of_ws = 11 To int_last_row_of_ws
cell_value = ws.Cells(int_current_row_of_ws, 3).Value
Select Case cell_value
'Concept Stage
Case Is = "Creation,Overview,Concept,Scope,Potential"
module_to_look_for = "Project Creation"
Case Is = "Product Briefing !"
' 20 or 30 more cases
' Others that are manually typed
Case Else
module_to_look_for = "MANUAL"
End Select
If Not rng_filtered_raw Is Nothing Then
If module_to_look_for = "MANUAL" Then
' Do nothing
' Highlight cell, etc.
Else
look_up_result = Application.WorksheetFunction.VLookup(module_to_look_for, rng_filtered_raw, 3, False)
If look_up_result = "" Then
ws.Cells(int_current_row_of_ws, 56).Value = "Blank Date!"
Else
ws.Cells(int_current_row_of_ws, 56).Value = look_up_result
End If
End If
End If
Next int_current_row_of_ws
End If
End If
Next ws
Upvotes: 0
Views: 55
Reputation: 1691
Just to expand a bit on my comment above, which seems to have done the trick:
Due to On Error Resume Next
whenever the the range object cannot be set, the range object is not set to nothing, rather it just remains set to whatever it was set to in the previous iteration of the loop. (Without the On Error Resume Next
statement, you would get a run-time error whenever the rng_filtered_raw
range object cannot be set). For this reason, your conditional statement below to catch when the range object is nothing does not work as intended.
Hence, adding Set rng_filtered_raw = Nothing
before the On Error Resume Next
statement should fix this problem.
Upvotes: 1