+ Reply to Thread
Results 1 to 19 of 19

Help Building Array

Hybrid View

jkelly228 Help Building Array 04-07-2011, 04:33 PM
jkelly228 Re: Help Building Array 04-08-2011, 11:52 AM
jkelly228 Re: Help Building Array 04-08-2011, 01:24 PM
Brad4444 Re: Help Building Array 04-08-2011, 01:43 PM
jkelly228 Re: Help Building Array 04-08-2011, 01:44 PM
Brad4444 Re: Help Building Array 04-08-2011, 01:48 PM
jkelly228 Re: Help Building Array 04-08-2011, 01:53 PM
Brad4444 Re: Help Building Array 04-08-2011, 02:09 PM
Brad4444 Re: Help Building Array 04-08-2011, 02:13 PM
jkelly228 Re: Help Building Array 04-08-2011, 02:21 PM
Brad4444 Re: Help Building Array 04-08-2011, 03:04 PM
jkelly228 Re: Help Building Array 04-11-2011, 12:42 PM
jkelly228 Re: Help Building Array 04-11-2011, 04:44 PM
watersev Re: Help Building Array 04-12-2011, 03:37 AM
jkelly228 Re: Help Building Array 04-12-2011, 12:30 PM
watersev Re: Help Building Array 04-13-2011, 09:35 AM
jkelly228 Re: Help Building Array 04-13-2011, 02:14 PM
watersev Re: Help Building Array 04-13-2011, 05:32 PM
jkelly228 Re: Help Building Array 04-14-2011, 01:49 PM
  1. #1
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Help Building Array

    Hello All,

    This goes hand in hand with the thread located here, but it is only a small portion of it. Trying to build an array of Sheet 1 Column A & B and Sheet 3 Column A & B, then compare and where they match move the values from Sheet 3 to Sheet 1.

    This is the code that I have so far, and I have never used variables before.

    Sub Date_Range()
    
        Sheet3.Select
        Dim ItemLookup3        As Long
        ItemLookup = Cells.Find(What:="*", _
        SearchDirection:=xlPrevious, _
        SearchOrder:=xlByRows).Row
        
        Sheet1.Select
        Dim ItemLookup1        As Long
        ItemLookup = Cells.Find(What:="*", _
        SearchDirection:=xlPrevious, _
        SearchOrder:=xlByRows).Row
        
        Dim MyArr3 As Variant
        Dim MyArr1 As Variant
    
        With Sheets(3): Set MyArr3 = .Range(.[a2], .Cells(Rows.Count, "a").End(xlUp)): End With
        With Sheets(1): Set MyArr1 = .Range(.[a2], .Cells(Rows.Count, "a").End(xlUp)): End With: i = 1
    
        Sheet3.Select
        Dim LoopColumn As Long
        Dim LoopColumn2 As Long
        For LoopColumn = 2000 To 2 Step -1
            If "MyArr3" = "MyArr1" Then
                Range(Cells(LoopColumn, 3), Cells(LoopColumn, 11)).Copy
                Sheet1.Select
                Cells(, 1) = "MyArr1"
                Range(Cells(, 4)).PasteSpecial Paste:=xlPasteValues
            End If
        Next LoopColumn
    
    End Sub
    Last edited by jkelly228; 04-14-2011 at 01:50 PM.

  2. #2
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Help Building Array

    In building my array I have come across a function that allows me to combine 2 arrays to create a string. The problem with the function though is that it creates a seperate sub routine. Not sure how to use a function without it breaking up my sub routine. Below is the code. Any help on this is greatly appreciated as I am quickly running out of time to finish this.

    Sub Date_Range()
    
        Sheet3.Select
        Dim ItemLookup3        As Long
        ItemLookup = Cells.Find(What:="*", _
        SearchDirection:=xlPrevious, _
        SearchOrder:=xlByRows).Row
        
        Sheet1.Select
        Dim ItemLookup1        As Long
        ItemLookup = Cells.Find(What:="*", _
        SearchDirection:=xlPrevious, _
        SearchOrder:=xlByRows).Row
        
        Dim MyArr3 As Variant
        Dim MyArr1 As Variant
    
        With Sheets(3): Set MyArrItem = .Range("a:a") ', .Cells(Rows.Count, "a").End(xlUp)) ': End With
        With Sheets(3): Set MyArrOrder = .Range("B:B")
        With Sheets(1): Set MyArr1 = .Range("a:a") ', .Cells(Rows.Count, "a").End(xlUp)) ': End With: i = 1
        With Sheets(1): Set MyArrOrder1 = .Range("B:B")
    
        Public Function CombineTwoDArrays(MyArrItem As Variant, _
        MyArrOrder As Variant) As Variant: End Function
    
    
        Public Function CombineTwoMoreDArrays(MyArr1 As Variant, _
        MyArrOrder1 As Variant) As Variant: End Function
    
        Sheet3.Select
        Dim LoopColumn As Long
        Dim LoopColumn2 As Long
        For LoopColumn = 2000 To 2 Step -1
            If Cells(LoopColumn & CombineTwoDArrays) = "CombineTwoMoreDArrays" Then
                Range(Cells(LoopColumn, 3), Cells(LoopColumn, 11)).Copy
                Sheet1.Select
              '  Range(Cells(, 1) = "010344-001R" And Cells(, 2)) = "Purchase requisition"
                Range(Cells("MyArr1", 4)).PasteSpecial Paste:=xlPasteValues
                'Selection.PasteSpecial Paste:=xlPasteValues
            End If
        Next LoopColumn
        End With: End With: End With: End With
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Help Building Array

    Hello All,

    Still working on building an array, and I will need that pretty quick. However, right now maybe someone can help me with some simple code fix. In the code highlighted in red it keeps giving me an error (code line 8 in case it doesn't highlight red). Does anyone know what I am doing wrong. I appreciate any help. Thanks

    Kelly

        Dim ItemLookup1        As Long
        ItemLookup = Cells.Find(What:="*", _
        SearchDirection:=xlPrevious, _
        SearchOrder:=xlByRows).Row
    
        Dim LoopColumn2 As Long
        For LoopColumn2 = 2000 To 2 Step -1
            If Cells(LoopColumn2, 1) = "Item NUmber" And Cells(LoopColumn2, 2) = "Purchase requisition" Then
                Range(Cells(LoopColumn2, 4)).Select. PasteSpecial Paste:=xlPasteValues            
            End If
        Next LoopColumn2
    Last edited by jkelly228; 04-08-2011 at 01:52 PM.

  4. #4
    Registered User
    Join Date
    04-04-2011
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: Help Building Array

    Is the extra period supposed to be in line 8? Between Select and PasteSpecial?

    EDIT:

    I'd do this

            If Cells(LoopColumn2, 1) = "010344-001R" And Cells(LoopColumn2, 2) = "Purchase requisition" Then
                Cells(LoopColumn2, 4).Select
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False     
            End If
    Last edited by Brad4444; 04-08-2011 at 01:46 PM.

  5. #5
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Help Building Array

    I'm not sure to be honest. If not, how should it look?

  6. #6
    Registered User
    Join Date
    04-04-2011
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: Help Building Array

    Read above code.

  7. #7
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Help Building Array

    Thank you, Thank you, Thank you. Brad4444.

    I really appreciate the help.

    So, do you know anything about creating an array? As you can see from my code I have one specific Item number. Now, I need the system to compare hundreds of Items and Order Types from one sheet to a second sheet and where it finds match do the same copy and paste function as you see in the original code.

    Thank you for the help. I really appreciate it.

  8. #8
    Registered User
    Join Date
    04-04-2011
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: Help Building Array

    This goes hand in hand with the thread located here, but it is only a small portion of it. Trying to build an array of Sheet 1 Column A & B and Sheet 3 Column A & B, then compare and where they match move the values from Sheet 3 to Sheet 1.
    Unfortunately, I am still new to VBA and have a thread myself open on this forum asking for help with an array. So my question is do you have to use an array to do this? As in it was part of the assignment?
    Also are you looking for exact matches?
    I would read the values of sheet 1 into an array. Then read the values of sheet 3 into a different array. Then it should be fairly easy, I think, to compare them. If you find a match go to that specific cell and copy the values to where you need to go.
    The code below is some stuff I was working on earlier this week and I think it might give you some ideas possibly.

    EDIT: The code below is used to find the last rows of a column and also looping to do stuff on each row possibly. Sorry I couldn't be more help.

    Dim LastRow As Integer
    Dim iRow As Integer
    
    'Find where data ends.
    LastRow = Cells(65536, "A").End(xlUp).row
    For iRow = 2 To LastRow
        Select Case Cells(iRow, "B").Value
            Case "X"
        Call CopyX(Range(Cells(iRow, "A"), Cells(iRow, "D")))
            Case "XX"
        Call CopyXX(Range(Cells(iRow, "A"), Cells(iRow, "D")))
            Case "XXX"
        Call CopyXXX(Range(Cells(iRow, "A"), Cells(iRow, "D")))
        End Select
    Next iRow
    
    End Sub
    
    Sub CopyX(row As Range)
        'Defining data types.
        Dim LowerLeftCell As Long
        Dim Rng As Range
    
        row.Copy Destination:=Worksheets("Formulas").Range("G2:J2")
    
        LowerLeftCell = Sheets("Master_Inputs").Range("A" & Rows.Count).End(xlUp).row
        Set Rng = Sheets("Formulas").Range("A4:F61")
        Rng.Copy
        Sheets("Master_Inputs").Range("A" & LowerLeftCell + 1).PasteSpecial xlPasteValues
        Application.CutCopyMode = False
        
        LowerLeftCell = Sheets("Master_Outputs").Range("A" & Rows.Count).End(xlUp).row
        Set Rng = Sheets("Formulas").Range("A63:F79")
        Rng.Copy
        Sheets("Master_Outputs").Range("A" & LowerLeftCell + 1).PasteSpecial xlPasteValues
        Application.CutCopyMode = False
    
    
    End Sub
    Ask your teacher for an extension and you might get it.

  9. #9
    Registered User
    Join Date
    04-04-2011
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: Help Building Array

    http://www.excelforum.com/excel-prog...correctly.html

    My discussion on arrays. I can make the array just not use it correctly for my purpose.

  10. #10
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Help Building Array

    Hi Brad,

    Nope, not an assignment for School. Something I am working for my job. I have thousands of rows of data with various Devices on 2 different sheets. I need find how much we are shipping of one device and then paste it into the correct cell of another sheet.

    The attached spreadhsheet shows a small portion of what I am looking for. The actual spreadsheet has hundreds of items on more than 20,000 rows of data.

    Thanks for the help

    Kelly
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    04-04-2011
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: Help Building Array

    If your due date can wait till Monday I can look at it tonight. I need to learn VBA better anyways so it will give me something to do.

  12. #12
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Help Building Array

    Hello All,

    Still stuck on the Array function. I have the code all down to where if I identify a specific Item and Order Type then it runs perfectly, but what I need is it to do a mass comparison. This is really holding me up, if anyone can help I would appreciate it. Thanks

    Kelly

  13. #13
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Help Building Array

    Hello All,

    The below code still doesn't work completely, but I think I am closer. The error I get now is 'Type Mismatch' at "If MyArr3 = MyArr1 Then". So not sure what the fix is for that. if anyone can help I would appreciate it.

    Thanks

    Sub Date_Range()
    
        Dim MyArr3 As Variant
        Dim MyArr1 As Variant
            
        Sheet3.Select
        Dim ItemLookup3        As Long
        ItemLookup = Cells.Find(What:="*", _
        SearchDirection:=xlPrevious, _
        SearchOrder:=xlByRows).Row
        
        Sheet1.Select
        Dim ItemLookup1        As Long
        ItemLookup = Cells.Find(What:="*", _
        SearchDirection:=xlPrevious, _
        SearchOrder:=xlByRows).Row
        
    
        With Sheets(3): Set MyArr3 = .Range(.[a2], .Cells(Rows.Count, "b").End(xlUp)): End With
        With Sheets(1): Set MyArr1 = .Range(.[a2], .Cells(Rows.Count, "b").End(xlUp)): End With: i = 1
        
        Sheet3.Select
        Dim LoopColumn As Variant
        Dim LoopColumn2 As Variant
        For LoopColumn = 2000 To 2 Step -1
            If MyArr3 = MyArr1 Then
                Range(Cells(LoopColumn, 3), Cells(LoopColumn, 11)).Copy
                Sheet1.Select
                Call Find_Part2
            End If
        Next LoopColumn
    
    End Sub
    
    Sub Find_Part2()
    
        Dim ItemLookup1        As Long
        ItemLookup = Cells.Find(What:="*", _
        SearchDirection:=xlPrevious, _
        SearchOrder:=xlByRows).Row
    
        Dim LoopColumn2 As Long
        For LoopColumn2 = 2000 To 2 Step -1
            If Cells(LoopColumn2, 1) = "MyArr3" And Cells(LoopColumn2, 2) = "MyArr1" Then
                Cells(LoopColumn2, 4).Select '.PasteSpecial Paste:=xlPasteValues
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End If
        Next LoopColumn2
    
    End Sub

  14. #14
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Help Building Array

    hi, jkelly228, can you show real data sample file (100-200 rows will be enough) and result you are trying to obtain?

  15. #15
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Help Building Array

    Hi Watersev,

    Attached is a report with a lot more lines. The overall look and even the names of the Sheets match the ultimate look of the report. I apreciate any help you can provide. Thanks

    Kelly
    Attached Files Attached Files

  16. #16
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Help Building Array

    hi, jkelly228, please check attachment, run code "test
    Attached Files Attached Files
    Last edited by watersev; 04-13-2011 at 10:00 AM.

  17. #17
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Help Building Array

    @Watersev,

    Thank you very much. I really appreciate the help. I am working on moving this over to my production file, but it isn't going as smoothly. I can watch on yours as variable "b" is built and filled in. However, when I move to the production file that same variable is never filled in. It just skips right over.

    Below is the code that I am using. As far as I can tell the spreadsheets are nearly almost exactly the same, the only difference is that I am using real Item Numbers. Variable "c" seems to be just fine. Not sure why one would work and another wouldn't. Exact same code.

    However, when I copy column A from my spreadsheet and put it in yours, I get the exact same problem. So it seems to be something with my data that is causing it to skip over building the "b" variable. Any idea what could cause this?

    Within this code, what is the purpose of the "15"

    With Sheets("BalanceBySku"): b = .Range(.[a1], .Cells(Rows.Count, "a").End(xlUp).Offset(, 15))
    I want to say that I understand most of the rest of the code, but obviously I am still missing something. Thanks again.

    Sub Qty_Fill()
    Dim a As New Collection, b, c, i As Long, n As Long, irow As Long
    Application.ScreenUpdating = False: On Error Resume Next
    With Sheets("BalanceBySku"): b = .Range(.[a1], .Cells(Rows.Count, "a").End(xlUp).Offset(, 15))
    For i = 2 To UBound(b)
       If b(i, 1) <> "" Then a.Add (i), b(i, 1) & b(i, 2)
    Next: Err.Clear
    With Sheets("ASCPivot"): c = .Range(.[a6], .[a6].End(xlDown).Offset(, 10)): End With
    ReDim d(1 To UBound(b), 1 To 8)
    For i = 1 To UBound(c)
        a.Add (i), (c(i, 1) & c(i, 2))
        If Err.Number <> 0 Then
            irow = a.Item(c(i, 1) & c(i, 2))
                For n = 7 To 15
                    d(irow - 1, n - 6) = c(i, n - 4)
                Next: Err.Clear
        End If
    Next: .[g2].Resize(UBound(d), UBound(d, 2)) = d: End With: Application.ScreenUpdating = True:
    
    End Sub
    Last edited by jkelly228; 04-13-2011 at 02:17 PM.

  18. #18
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Help Building Array

    1. 7 - number of the column for April
    2. 15 - number of the column for December
    3. Post a sample of the data not working with the code, please

  19. #19
    Forum Contributor
    Join Date
    01-04-2011
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Help Building Array

    Hi Watersev,

    It actually did work. I had to change the format of the cells from Date to General and it worked perfectly. Thank you so much for all of the help.

    Kelly

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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