Help with VBA preventing duplicate selections

y_ahmadi

New Member
Joined
Sep 12, 2017
Messages
4
Hi all,

Im newbie in VBA and i have an issue about duplicate selection.

im using this code monthlyfile = Application.GetOpenFilename("Text Files (*.txt), *.txt") in my macro to select a specific file and fetch the data from it. Example: im selecting Jan 2017 file.

I need a code to prevent me from selecting the same file which is Jan 2017 because its already selected before when Im supposed to select Feb 2017.

Much appreciated

ATTN: IM NEWBIE, please try to keep the discussion as simple as possible :nya::laugh:
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Welcome to the board.

Can you post your whole code please? That alone isn't enough to suggest how to test if selected text file has already been opened.
 
Upvote 0
Here you go

Sub Data_Import()
'
' Data_Import Macro
'


'
ChDir "C:\Users\pcadmin\Documents\VBA DUBAI\VBA"

monthlyfile = Application.GetOpenFilename("Text Files (*.txt), *.txt")

Workbooks.OpenText Filename:= _
monthlyfile, Origin:=437, _
StartRow:=4, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(11 _
, 1), Array(23, 1), Array(28, 1), Array(43, 1), Array(53, 1)), TrailingMinusNumbers:= _
True
' Sheets("Nov2007").Select (Crap statement)
ActiveSheet.Move Before:=Workbooks("Monthly Update.xlsm").Sheets(1)
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Range("A1").Select
End Sub
 
Upvote 0
Untested, try:
Code:
Sub Data_Import_v1()

    Dim strFilesSoFar   As String
    Dim strMonthly      As String
    
    ChDir "C:\Users\pcadmin\Documents\VBA DUBAI\VBA"
    
    Application.ScreenUpdating = False
    
    Do Until InStr(strFilesSoFar, strMonthly) = 0
        strMonthly = Application.GetOpenFilename("Text Files (*.txt), *.txt")
        strFilesSoFar = strFilesSoFar & strMonthly & "|"
        
        If InStr(strFilesSoFar, strMonthly) > 0 Then
            MsgBox "Please select a different text file", vbExclamation, "Text File Already Opened"
        Else
            Workbooks.OpenText FileName:=strMonthly, origin:=437, StartRow:=4, DataType:=xlFixedWidth, fieldinfo:=Array(Array(0, 1), _
                Array(11, 1), Array(23, 1), Array(28, 1), Array(43, 1), Array(53, 1)), TrailingMinusNumbers:=True
            
            With ActiveSheet
                .Move before:=ThisWorkbook.Sheets(1)
                .Cells(1, 2).EntireRow.Delete
                Application.Goto Cells(1, 1), True
            End With
        End If
        
        If MsgBox("Select another file?", vbYesNoCancel, "Import Another Data File") <> vbYes Then Exit Do
    Loop
    
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
Appreciate your response, i have a debug message poping up.

Sorry i forget to mention it earlier, is it possible to pop up a box when selecting the same file again? i mean a box with a message (This file has been already imported)
 
Upvote 0
On what line does that debug message occur on? Click debug when the box appears and it should highlight a line in the VBE editor window.
 
Upvote 0
Made a few changes, try:
Code:
Sub Data_Import_v1()


    Dim strFilesSoFar   As String
    Dim strMonthly      As String
    
    ChDir "C:\Users\pcadmin\Documents\VBA DUBAI\VBA"
        
    Application.ScreenUpdating = False
    
    Do
        strMonthly = Application.GetOpenFilename("Text Files (*.txt), *.txt")
        strFilesSoFar = strFilesSoFar & strMonthly & "|"
        
        If InStr(strFilesSoFar, strMonthly) > 0 Then
            MsgBox "Please select a different text file", vbExclamation, "Text File Already Opened"
        Else
            Workbooks.OpenText FileName:=strMonthly, origin:=437, StartRow:=4, DataType:=xlFixedWidth, fieldinfo:=Array(Array(0, 1), _
                Array(11, 1), Array(23, 1), Array(28, 1), Array(43, 1), Array(53, 1)), TrailingMinusNumbers:=True
            
            ActiveSheet.Move before:=ThisWorkbook.Sheets(1)
            Cells(1, 2).EntireRow.Delete
            If Intersect(ActiveCell, Cells(1, 1)) Is Nothing Then Application.Goto Cells(1, 1), True
        End If
        
    Loop Until MsgBox("Select another file?", vbYesNoCancel, "Import Another Data File") <> vbYes
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
On what line does that debug message occur on? Click debug when the box appears and it should highlight a line in the VBE editor window.


on this line

Else
Workbooks.OpenText FileName:=strMonthly, origin:=437, StartRow:=4, DataType:=xlFixedWidth, fieldinfo:=Array(Array(0, 1), _
Array(11, 1), Array(23, 1), Array(28, 1), Array(43, 1), Array(53, 1)), TrailingMinusNumbers:=True
 
Upvote 0
Using a ChDir path on my PC and a random text file, I do not get an error message on that line I'm afraid. What does the error message say?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top