I Think I Need a "Static" But I'm Not Sure

Shiseiji

Board Regular
Joined
Oct 23, 2009
Messages
214
Office Version
  1. 2019
Platform
  1. Windows
I'm trying to render the fNameAndPath in the code below into a cell on the worksheet the text was imported onto.

I "think" one option is to set the fNameAndPath as a static variable so it can be reused. But . . the code below isn't failing, but neither is it putting the fNameAndPath in the desired cell.

Thanks for taking a look.

Ron


Code:
Sub tm_ImportPastUsersText()
'Import Active Directory file
' Validated mm-dd-yyyy
'
    
    Application.DisplayAlerts = False
    Application.DisplayStatusBar = False
    Application.ScreenUpdating = False
    '
    Sheets("Past User Accounts").Activate
    '
    Dim LastCol                     As Integer
    Dim LastRow                     As Long
    Dim FileName                    As Variant
    Dim fNameAndPath                As Variant
    Dim wbk1                        As Workbook
    Dim wks                         As Worksheet
    Dim Thisws                      As Worksheet
        
    Application.Volatile True
    '
    Static Import_FileName
    Import_FileName = fNameAndPath
    '
    Set Thisws = ActiveSheet
    '
    ChDrive "S:"
    ChDir "\AD_Listing\Raw_AD_Users\"
    '
    '----------------------------------------------------------
    MsgBox ("Select the most recent AD User text file to import or [Ctrl] + [Fn] + B (B for Break).")
    
    Do
      fNameAndPath = Application.GetOpenFilename(fileFilter:="Text Files (*.txt),*.txt", _
                     Title:="Select AD User text File To Be Opened")
    
    If fNameAndPath = "False" Then
        Exit Sub


    End If
    
    If InStr(fNameAndPath, "AD_User") = 0 Then
        MsgBox "You can only import an Active Directory report file.  Please select the most recent file with 'AD...' in the file name."
    End If
    Loop Until InStr(fNameAndPath, "AD_User") <> 0
    '
    '-----------------------------------------------------------
    '
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fNameAndPath, Destination:=Range("$A$1"))
        .Name = FileName
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileOtherDelimiter = "^"
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery = False
    End With
    '
    With Thiswksht
        LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
        LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    End With
    '
    Cells(1, LastCol).Offset(0, 1).Value = Import_FileName
    


BeforeExit:
'Application.ScreenUpdating = True
'Exit Sub
    Range("A1").Select
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi,

I think you have placed the code in the wrong position and the variable is empty.
Static? I would Dim it as a String

Try placing Import_FileName = fNameAndPath after ApplicationGetOpenFileName

Use ImportFileName= Dir(fNameAndPath) if you just want the name of the file
 
Upvote 0
Solution
Thanks! Pointed me in the right direction.
Code:
Cells(1, LastCol).Offset(0, 1).Value = fNameAndPath

I've been going around in circles trying to figure out how to capture the imported file's create date to put it in the worksheet name. As I create the query that creates the text file, putting the date and time in the file name is easy, getting the last saved date time from the metadata from an Excel file not so much. It's a workaround, but it will work.

Thanks again,

Ron

Ron
 
Upvote 0
One of the most sincint explanations I've read! And with straight to the point examples. Thanks much!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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