Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit Reference to ActiveSheet False Positives in Worksheet module #3569

Open
ThunderFrame opened this issue Nov 25, 2017 · 6 comments

Comments

@ThunderFrame
Copy link
Member

commented Nov 25, 2017

In a worksheet module, the use of Range is not implicitly referencing the ActiveSheet, it is referring to the sheet module within which it is used.

This is valid code, but the inspection fires nonetheless (although there doesn't seem to be a quickfix available, so I haven't tested what happens if a fix is applied):

Sub ComboBox1_Change()
    Range("price_zero").Copy Destination:=Range("price_on_view")
End Sub

Even so, my preference would be to be more explicit, by using Me.

Sub ComboBox1_Change()
    Me.Range("price_zero").Copy Destination:=Me.Range("price_on_view")
End Sub
@retailcoder

This comment has been minimized.

Copy link
Member

commented Nov 25, 2017

Gah, I meant to fix that a long time ago... Simply ignore document modules for that inspection.

@retailcoder

This comment has been minimized.

Copy link
Member

commented Nov 25, 2017

If we want the Me fix, we'll need a separate inspection for it (e.g. "Implicit reference to current instance" or something).. I'd only fire it in document modules though.

@ThunderFrame

This comment has been minimized.

Copy link
Member Author

commented Nov 25, 2017

Except we can't ignore document modules... Range in a Workbook module is still a reference to ActiveSheet.

@retailcoder

This comment has been minimized.

Copy link
Member

commented Mar 15, 2018

I believe with Wayne's work we are now in a position to fix this bug correctly.

@daFreeMan

This comment has been minimized.

Copy link
Contributor

commented Apr 13, 2018

Similarly, I'm getting a false positive on this code (in Access):

Option Compare Database
Option Explicit

Private XLClinic As Excel.Workbook

Private Sub PreProcessDataSheet(ByVal SheetName As String)

  LogManager.Log DebugLevel, "Quarterly Report.GenQuarterly.PreProcessDataSheet - SheetName: " & SheetName

  Dim CurSheet As Worksheet
  Err.Clear
  On Error Resume Next                           'it's expected for this to fail if the sheet hasn't been used yet.
'vvvvvv Error flagged here vvvvv
  Set CurSheet = XLClinic.Sheets(TempVars.Item("KeepWord").Value & SheetName)
  If Err.Number = 9 Then                         'we haven't marked this worksheet for keeping yet, so let's do it
    Err.Clear
    On Error GoTo EH                             'let's not ignore ALL errors...
    Set CurSheet = XLClinic.Sheets(SheetName)
    CurSheet.Name = TempVars.Item("KeepWord").Value & CurSheet.Name
    ProcessDataSheet CurSheet, Form_GenQuarterlyRpt.ProcessStatus.Value
  End If

  Exit Sub
  
EH:
  With Err
    If .Number = 9 Then
      MsgBox "An attempt has been made to assign a worksheet to this this report, but the worksheet doesn't exist." & vbCrLf & "Worksheet name: " & SheetName
    End If
  End With
  
End Sub
@daFreeMan

This comment has been minimized.

Copy link
Contributor

commented Apr 13, 2018

Also getting it here:

  Dim sourceXL As Excel.Workbook
  newSheet.Application.DisplayAlerts = False
  Set sourceXL = XLobj.Workbooks.Open(fileName:=excelFileName, ReadOnly:=True)
  newSheet.Application.DisplayAlerts = True

  sourceXL.Sheets(sourceSheetName).Range(rangeName).Copy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.