Search code examples
excelvbavariablespublicambiguous

Excel VBA Public Variables are not recognized


In MS Excel, I am trying to declare some public variables to use them in several modules after. But I am getting this error:

Ambiguous name detected: datebck

This error is valid for all public variables that I declared.

The modules are like this:

Option Explicit
Public LastColumnBck As Long
Public wsbck As Worksheet
Public datebck As String
Public linebck As String
Public TargetRowBck1 As Integer
Public TargetRowBck2 As Integer
Public StationRowBck As Integer


Public Sub FindLastStationColumnBck()
Dim excelfilename As String
excelfilename = "ERTP-Construction Tracking Sheet " & "(" & datebck & ")" & ".xlsx"
Set wsbck = Workbooks(excelfilename).Worksheets(linebck)
TargetRowBck1 = wsbck.Application.WorksheetFunction.Match("Sand filling", wsbck.Range("A:A"), 0)
TargetRowBck2 = wsbck.Application.WorksheetFunction.Match("Asphalt", wsbck.Range("A:A"), 0) - 1
StationRowBck = wsbck.Application.WorksheetFunction.Match("Main Activity", wsbck.Range("A:A"), 0)
LastColumnBck = wsbck.Cells(StationRowBck, wsbck.Columns.Count).End(xlToLeft).Column
End Sub

UserForm Code:

Option Explicit

Private Sub CommandButton1_Click()
Unload UsrForm6
End Sub

Private Sub CommandButton2_Click()
datebck = TextBoxDateUsrForm6
linebck = ListBoxUsrForm6
Call Module20.Backfilling (*this is another module that should use public variables also*
End Sub

I tried this type of declaration before and they were working but in this situation what is missing although structure of code is exactly the same ?


Solution

  • So the error says basically that datebck is duplicated. Did you define it elsewhere? Maybe search for "datebck As" in your Code, maybe you declared it elsewhere