Hello everyone.
I'm trying to create a macro that doesn't work the way I want it to.
I have 2 Excel files, one old and one new. I've created a macro button in the new file that should only import some data from the old one, cell A1 and cells B2: D5. I failed to convince him to work properly. The code copies all the data.
The second problem is that both files are password protected against modification of some cells (yellow cells). The password is "secret". If I leave the password, the macro code does not copy anything. I tried to enter the password in the OpenFile command, but I was definitely doing something wrong.
I just need some hint for this code, as I am pretty new in the VBA world. Thanks in advance.
Below is the code I use.
Sub ImportData()
Dim fileToOpen As Variant
Dim fileFilterPattern As String
Dim NewWorksheet As Worksheet
Dim OldWorkbook As Workbook
Application.ScreenUpdating = False
fileFilterPattern = "Excel Files (*.xls; *.xlsx),*.xls;*.xlsx"
fileToOpen = Application.GetOpenFilename(fileFilterPattern)
If fileToOpen = False Then
MsgBox "No file selected."
Else
Workbooks.Open (fileToOpen)
Set OldWorkbook = ActiveWorkbook
Set NewWorksheet = ThisWorkbook.Worksheets("Data")
OldWorkbook.Worksheets(2).Range("A1:A1").CurrentRegion.Copy NewWorksheet.Range("A1:A1")
OldWorkbook.Close False
End If
Application.ScreenUpdating = True
End Sub