Macro copy data to not open workbook

ste33uka

Active Member
Joined
Jan 31, 2020
Messages
471
Office Version
  1. 365
Platform
  1. Windows
I use the following macro to copy data from an open workbook to a workbook called "stored", sheet "data" if D1 value is "yes"
Is it possible to copy data without workbook "stored" being open ?


VBA Code:
Sub copy_to_dataworkbook()
   Dim i As Long
   For i = 1 To 60
      With Sheets(CStr(i))
         If LCase(.Range("D1").Value) = "yes" Then
          .Range("Vs86:WH105").Copy
            Workbooks("stored").Sheets("data").Range("cu" & Rows.Count).End(xlUp).Offset(2).PasteSpecial xlPasteValues
         End If
      End With
   Next i
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
No but U can open the wb, put stuff in it and close it in the background without even knowing that it was open... sort of the same. HTH. Dave
 
Upvote 0
Untested code. Please make a back up of your file before you test the code. Dave
Code:
Sub copy_to_dataworkbook()
Dim i As Long, FSO As Object, FilDir As Object
On Error GoTo QuitThis
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Set FSO = CreateObject("scripting.filesystemobject")
'***change File path to your file
Set FilDir = FSO.getfile("C:\Users\home\Desktop\stored.xlsm")
Workbooks.Open Filename:=FilDir
   For i = 1 To 60
      With ThisWorkbook.Sheets(CStr(i))
         If LCase(.Range("D1").Value) = "yes" Then
          .Range("Vs86:WH105").Copy
            Workbooks(FilDir.Name).Sheets("data").Range("cu" & Rows.Count) _
                    .End(xlUp).Offset(2).PasteSpecial xlPasteValues
         Application.CutCopyMode = False
         End If
      End With
   Next i
Workbooks(FilDir.Name).Close SaveChanges:=True
QuitThis:
Set FSO = Nothing
Set FilDir = Nothing
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
End Sub
edit: changed save to true
 
Last edited:
Upvote 0
Solution
Hi
What about
VBA Code:
Sub copy_to_dataworkbook()
   Dim i As Long
   Dim wb
   Set wb = Workbooks.Open("C:\Users\home\Desktop\stored.xlsx")
   For i = 1 To 60
      With Sheets(CStr(i))
         If LCase(.Range("D1").Value) = "yes" Then
          .Range("Vs86:WH105").Copy
            wb.Sheets("data").Range("cu" & Rows.Count).End(xlUp).Offset(2).PasteSpecial xlPasteValues
         End If
      End With
   Next i
   wb.Close False
End Sub
 
Upvote 0
Missed the edit timer. Added some error msg along with the save change.
Code:
Sub copy_to_dataworkbook()
Dim i As Long, FSO As Object, FilDir As Object
On Error GoTo QuitThis
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Set FSO = CreateObject("scripting.filesystemobject")
'***change File path to your file
Set FilDir = FSO.getfile("C:\Users\home\Desktop\stored.xlsm")
Workbooks.Open Filename:=FilDir
   For i = 1 To 60
      With ThisWorkbook.Sheets(CStr(i))
         If LCase(.Range("D1").Value) = "yes" Then
          .Range("Vs86:WH105").Copy
            Workbooks(FilDir.Name).Sheets("data").Range("cu" & Rows.Count) _
                    .End(xlUp).Offset(2).PasteSpecial xlPasteValues
         Application.CutCopyMode = True
         End If
      End With
   Next i
Workbooks(FilDir.Name).Close SaveChanges:=True
QuitThis:
If Err.Number <> 0 Then
On Error GoTo 0
MsgBox "Error"
End If
Set FSO = Nothing
Set FilDir = Nothing
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
End Sub
Dave
 
Upvote 0
Untested code. Please make a back up of your file before you test the code. Dave
Code:
Sub copy_to_dataworkbook()
Dim i As Long, FSO As Object, FilDir As Object
On Error GoTo QuitThis
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Set FSO = CreateObject("scripting.filesystemobject")
'***change File path to your file
Set FilDir = FSO.getfile("C:\Users\home\Desktop\stored.xlsm")
Workbooks.Open Filename:=FilDir
   For i = 1 To 60
      With ThisWorkbook.Sheets(CStr(i))
         If LCase(.Range("D1").Value) = "yes" Then
          .Range("Vs86:WH105").Copy
            Workbooks(FilDir.Name).Sheets("data").Range("cu" & Rows.Count) _
                    .End(xlUp).Offset(2).PasteSpecial xlPasteValues
         Application.CutCopyMode = False
         End If
      End With
   Next i
Workbooks(FilDir.Name).Close SaveChanges:=True
QuitThis:
Set FSO = Nothing
Set FilDir = Nothing
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
End Sub
edit: changed save to true
Thanks alot for your help on that, that worked , job done
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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