Results 1 to 5 of 5

Split match array results into new worksheets

Threaded View

  1. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Split match array results into new worksheets

    I am assuming the values in column C are numeric, not text. Your pseudo code above would not work for actual NUMERIC tests, only for text comparisons. If you search for 1 in a cell with a value 15, it will match using INSTR. So I did not use that.

    If the column C values are actually text strings, we'll need to discuss exactly what those strings look like for all the 1-17 examples to be sure we get an accurate test.

    Meanwhile, using straight numeric testing in column C, this seems to work.
    Option Explicit
    Option Compare Text
      
    Sub GetData()
    Dim MainArr As Variant, DestArr As Variant, wbOUT As Workbook
    Dim LRow As Long, i As Long, o As Long, c As Long
    Dim DMAValue As Long, RowCount As Long, FileNum As Long
    
    Application.ScreenUpdating = False
    
    If MsgBox("Proceed?", vbOKCancel) <> vbOK Then Exit Sub
    
    DMAValue = Application.InputBox("Please input the DMA number required (1-17).", "DMA Value", 2, Type:=1)
    RowCount = Application.InputBox("Please input the number of rows per spreadsheet.", "Rows Per Output File", 10, Type:=1)
    
    With ThisWorkbook.Sheets(2)
        LRow = .Range("A" & Rows.Count).End(xlUp).Row
        MainArr = .Range("A2:H" & LRow).Value
    End With
    
    'create empty output array
    ReDim DestArr(1 To RowCount, 1 To 8)
    
    'prime the output row
    o = 0
    
    'fill in specific number of rows
    For i = LBound(MainArr) To UBound(MainArr)
        If MainArr(i, 3) = DMAValue And InStr(MainArr(i, 4), "N") > 0 Then
            'increment next output row because we are about to write data now
            o = o + 1
            'add data to destarr in output row
            For c = 1 To 8
                DestArr(o, c) = MainArr(i, c)
            Next c
        End If
        'check if we are at the end of either array
        If (i = UBound(MainArr) Or o = UBound(DestArr)) Then
            'check if there is data to write out in the destarr
            If DestArr(2, 1) <> "" Then
                'increment the output filenum
                FileNum = FileNum + 1
                Set wbOUT = Workbooks.Add
                wbOUT.Sheets(1).Range("A2:H2").Resize(RowCount).Value = DestArr
                'add headers
                ThisWorkbook.Sheets(2).Range("A1:H1").Copy wbOUT.Sheets(1).Range("A1")
                ActiveSheet.Columns.AutoFit
                wbOUT.SaveAs ThisWorkbook.Path & Application.PathSeparator & "DMA-" & DMAValue & "-" & FileNum & ".xlsx", 51
                wbOUT.Close False
                
                'clear the destarr data
                ReDim DestArr(1 To RowCount, 1 To 8)
                'reset output tow to 1, we use row because we don't want to ever enter the loop on a datarow,
                'else the workbook will get written out before it is completely filled.
                o = 0
            End If
        End If
    Next i
    
    MsgBox "Done - a total of " & FileNum & " files were created for DMA value " & DMAValue
    Application.ScreenUpdating = True
    
    End Sub
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Match 3 cells are equal or not, return results in a row array
    By lmattenl in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-17-2019, 03:50 AM
  2. [SOLVED] Index/Match only returning results from 1st in array cosntant
    By smorice in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-21-2019, 04:25 PM
  3. (SOLVED) help with Array to look up multiple match results from column
    By clundeen in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-03-2017, 12:24 PM
  4. Sum of multiple Index/Match results using array formula
    By cray36 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 08-25-2015, 11:12 PM
  5. [SOLVED] Index/Match Array with multiple results concatenate in one cell
    By samiesosa in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-15-2013, 01:34 PM
  6. Match Closest Results from Data Array
    By TheRobsterUK in forum Excel General
    Replies: 2
    Last Post: 09-29-2005, 08:48 AM
  7. split results to multiple worksheets
    By Rich in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-11-2005, 03:06 PM

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