L
Louise
I am trying to copy a range from several closed and password protected workbooks. I have 2 problems:
1) I have not been able to unprotect the workbooks automatically
Using the ActiveWorkbook.Unprotect (MyPassword) does not work
2) I want to be able to tie the file names to a cell in the basebook
Where the code reads: If Left(FNames, 4) = "YEAR" Then
I need to replace the "YEAR" for Cell C3 in the basebook.
Following is the code that I am using.
Thanks in advance for the help!
Louise
------------------------------
Sub CopyRangesOfClosedWorkbooks()
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim rnum As Long
Dim SourceRcount As Long
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String
Dim subfolder As String
SaveDriveDir = CurDir
MyPath = Range("A1")
ChDrive MyPath
ChDir MyPath
FNames = Dir("*.xls")
If Len(FNames) = 0 Then
MsgBox "No files in the Directory"
ChDrive SaveDriveDir
ChDir SaveDriveDir
Exit Sub
End If
Application.ScreenUpdating = False
Set basebook = ThisWorkbook
basebook.Worksheets(2).Cells.Clear
'clear all cells on the second sheet
rnum = 1
Do While FNames <> ""
If Left(FNames, 4) = "YEAR" Then
Set mybook = Workbooks.Open(FNames)
Set sourceRange = mybook.Worksheets("1").Range("c30:c32")
SourceRcount = sourceRange.Rows.Count
Set destrange = basebook.Worksheets(2).Cells(rnum, 1)
basebook.Worksheets(2).Cells(rnum, "D").Value = mybook.Name
'This will add the workbook name in column D if you want
With sourceRange
Set destrange = basebook.Worksheets(2).Cells(rnum, "A").Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount
mybook.Close False
End If
FNames = Dir()
Loop
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub
1) I have not been able to unprotect the workbooks automatically
Using the ActiveWorkbook.Unprotect (MyPassword) does not work
2) I want to be able to tie the file names to a cell in the basebook
Where the code reads: If Left(FNames, 4) = "YEAR" Then
I need to replace the "YEAR" for Cell C3 in the basebook.
Following is the code that I am using.
Thanks in advance for the help!
Louise
------------------------------
Sub CopyRangesOfClosedWorkbooks()
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim rnum As Long
Dim SourceRcount As Long
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String
Dim subfolder As String
SaveDriveDir = CurDir
MyPath = Range("A1")
ChDrive MyPath
ChDir MyPath
FNames = Dir("*.xls")
If Len(FNames) = 0 Then
MsgBox "No files in the Directory"
ChDrive SaveDriveDir
ChDir SaveDriveDir
Exit Sub
End If
Application.ScreenUpdating = False
Set basebook = ThisWorkbook
basebook.Worksheets(2).Cells.Clear
'clear all cells on the second sheet
rnum = 1
Do While FNames <> ""
If Left(FNames, 4) = "YEAR" Then
Set mybook = Workbooks.Open(FNames)
Set sourceRange = mybook.Worksheets("1").Range("c30:c32")
SourceRcount = sourceRange.Rows.Count
Set destrange = basebook.Worksheets(2).Cells(rnum, 1)
basebook.Worksheets(2).Cells(rnum, "D").Value = mybook.Name
'This will add the workbook name in column D if you want
With sourceRange
Set destrange = basebook.Worksheets(2).Cells(rnum, "A").Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount
mybook.Close False
End If
FNames = Dir()
Loop
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub