Results 1 to 8 of 8

.csv Import. Resize table to match data set

Threaded View

  1. #1
    Registered User
    Join Date
    11-12-2018
    Location
    Cornwall, England
    MS-Off Ver
    Excel 2016 (Office 365 ProPlus)
    Posts
    58

    Lightbulb .csv Import. Resize table to match data set

    I have written a macro module to aid users import .csv files into some empty tables ready for further analysis. These .csv files always contain three columns of data, but the amount of rows varies greatly and can be in the thousands. Previously we used to copy and paste values, but this is notoriously slow. Instead we have switched to using ''Range.Value ='' (shown in green below).

    This has sped things up massively, but I am struggling to size the receiving table to match the amount of rows from the incoming data set. As a work around I have just manually set, using a resize function, to a value that I know to be larger than the amount of rows in the data set (shown in blue below and set in this example to 100 rows). This will then just show any empty rows as #N/A. Not pretty!

    I could write some code to remove the #N/A rows, but I am sure there must be a neater way to do this from the off. Can anyone let me know how to code this so each table will match the size of their imported data set? I have attached a striped down version of the macro file along with a test .csv file to allow you to try this out. Thanks in advance for any help.

    Sub Data_IMPORT_CSV()
        Dim ToSheet As Worksheet
        Dim FromBook As String
        Dim FromSheet As Worksheet
        Dim RngT1 As Range, RngT2 As Range
        Dim RngF1 As Range, RngF2 As Range
        Dim TableRng As Range
        
        '-------------------------------------------------------------------------
    On Error Resume Next
            
            Set ToSheet = ActiveSheet
            FromBook = Application.GetOpenFilename("CSV (*.csv), *.csv, All Files (*.*), *.*")
            'They have cancelled.
            If FromBook = "False" Then Exit Sub
            
            Workbooks.Open FromBook, Local:=True
            Set FromSheet = ActiveWorkbook.Sheets(1)
            
            'COPY & PASTE DATA **Coulmn A**
            ActiveSheet.Range("A1").Select
            ActiveSheet.Range(ActiveCell.Offset(0, 0), ActiveCell.End(xlDown)).Select
            Set RngF1 = Selection
            
            ToSheet.Activate
            Selection.ListObject.HeaderRowRange.Find("Type").Offset(1, 0).Resize(100, 1).Select 'Find the named range & pastes
            Set RngT1 = Selection
            
            RngT1.Value = RngF1.Value
                    
            'COPY & PASTE DATA 2 **Column C**
            FromSheet.Activate
            ActiveSheet.Range("C1").Select
            ActiveSheet.Range(ActiveCell.Offset(0, 0), ActiveCell.End(xlDown)).Select
            Set RngF2 = Selection
            
            ToSheet.Activate
            Selection.ListObject.HeaderRowRange.Find("Value").Offset(1, 0).Resize(100, 1).Select 'Find the named range & pastes
            Set RngT2 = Selection
            
            RngT2.Value = RngF2.Value
                   
            'CLOSE FROMBOOK
            Workbooks(2).Close SaveChanges:=False
                   
    If Err Then MsgBox "No table selected!": Err.Clear 'display this message if the above fails
    
    MsgBox ("DONE")
            
    End Sub
    Attached Files Attached Files
    Last edited by mattydboom1; 09-11-2020 at 06:35 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Auto Resize Table upon data entry from separate sheet
    By sorensjp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-01-2020, 06:50 AM
  2. [SOLVED] Import Excel Data to Find Match in Access Table
    By CRW in forum Access Tables & Databases
    Replies: 3
    Last Post: 09-15-2012, 08:11 AM
  3. Macro to import and resize images to cell
    By wilsonyeoh in forum Word Programming / VBA / Macros
    Replies: 1
    Last Post: 05-18-2011, 01:58 AM
  4. Resize range after import
    By gnoke in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-30-2010, 07:49 AM
  5. Excel Macro Import web image, crop, and resize.
    By jdax57 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-17-2009, 08:30 AM
  6. Import Pics and Resize at the same time
    By silver_wire in forum Excel General
    Replies: 0
    Last Post: 08-25-2006, 05:04 PM
  7. Import And Resize Images
    By 2eXtreme in forum Excel General
    Replies: 6
    Last Post: 02-05-2006, 12:00 PM

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