Code Edit to add Browse File Option

levanoj

Active Member
Joined
Oct 25, 2007
Messages
311
The code below opens all files individually within a specific folder, Copies all then Pastes back the Values only. What I'm looking to change in this code is to instead prompt me to select a folder from my C: Drive and not limit me to just one specific folder then continue with the rest of the process. Can anyone Help?

Code:
Sub PasteSpclOnly()
Application.DisplayAlerts = False
Dim myDir As String, fn As String, ws As Worksheet
myDir = "C:\Documents and Settings\levanoj\Desktop\LAB\"
fn = Dir(myDir & "\*.xls")
Do While fn <> ""
    With Workbooks.Open(myDir & "\" & fn)
        For Each ws In .Worksheets
            ws.UsedRange.Copy
            ws.UsedRange.PasteSpecial xlPasteValues
        Next
        .Close True
    End With
    fn = Dir
Loop
Application.DisplayAlerts = True
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Thanks John W but I'm pretty much an amateur when it comes to rewriting code. I basically pasted the code from the link you gave me within my macro and got an error. Can you get more specific as to how exactly I'm suppose to incorporate this macro with the link you provided?
 
Upvote 0
It tells you how to use and test it. In short, put the code in a standard module (Insert - Module in the VB Editor).

To use it with your code instead of your hardcoded folder, something like:

Dim fn As String, ws As Worksheet
Dim myDir As Variant
myDir = BrowseForFolder("C:\")

You might need to remove the \ from fn = Dir(myDir & "\*.xls") and the .Open depending on whether or not the folder returned by BrowseForFolder ends with \ - I haven't tested it.
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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