Simple Copy of data to csv file

cotech_10

Board Regular
Joined
Sep 11, 2010
Messages
135
Office Version
  1. 2016
Platform
  1. Windows
Hi All,
I think I have a simple request and that is I would like to select any range of data contained in a worksheet and copy that data directly into pre-defined csv file( or one I have already created).

I look forward for someones assistance.

Cheers
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I am assuming that you want a Macro to do this. I believe you can get most of the code you need to do this using the Macro Recorder.
Turn on your Macro Recorder and do the following:
1. Open the file that has the new data in Excel
2. Open the CSV file that you want to copy to in Excel (being careful that you convert each field correctly so it doesn't change the format on you)
3. Copy the data from step 1 to the file in step 2
4. Re-save the CSV file
5. Close out of both files

If you already have data in your CSV file, and you want to copy the new data below that, you can find that ending spot and determine where to paste the data to like this (assuming we can look at column A to find the end of the data):
VBA Code:
Cells(Rows.Count,"A").End(xlUp).Offset(1,0)

And if all the data you want to copy over is contiguous, you can easily select it all like this:
VBA Code:
Range("A1").CurrentRegion.Copy

You may need to clean up the code a little, because the Macro Recorder is very literal. If you need any help with that part post the code you came up, along with the changes I mentioned above, here and we can help you with that.
 
Upvote 0
Maybe modify this code

VBA Code:
Option Explicit

Sub CopyDataToMaster()
'Copy Columns A,B,C and D to Master Spreadsheet
    Dim lr As Long
    Dim lrC As Long
    Dim wbTarget As Workbook    'Master
    Dim wbThis As Workbook  'Current Open Workbook
    Dim strName As String    'Name for source sheet/target workbook
    Dim thePath As String  'Path for Master Spreadsheet

    Application.ScreenUpdating = False

    'set the current active workbook
    Set wbThis = ActiveWorkbook
    'set the target workbook name
    strName = "YourWorkbookName"  'change as required
    'set the path to the Comments Spreadsheet

    thePath = "C:YourPathToNewWorkbook"  'Change as required
    'open Master Spreadsheet
    Set wbTarget = Workbooks.Open(thePath & strName & ".xlsm")
    'Activate the Target Workbook
    wbTarget.Activate
    'Find the last row in the target workbook
    lrC = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    'activate source workbook
    wbThis.Activate
    'find the last row in column A to determine the range to copy
    lr = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    'clear any thing on the clipboard to mazimize available memory
    Application.CutCopyMode = False
    'Copy Data in Columns A,B,C,D
    wbThis.Sheets("Sheet1").Range("A2:E" & lr).Copy  'Change "E" to actual Right Column
    'paste the data to the Comments Worksheet
    wbTarget.Sheets("Sheet1").Range("A" & lrC + 1).PasteSpecial
    'Clear the clipboard
    Application.CutCopyMode = False
    wbTarget.Save
    wbTarget.Close
    wbThis.Activate
    Application.ScreenUpdating = True

    'clear memory
    Set wbTarget = Nothing
    Set wbThis = Nothing
    MsgBox "Data Transferred"
End Sub
 
Upvote 0
Hi Alansidman,

Thank you once again I will try this code today..

Regards
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,449
Members
448,966
Latest member
DannyC96

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