How to use the cell selected by an inputbox and use it to loop through all files/workbooks

patrickpperron

New Member
Joined
Aug 8, 2014
Messages
4
Hello,

I am attempting to figure out how to prompt the user to type in the cell label he/she wants from an inputbox and then automatically use that same cell address in all the files I want to loop through.

I want to prompt the user only once, and not every time the code loops through each file. I would want this code to loop through any number of files. These files are: Prio001.xlsx, Prio002.xlsx, Prio003.xlsx, etc....

Hence "Prio*.xlsx"

I am writing this code in a masterfile named "master.xlsm".

I was wondering if I could use a For Each loop; any help would be greatly appreciated. I hope this is enough to give you a feel of what I'm trying to achieve here.


sub looptest()

Dim FNum As Long
Dim FilesInPath As String
Dim MyPath As String


MyPath = ActiveWorkbook.Path


If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If


FilesInPath = Dir(MyPath & "Prio*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If


Set Hardness = Application.InputBox(prompt:="Select a cell...", Title:="Select a Cell from All Prio Files for Hardness", Default:=Selection.Address(2, 1), Type:=8)

For Each FNum In FilesInPath

FNum.Activate
FNum.Range(Target.Address).Select


Next FNum
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hiya
UNTESTED, but I think it's the sort of thing you're after
Code:
Sub looptest()

    Dim FNum As Long
    Dim FilesInPath As String
    Dim MyPath As String
    Dim FilesInPath
    Dim Hardness As Range


    MyPath = ActiveWorkbook.Path

    If Right(MyPath, 1) <> "\" Then
        MyPath = MyPath & "\"
    End If

    FilesInPath = Dir(MyPath & "Prio*.xl*")
    If FilesInPath = "" Then
        MsgBox "No files found"
        Exit Sub
    End If

    Set Hardness = Application.InputBox(prompt:="Select a cell...", Title:="Select a Cell from All Prio Files for Hardness", Default:=Selection.Address(2, 1), Type:=8)

    Do While FilesInPath <> ""
        Workbooks.Open MyPath & MyFile
        Hardness.Select
        .
        .
        .
        Workbooks(FilesInPath).Close Savechanges:=False
        FilesInPath = Dir
    Loop
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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