Share via

Duplicate declaration in current scope

Anonymous
2022-03-14T17:05:00+00:00

Can anyone see an issue with this code that would cause the above error? It was running fine, before I added the Case "ITEM" into the mix. When the error shows up, VBA is high lighting the "App As Object" in the second Case.

Select Case ImportFile

        Case "IADJMATM", "Drawing Rev Excel" 

            Dim App As Object, FileTemp As String 

            Set App = CreateObject("Excel.Application") 

            App.Visible = False 

            FileTemp = CurrentProject.Path & "\Temp.xls" 

        Case "BoMChg"   'Added these lines to see if I can pull in BoM\_Chg.xls for the BoM\_Chg Imports 

            Dim App As Object, FileTemp As String 

            Set App = CreateObject("Excel.Application") 

            App.Visible = False 

            FileTemp = CurrentProject.Path & "\BoM\_Chg.xls" 

        Case "BoM"      'Added these lines to see if I can pull in BoM\_New.xls for the New BoM Imports 

            Dim App As Object, FileTemp As String 

            Set App = CreateObject("Excel.Application") 

            App.Visible = False 

            FileTemp = CurrentProject.Path & "\BoM\_New.xls" 

        Case "ITEM"     'Added these lines to see if I can pull in ITEM.xls for the ITEM Imports 

            Dim App As Object, FileTemp As String 

            Set App = CreateObject("Excel.Application") 

            App.Visible = False 

            FileTemp = CurrentProject.Path & "\ITEM.xls" 

            Me.CopyProgress = "Importing File " + Me.File.Column(0, i) 

            Me.Repaint 

            App.DisplayAlerts = False 

            App.Workbooks.Open (Me.File.Column(0, i)) 

            App.ActiveWorkbook.SaveAs FileTemp, FileFormat:=56 

            App.DisplayAlerts = True 

            App.Workbooks.Close 

    End Select
Microsoft 365 and Office | Access | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Anonymous
    2022-03-14T17:35:21+00:00

    Can anyone see an issue with this code that would cause the above error? It was running fine, before I added the Case "ITEM" into the mix. When the error shows up, VBA is high lighting the "App As Object" in the second Case.

    Hi darrend7,

    You have multiple declarations of

    Dim App As Object, FileTemp As String
    

    in the different Case parts.

    You could declare this in the beginning of the Sub, and remove from the Select instruction.

    But there is more duplcate code in the Select instruction. however not fatal.

    Imb.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2022-03-14T18:38:38+00:00

    That make sense. This is what I did to fix the issue.

        Dim App As Object, FileTemp As String 
    
        Set App = CreateObject("Excel.Application") 
    
        App.Visible = False 
    
        Select Case ImportFile 
    
            Case "IADJMATM", "Drawing Rev Excel" 
    
                FileTemp = CurrentProject.Path & "\Temp.xls" 
    
            Case "BoMChg"   'Added these lines to see if I can pull in BoM\_Chg.xls for the BoM\_Chg Imports 
    
                FileTemp = CurrentProject.Path & "\BoM\_Chg.xls" 
    
            Case "BoM"      'Added these lines to see if I can pull in BoM\_New.xls for the New BoM Imports 
    
                FileTemp = CurrentProject.Path & "\BoM\_New.xls" 
    
            Case "ITEM"     'Added these lines to see if I can pull in ITEM.xls for the ITEM Imports 
    
                FileTemp = CurrentProject.Path & "\ITEM.xls" 
    
                Me.CopyProgress = "Importing File " + Me.File.Column(0, i) 
    
                Me.Repaint 
    
                App.DisplayAlerts = False 
    
                App.Workbooks.Open (Me.File.Column(0, i)) 
    
                App.ActiveWorkbook.SaveAs FileTemp, FileFormat:=56 
    
                App.DisplayAlerts = True 
    
                App.Workbooks.Close 
    
        End Select
    

    Thank you for your help

    Was this answer helpful?

    0 comments No comments