+ Reply to Thread
Results 1 to 7 of 7

Is there a way to import data from CSV file to xlsx table using VB?

  1. #1
    Registered User
    Join Date
    10-29-2023
    Location
    Arkansas, USA
    MS-Off Ver
    Office 365
    Posts
    2

    Is there a way to import data from CSV file to xlsx table using VB?

    I currently have a csv file and xslm file in the same folder, and right now, I have to open the CSV, copy column A to a CSV tab on my xlsm file, then I have a VB script that is activated by a button that does text to columns and removes duplicates. I then have a formula pulling that information in to another worksheet to populate the AP review table. I would like to remove the step of opening the CSV file altogether, if possible. I would like to basically combine the code from my csv button with something that can import the data. I am extremely new to programming with VB, so any help is appreciated.

    Is there a way to use a VB script to take the data from column A of the CSV file, strip away everything except the 3 digit number, remove the duplicates, and input the data in column A of my table in my xlsx file?

    There will only be one csv file in the folder, so *.csv could be used to call that file.

    Here is a sample of the data in column A in the CSV file:

    001 CoordX -350
    001 CoordY 31
    001 APModel
    002 CoordX -365
    002 CoordY 102
    002 APModel
    003 CoordX -315
    003 CoordY 280
    003 APModel
    004 CoordX -227
    004 CoordY 278
    004 APModel
    005 CoordX -139
    005 CoordY 283
    005 APModel
    006 CoordX -75
    006 CoordY 258
    006 APModel
    007 CoordX -14
    007 CoordY 286
    007 APModel

    I would like to use a button in the xlsx file to grab the data from column A in the CSV file, convert the text to columns, strip away everything except the 3 digit number, remove the duplicates, and input the data in column A of my table in my xlsx file?

    I don't want to change anything in any other columns.

    The input destination is A202 on the xlsx file.

    When I push the button, I'd like to activate the vb script to do the above and have column A of my table look like this:

    AP001
    AP002
    AP003
    AP004
    AP005
    AP006
    AP007


    Please let me know if I can provide any more details.

    I have attached a sample CSV and xlsm files.

    Thank you for your help!
    Attached Files Attached Files
    Last edited by confused_zebra; 10-29-2023 at 06:18 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: Is there a way to import data from CSV file to xlsx table using VB?

    Try this as an example:

    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    10-29-2023
    Location
    Arkansas, USA
    MS-Off Ver
    Office 365
    Posts
    2

    Re: Is there a way to import data from CSV file to xlsx table using VB?

    Thank you for your reply!

    Is there a way to either have the csv directory be limited to the folder that the xlsm and csv file are in or to have the user choose the csv file to import?

    Our file structure is as follows:

    C:\Users\%username%\OneDrive\Survey Review Reports\%Filename%\*.csv

    %Filename% = (5 digit site ID.2 character country code)
    01234.US, 00100.US, etc...

    The CSV file will always be in the same directory as the xlsm file that the macro is being ran on.
    The CSV file will always be named 01234.US, 05520.US, 01032.CA, etc... (5 digit site ID.2 character country code) The folder that contains these files is named the same (5 digit site ID.2 character country code).

    Please Login or Register  to view this content.
    I tried editing the macro to include that wildcard directory, but I'm afraid I don't know enough about it yet and I'm getting an error on "Open PathAndFileName For Binary As #FileNum"
    Please Login or Register  to view this content.
    Last edited by confused_zebra; 10-29-2023 at 09:47 PM.

  4. #4
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Is there a way to import data from CSV file to xlsx table using VB?

    Hi all. Perhaps replace Const PathAndFileName As ... with:
    Please Login or Register  to view this content.
    This assumes the intended source is the only .csv file in that directory, as stated in post#1.
    Last edited by leelnich; 10-30-2023 at 02:15 AM.
    Clicking the Add Reputation star below helpful posts is a great way to show your appreciation.
    Please mark your threads as SOLVED upon conclusion (Thread Tools above Post # 1). - Lee

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Is there a way to import data from CSV file to xlsx table using VB?

    Here's another method.
    Please Login or Register  to view this content.

  6. #6
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Is there a way to import data from CSV file to xlsx table using VB?

    Or this... Similar to post#2, but uses the post#4 pathname hack, a dictionary for duplicate removal, and table references for output:
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-27-2023
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    11

    Re: Is there a way to import data from CSV file to xlsx table using VB?

    What Div you are using?
    Thanks
    James

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Tutorial Needed: User upload xlsx file, read that file, write to table
    By PrimePorkchop in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-30-2021, 10:50 PM
  2. [SOLVED] Import data from .XLS, xlsx file into .xlsm file and do vlookup
    By pacificepoc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-20-2018, 01:32 AM
  3. Faster Import of XLSX File needed (plus "delete specific lines")
    By ydrive in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-01-2017, 05:49 PM
  4. Help with code to convert xls to xlsx and continue with xlsx file
    By 3345james in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-02-2016, 11:47 AM
  5. Data missing from .xlsx import into Excel, but column titles there
    By Zonc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-25-2014, 05:01 AM
  6. Replies: 3
    Last Post: 01-05-2014, 05:50 AM
  7. How to add the columns data of several xlsx files of a folder in another xlsx file
    By ravikumar00008 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-25-2012, 04:29 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1