After running the code inspections in Rubberduck 2.0.11.2453, there are 4 Range references that get tagged as:
Member 'Range' implicitly references ActiveSheet
The ranges in question are referring to Named Ranges. Is it necessary to qualify a named range reference?
Private Sub RunORatio(ByVal TabNum As Integer)
Dim Start As Integer, Cat As Integer, iMth As Integer, CurrentRow As Integer, Report As Integer
Dim wsORatio As Worksheet, wsData As Worksheet, wsMacro As Worksheet
Dim sMap As String, Test As String
With ActiveWorkbook
Set wsMacro = .Worksheets("Macro")
Set wsORatio = .Worksheets("ORatio" & TabNum)
With wsORatio
sMap = "oratio" & TabNum & "map"
For CurrentRow = 1 To Range(sMap).Rows.Count '<---1 here
Test = Range(sMap).Cells(CurrentRow, 1) '<---1 Here
Set wsData = ActiveWorkbook.Worksheets(Test)
Start = Range(Range(sMap).Cells(CurrentRow, 2)).Row '<---2 Here
Report = wsMacro.Range(sMap).Cells(CurrentRow, 3)
For Cat = 0 To 12
For iMth = 1 To 12
wsORatio.Cells(Report + Cat, 7 + iMth) = wsData.Cells(Start + Cat, 37 + iMth)
Next iMth
Next Cat
Next CurrentRow
End With
End With
End Sub
As @Mat'sMug already pointed out, the inspection is providing you information about the code. What you do with that information is a matter of preference, coding sytle, etc.
In this case, the inspection isn't telling you that there is an error in your code, it's telling you that there is a potential that implicit object references will cause your code to behave in unexpected ways. The takeaway from this specific inspection is that you are letting Excel decide how to interpret which object you are referring to.
From the documentation for
Application.Range
:The last sentence is the first reason you shouldn't ignore this inspection -
Range
without a qualifier will throw if you have aChart
selected. This is the same reason why you should useWorkbook.Worksheets(foo)
instead ofWorkbook.Sheets(foo)
if you are assigning to aWorksheet
(this is actually on the Rubberduck inspection wish-list).The second reason is related to the first. As you correctly point out in the comments, "the named range has a sheet name as part of its reference", or rephrased "a named range can be assumed to be unique". But it doesn't have to be unique if you have more than one workbook open. Since
ActiveSheet
is always returned from theActiveWorkbook
at the time of the call, if theWorkbook
the code expects is not active, this code will either throw or return the incorrectRange
(if the otherWorkbook
coincidentally contains aRange
with the same name).Keep in mind that although the
With
block captures a hard reference toActiveWorkbook
,Application
does not - andActiveWorkbook
can change during the course of your code's execution. This all ties back to the reasons why you should avoid using theActiveFoo
objects - they introduce the possibility that errors will result from otherwise "correct" code.The solution is simple. Just add the dot in front of them. That way they'll be calling
Workbook.Range
via the captured hard reference from theWith
block instead ofApplication.Range
.Consider this common mistake:
Rows
is unqualified, and therefore implicitly refers to the active sheet and thereforeRows.Count
isn't necessarily the row count on "Sheet12". The code might work, but it could also result in a subtle bug wherelastRow
doesn't have the correct value because of this, depending on the content of the active sheet.Or this one:
See it? Because the
Key
parameter isn't qualified, the call is going to fail at run-time with error 1004 - "Method 'Range' of object '_Global' failed". That's 169 Stack Overflow questions. "Error 1004" yields 1465 Stack Overflow questions.Implicit references to the active worksheet are a very common cause of bugs.
Rubberduck's VBA code inspections are, like ReSharper's C# static code analysis, hints/suggestions. The tool is telling you that it's possible there's something here that could cause problems, or that makes the code less explicit than it could be.
Do you need to fully qualify every single
Range
call? Of course not - Rubberduck is merely letting you know that in these instances,ActiveSheet
is implicitly being referenced, that's all there is to it.You can always tell Rubberduck "look, I know what I'm doing", using the "Ignore once" quick-fix:
That "fix" inserts a special comment (internally, Rubberduck calls them "annotations") that instructs the inspection to ignore specific results, while leaving the inspection enabled:
These annotations have the advantage of telling the reader (future you, or whoever takes your code over) that there's something going on here.
Future versions will eventually support specifying
@Ignore
annotations once at module-level, to ignore all results of a particular inspection in an entire module.Note that the inspection is under the Maintainability and Readability Issues category.
Range("DefinedName")
isn't half as explicit and fail-safe as:Which gives you the same range, and reads like it's actually pulling a named range scoped at workbook level.