VBA Code in Separate File

dwoom65

New Member
Joined
Feb 5, 2014
Messages
4
I am running this code which opens files on a network drive and pastes them into an Excel file on the tabs specified in the code. Is there a way to tweak the code so that the macro could be in a separate file with the code referencing the destination spreadsheet file and file tabs? I would be fine with having the destination file open when running the code. Thanks.

Sub BILLS()

Dim path As String
Dim xxxEnd As String

Dim xxxStart As String

path = "\folder path\" (location of files pasted into destination file)

xxxEnd = "E500"
xxxStart = "A1"


Call exportRoutine(path, "month_bill_xxx.csv", xxxEnd, "destination file tab name", xxxStart)


End Sub

Sub exportRoutine(path As String, file As String, copyEnd As String, Reports As String, pasteCell As String)

Application.DisplayAlerts = False

Application.Workbooks.Open path & file
Windows(file).Activate
Range("A1", copyEnd).Select
Selection.Copy

ThisWorkbook.Activate
Sheets(Reports).Select
Range(pasteCell).Select
Selection.PasteSpecial Paste:=xlValues
Windows(file).Activate
ActiveWorkbook.Close SaveChanges:=False
ThisWorkbook.Activate

End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi

Maybe you could try something like

VBA Code:
Sub exportRoutine(path As String, file As String, copyEnd As String, Reports As String, pasteCell As String)
Dim srcWB As Workbook
Dim srcSH As Worksheet
Dim actSH As Worksheet

Application.DisplayAlerts = False

Set actSH = ThisWorkbook.Sheets(Report)

Set srcWB = Application.Workbooks.Open(path & file)
Set srcSH = srcWB.ActiveSheet

actSH.Range(pasteCell, copyEnd).Value = srcSH.Range("A1", copyEnd).Value

srcWB.Close SaveChanges:=False
ThisWorkbook.Activate

End Sub
 
Upvote 0
Thanks. I am getting an error on this line--not sure what I should be referencing in the parentheses where the word report is.

Set actSH = ThisWorkbook.Sheets(Report)
 
Upvote 0
Looks like a typo, your original code uses Reports, I put Report. Apologies.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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