+ Reply to Thread
Results 1 to 3 of 3

Pasting Array information to new worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    08-02-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    44

    Pasting Array information to new worksheet

    I must rely on those more fluent in VBA and array manipulation than moi.
    For the life of me I'm stuck.

    I am placing into an array columns A, B, and D, NOT C.
    Appears this must be done with a union - if not let me know.
    Yes, have Dim myData as Variant

    Set facilityID = Wks.Range("A:B")
    Set triChemical = Wks.Range("D:D")

    myData = Union(facilityID, triChemical)

    In column C is a year. I use an inputbox to tell Excel which year I want data for, e.g., 2014.

    I use an If statement to test the year, i.e., variable name YeatToInput

    'Test the to see which year to extract data
    If bigArray(i, 3) = YearToInput Then
    Worksheets(NewWorkSheetName).Range("A1:C3").Resize(UBound(myData, 2)).Value = myData
    Worksheets(NewWorkSheetName).Columns("A:C").AutoFit

    A new worksheet is generated and some data is pasted to the worksheet, but the last column ends up #NA, I have no clue why?

    Output example:

    Col A Col B Col C

    88330HLLMNHWY70 US DOD USAF HOLLOMAN AFB #N/A
    88330HLLMNHWY70 US DOD USAF HOLLOMAN AFB #N/A

    Also, there should in fact be 3 rows of data not 2.

    Suggestions on correct way to output would be appreciated.

    Help an ole Vet on Veterans Day - smile.
    Thanks,
    Mort

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Pasting Array information to new worksheet

    It's kind of hard to follow what you're describing. Is bigArray the data from the range myData? If yes, you can't read data into an array from a noncontiguous range by using the syntax; Array = noncontiguous_range.Value
    Is that what you did?

    Otherwise, I'm quite lost as to what you're describing. Do you have an example workbook you can post?
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    08-02-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: Pasting Array information to new worksheet

    Thanks you AlphaFrog!! I so agree that 99% of the problems with communicating in writing is EFFECTIVELY EXPLAINING THE ISSUE.
    My issue has been finally resolved and I would like to post the code...ahhh now I see the code tag icon.

    I had a long list of data occupying columns A through F, 40,000 rows long. "Regular VBA Looping Code" took about 5 minutes to run - LONG TIME!!

    I simply wanted to use an array to read all the data in from the main worksheet, then extract only columns A, B, C, when condition in column D was met (here it was a year)

    An InputBox was used to name the year one wanted to extract, as well as the new worksheet name the extracted data would be copied to.
    I have a "smattering" of comments throughout the code so hopefully it will be much easier to decipher? Hope you find useful?

    Option Explicit
    Option Base 1
    
    Sub MyFirstArray()
    Dim bigArray As Variant
    Dim Rng As Range
    Dim i As Integer
    Dim j As Integer
    Dim Wks As Worksheet
    Dim NewWorkSheetName As String
    Dim YearToInput As Variant
    Dim myThirdColumn As Variant
    Dim facilityID As Range
    Dim triChemical As Range
    Dim myData As Variant
    Dim LastUsedRow As Long
    Dim NextRow As Long
    
    Application.ScreenUpdating = False
    
    'Determine the last row where there is data of the original dataset, can use because all columns are same length.
    LastUsedRow = Range("A" & Rows.Count).End(xlUp).Row
    
    'Set the variable Wks to be the name of the worksheet on which the original data is located
    Set Wks = ActiveWorkbook.ActiveSheet
    
    'Input the reporting year of the data you want to extract - Input boxes return a STRING so convert this one to a value since its a year
    YearToInput = InputBox("Enter the first year you want to extract to a new worksheet, then click OK or Enter")
    YearToInput = Val(YearToInput)
    
    'Use an inputbox to name the worksheet on which you want to paste that year's information
    NewWorkSheetName = InputBox("Normally enter the year, will autogenerate, then click OK or Enter", "Name the New Worksheet")
    Sheets.Add.Name = NewWorkSheetName
    
    'Go back and make the original data sheet the active worksheet the newly added sheet is now the active sheet
    Wks.Activate
      With ActiveSheet
        Set Rng = Range("A1:D" & LastUsedRow)
        'Store all the elements, i.e., data, into bigArray to test year in question
        bigArray = Rng.Value
    NextRow = 1
     'Use entire data range to test for the year to be extracted
      For i = 1 To UBound(bigArray)
      
     'Test to see which year to extract data from the main worksheet
            If bigArray(i, 3) = YearToInput Then
            Worksheets(NewWorkSheetName).Cells(NextRow, "A") = bigArray(i, 1)
            Worksheets(NewWorkSheetName).Cells(NextRow, "B") = bigArray(i, 2)
            Worksheets(NewWorkSheetName).Cells(NextRow, "C") = bigArray(i, 4)
            NextRow = NextRow + 1
            End If
        Next i
        
        Worksheets(NewWorkSheetName).Columns("A:C").AutoFit
        Application.CutCopyMode = False
        End With
    Application.ScreenUpdating = True
    End Sub

+ 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: 06-16-2015, 12:28 PM
  2. Copy and pasting information from web
    By welchy82 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-19-2014, 04:07 PM
  3. help assigning elements of dictionary to array and then pasting array into worksheet
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-05-2013, 10:37 AM
  4. [SOLVED] formatting issues pasting date from array to column in worksheet
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-05-2013, 10:42 AM
  5. Replies: 0
    Last Post: 02-04-2013, 02:28 PM
  6. pasting array from VBA into worksheet
    By twd000 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-09-2010, 05:36 PM
  7. UserForm and Pasting Information
    By Sargon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-22-2008, 12:27 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