Copy VBA Code From One Workbook To Another ?

TkdKidSnake

Active Member
Joined
Nov 27, 2012
Messages
255
Office Version
  1. 365
Platform
  1. Windows
Hi all,

does anyone know how to copy VBA code from one workbook to another using VBA code and then for that code to run in the destination workbook?

The reason I need to do this is if the code is ran from the source workbook what its doing to the dates is quite bizarre however when its ran in the destination workbook all seems ok and when I compare the detail between the dates its realistic.

Source workbook = OTIF Dashboard.xlsm
Destination workbook = RawData.csv

The vba code is in module2 and is called "Sub RefreshData"

Below is the code
Code:
Sub RefreshDates()'
' SelectRawData Macro
'
    '
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    Workbooks.Open Filename:= _
        "P:\Public User Area\Quality\3 SUPPLIERS\Supplier OTIF\002 - RawData\RawData.csv"
    '
    '
    Columns("P:S").Select
    Application.CutCopyMode = False
    Selection.NumberFormat = "m/d/yyyy"
    '
    Columns("P:S").Select
    Selection.NumberFormat = "yyyy-mm-dd"


    Application.DisplayAlerts = False
    Application.ScreenUpdating = False


End Sub


If anyone can help with this it would be greatly appreciated

Thanks in advance
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
One way is to use dynamic Workbook object variables, which would allow you to easily bounce back and forth between workbooks, i.e.

Code:
Dim srcWB as Workbook
Dim destWB as Workbook

'Capture current workbook with macro as source workbook
Set srcWB = ActiveWorkbook

'Open destination workbook
Workbooks.Open Filename:= _
    "P:\Public User Area\Quality\3 SUPPLIERS\Supplier OTIF\002 - RawData\RawData.csv"

'Save workbook just opened as destination workbook
Set destWb = ActiveWorkbook

Then you can easily bounce back and forth between workbooks in your code like this:
Code:
srcWB.Activate

And can also save/close them by referencing these workbook objects.

Note that it looks like your destination file is a "CSV" file and not an Excel file. If you open a "CSV" file directly in Excel, Excel will often do some conversions on some of the number/date data (or what looks like numbers or dates), and may not also do what you want/expect.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
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