Get File Name

MarkCBB

Active Member
Joined
Apr 12, 2010
Messages
497
Hi there,

Every time I change the file name I have to update this part of the Code.

is there a way to make it dynamic and get the file name?

Code:
Windows("Template_File open.v5.1.xlsm").Activate
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Is your VBA code in that workbook?

Code:
ThisWorkbook.Activate

If not please post the rest of your code.
 
Upvote 0
Hi Andrew,

Yes its in that workbook, but it loops though other workbooks, not sure if that will make a differance? here is the rest of the code:

Code:
Option Base 1

Sub GetValues()

    Dim path As String
    Dim fileNames As Range
    Dim missingFile As String
    If Range("C1").Value = "" Then
    MsgBox ("Please fill in the file path")
    Exit Sub
    Else
 GetFileNames
    With wsControlPanel
        path = CheckPath(.Range("Path"))
        Set fileNames = .Range("FileNames")
        missingFile = checkMissingFiles(path, fileNames)
        If missingFile <> "" Then
            MsgBox missingFile & " is missing in " & path
            Exit Sub
        End If
        OpenWorkbook path, fileNames
    End With
    'Application.Calculation = xlCalculationAutomatic
    'ActiveWorkbook.Save
    End If
End Sub
Sub OpenWorkbook(path As String, fileNames As Range)

    
   With ActiveSheet

        For n = (Z + 1) To fileNames.Count
            If fileNames(n, 1) <> "" Then
                Workbooks.Open path & fileNames(n, 1), False, True
'My code
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Windows("Template_File open.v5.1.xlsm").Activate
    Sheets("INPUT").Select
    Range("A1").Select
    ActiveSheet.Paste
    ActiveWindow.ActivateNext
    Application.CutCopyMode = False

                ActiveWorkbook.Saved = True
                ActiveWorkbook.Close SaveChanges:=False
                Call IMPORT_DATA
                
            End If
        Next n
    End With
 
Upvote 0
I didn it! :) yay

Code:
Sub OpenWorkbook(path As String, fileNames As Range)
Dim WKB As Workbook
Set WKB = ThisWorkbook

   With ActiveSheet

        For n = (Z + 1) To fileNames.Count
            If fileNames(n, 1) <> "" Then
                Workbooks.Open path & fileNames(n, 1), False, True
'My code
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    
    WKB.Activate
 
Upvote 0

Forum statistics

Threads
1,217,415
Messages
6,136,506
Members
450,016
Latest member
murarj

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