Copying data to an open file sheet from another

rneilson52

New Member
Joined
Nov 22, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I am having an issue with an open file, selecting a second file and then copying data from a specific sheet in that second file, then closing that workbook and copying the data to the next blank row on a sheet in the first file. The code I have is

Sub Import_Data()
'
' Import_Data Macro
' Import from Manifest file
'

'
ThisWorkbook.Worksheets("Database").Select
ActiveSheet.Unprotect

Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim FolderPath As String

FolderPath = "C:\"
VBA.ChDir FolderPath


FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xlsm*),*xlsm*")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
Sheets("Reporting Data").Select
ActiveSheet.Unprotect
Range("A2:P250").Copy
ActiveSheet.Protect
OpenBook.Close False
End If
Application.ScreenUpdating = True
ThisWorkbook.Worksheets("Database").Select
ActiveSheet.Unprotect
NextRow = Range("A100000").End(xlUp).Row + 1
Range("A" & NextRow).Select.PasteSpecial xlPasteValues This is where I fall over with a runtime error 424

ActiveSheet.Protect

End Sub

Any help greatly appreciated
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I am having an issue with an open file, selecting a second file and then copying data from a specific sheet in that second file, then closing that workbook and copying the data to the next blank row on a sheet in the first file. The code I have is

Sub Import_Data()
'
' Import_Data Macro
' Import from Manifest file
'

'
ThisWorkbook.Worksheets("Database").Select
ActiveSheet.Unprotect

Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim FolderPath As String

FolderPath = "C:\"
VBA.ChDir FolderPath


FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xlsm*),*xlsm*")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
Sheets("Reporting Data").Select
ActiveSheet.Unprotect
Range("A2:P250").Copy
ActiveSheet.Protect
OpenBook.Close False
End If
Application.ScreenUpdating = True
ThisWorkbook.Worksheets("Database").Select
ActiveSheet.Unprotect
NextRow = Range("A100000").End(xlUp).Row + 1
Range("A" & NextRow).Select.PasteSpecial xlPasteValues This is where I fall over with a runtime error 424

ActiveSheet.Protect

End Sub

Any help greatly appreciated
try this:
VBA Code:
Sub Import_Data()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim FileToOpen As Variant
    Dim OpenBook As Workbook, OpenWs As Worksheet, thisWs As Worksheet
    Set thisWs = ThisWorkbook.Worksheets("Database")
    FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xlsm*),*xlsm*")
    If FileToOpen <> False Then
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        Set OpenWs = OpenBook.Sheets("Reporting Data")
        OpenWs.Unprotect
        thisWs.Unprotect
        OpenWs.Range("A2:P250").Copy
        NextRow = thisWs.Range("A100000").End(xlUp).Row + 1
        thisWs.Range("A" & NextRow).PasteSpecial xlPasteValues
        OpenWs.Protect
        thisWs.Protect
        OpenBook.Close False
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,091
Members
453,021
Latest member
Justyna P

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