Code to choose directory works in Xp, not in 7

3chords

New Member
Joined
Feb 2, 2010
Messages
3
Hi,

My code is designed to allow the user to browse to and designate a folder, open up all the excel files in that folder and then consolidate all the data within the excel files. I am using XP and the code works perfectly. On 7 it doesn't work. The folder shows up as empty. I had the user unhide the file extensions but that didn't work. Thanks in advance.

-3chords



Sub Combine()

'This opens up the workbooks, copies the data, and consolidates it into 1 sheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("Consolidate").Cells.Clear
LastCol = "O"

folder = GetFolder()
folder = folder & "\"
FName = Dir(folder & "*.xl*")

With ThisWorkbook.Sheets("Consolidate")
.Cells.ClearContents
Do While FName <> ""
Set bk = Workbooks.Open(Filename:=folder & FName)
For Each sht In bk.Sheets

ThisbookLastRow = .Range("A" & Rows.Count).End(xlUp).Row
If ThisbookLastRow = 1 Then
NewRow = 1
'copy header row
sht.Range("A1:" & LastCol & "1").Copy _
Destination:=.Range("B1")
'put filename in cell A1
sht.Range("A1") = "Workbook"
End If
NewRow = ThisbookLastRow + 1


With sht
lastrow = .Range("A" & Rows.Count).End(xlUp).Row
DataRows = lastrow - 1
Set CopyRange = .Range("A2:" & LastCol & lastrow)
End With

'copy data from old workbook to this workbook
If DataRows > 0 Then
CopyRange.Copy _
Destination:=.Range("B" & NewRow)
'put book name into column A
.Range("A" & NewRow & ":A" & (NewRow + DataRows - 1)) = _
FName
End If
Next sht
bk.Close savechanges:=False
FName = Dir()
Loop
End With

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub


Function BrowseForFolder(Optional OpenAt As Variant) As Variant

Dim ShellApp As Object
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

On Error Resume Next
BrowseForFolder = ShellApp.self.Path
frmGUI.txtFoundFolder.Text = BrowseForFolder
On Error GoTo 0

Set ShellApp = Nothing

Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select

Exit Function

Invalid:
BrowseForFolder = False

End Function
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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