+ Reply to Thread
Results 1 to 2 of 2

Creating an array while skipping random blank columns

Hybrid View

  1. #1
    Registered User
    Join Date
    05-18-2012
    Location
    London
    MS-Off Ver
    2013
    Posts
    14

    Creating an array while skipping random blank columns

    Hi,
    I work with spreadsheets exported from a third-party software. The problem is that the exported sheet has random number of blank columns in between the data columns (as shown in the attached spreadsheet).

    What I want to do is create a VBA array with the data in the sheet. For the attached data, the array will comprise of 12 rows and 4 columns (H1, H2, H3, H4).

    How can I create a simple loop that will fill up my array of (12,4) by skipping the blanks?

    Thanks!

    samplesheet.xlsx

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Creating an array while skipping random blank columns

    Hello pushkinpassey,

    The attached workbook has the macro below added to it. This will remove the unwanted columns. The macro converts the data on the same on the worksheet.

    Module1 Macro Code
    Sub Macro1()
    
        Dim arr     As Variant
        Dim c       As Long
        Dim Data    As Variant
        Dim LastCol As Long
        Dim LastRow As Long
        Dim n       As Long
        Dim r       As Long
        Dim Rng     As Range
        Dim Wks     As Worksheet
        
            Set Wks = Worksheets("Sheet1")
            Set Rng = Wks.Range("F1")
            
                LastCol = Wks.Cells(Rng.Row, Columns.Count).End(xlToLeft).Column
                LastRow = Wks.Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False, False, False).Row
                
                Set Rng = Wks.Range(Rng, Wks.Cells(LastRow, LastCol))
                
                Data = Rng.Value
                
                For c = 1 To Rng.Columns.Count
                    If Application.CountA(Rng.Columns(c)) Then
                        n = n + 1
                        For r = 1 To Rng.Rows.Count
                            Data(r, n) = Rng.Item(r, c)
                        Next r
                    End If
                Next c
            
            ReDim Preserve Data(1 To Rng.Rows.Count, 1 To n)
            
            Rng.ClearContents
            Rng.Resize(ColumnSize:=n).Value = Data
            
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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. Replies: 1
    Last Post: 05-21-2014, 04:30 PM
  2. Replies: 0
    Last Post: 03-05-2013, 09:22 AM
  3. Creating a vlookup and skipping blank cells.
    By Yscari in forum Excel General
    Replies: 2
    Last Post: 01-03-2011, 01:24 PM
  4. create a dynamic array skipping #N/A and blank rows?
    By William DeLeo in forum Excel General
    Replies: 5
    Last Post: 11-10-2010, 02:55 PM
  5. Loop skipping random rows for no reason at all
    By GeorgY in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-16-2009, 01:49 AM

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