Convert this code to have Pop Up folder selection

HotLanta

Board Regular
Joined
Nov 3, 2005
Messages
168
Hi all,

I have merged two pieces of code together and it is working perfectly. To make it more perfect I wanted to modify it once more but I am having trouble making that work.
I have some code that looks for text files in a hard coded folder location. Then a pop up box asks the user what text they want to change in the files and then another asks what they want to change it to.

I would like for instead of being hard coded to a particular folder location, a pop up ask the user to select the file location.

In the code below I simply typed "Process" in the hard coded folder location so not as to put any real information out there....

Sub Replace_text()

Dim objFSO, StrFolder, objFolder, objFile
Dim x As String, y As String
Dim strOldValue, strNewValue, objRead, strContents, objWrite

Const ForReading = 1
Const ForWriting = 2

StrFolder = "Process"

x = InputBox("Replace What?", "Replace What")
y = InputBox("Replace With?", "Replace With")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(StrFolder)
For Each objFile In objFolder.Files
Set objRead = objFSo_Opentextfile(objFile.Path, ForReading)

On Error Resume Next

strContents = objRead.readall
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "Cannot read: " & objFile.Path
strContents = ""
End If
On Error GoTo 0
objRead.Close
If (InStr(strContents, x) > 0) Then
strContents = Replace(strContents, x, y)
Set objWrite = objFSo_Opentextfile(objFile.Path, ForWriting)
objWrite.Write strContents
objWrite.Close
End If
Next

End Sub

Thanks
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,351
Replace the StrFolder = line with:
Code:
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select the folder"
        If .Show Then
            StrFolder = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,629
Messages
5,838,454
Members
430,549
Latest member
jayjay2022

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
Top