Convert this code to have Pop Up folder selection

HotLanta

Board Regular
Joined
Nov 3, 2005
Messages
176
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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

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