Results 1 to 10 of 10

Thread: Table Or Array That Can Auto Fill In Column Information Based On Two Cells Text Value

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    HI,

    I have made some more comments and write the actual sheet names in the code. Hope this helps you.

    Code:
    Sub kTest()
        
        Dim MyData          As Variant
        Dim MyTable         As Variant
        Dim i               As Long
        Dim r               As Long
        Dim rFound          As Variant
        Dim wksIniDevices   As Worksheet
        Dim wksPartNumList  As Worksheet
        Dim rngIniDevices   As Range
        
        Set wksIniDevices = ThisWorkbook.Worksheets("Initiating devices")
        Set wksPartNumList = ThisWorkbook.Worksheets("Sheet2")
        
        With wksIniDevices
            r = .Range("a" & .Rows.Count).End(xlUp).Row         'find last row in col A
            Set rngIniDevices = .Range("a2:f" & r)              'range where your data
            MyData = rngIniDevices.Value2                       'convert the range into variant array as array works faster than range
        End With
        
        With wksPartNumList
            r = .Range("a" & .Rows.Count).End(xlUp).Row
            MyTable = .Range("a2:c" & r).Value2                 'part num list
            .Range("a2:a" & r).Name = "Type"                    'define a name range to use in formula
            .Range("b2:b" & r).Name = "Manu"                    'define a name range to use in formula
    
            .Range("h2").FormulaArray = "=match(f2&""|""&g2,type & ""|"" & manu,0)" 'put the formula. see f2 and g2 - adjust these cells if you use a different cells
            
            For i = 1 To UBound(MyData, 1)
                'put the Type nad Manu in F2 and G2 respectively.adjust f2 and g2 if necessary
                .Range("f2") = MyData(i, 2) 'type
                .Range("g2") = MyData(i, 4) 'manu
                'find match row
                rFound = .Range("h2").Value2
                If Not IsError(rFound) Then
                    'insert part # in the array
                    MyData(i, 5) = MyTable(rFound, 3)
                End If
            Next
        End With
        'write back the array with parts
        rngIniDevices = MyData
        
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  2. #2
    Member
    Join Date
    Jun 2013
    Posts
    52
    Rep Power
    13
    OK you changed a few things from the first one but I just want to get one thing straight if you could explain this part of the code.
    Code:
    With wksIniDevices
            r = .Range("a" & .Rows.Count).End(xlUp).Row         'find last row in col A
            Set rngIniDevices = .Range("a7:f" & r)              'range where your data
            MyData = rngIniDevices.Value2                       'convert the range into variant array as array works faster than range
        End With
        
        With wksPartNumList
            r = .Range("a" & .Rows.Count).End(xlUp).Row
            MyTable = .Range("a2:c" & r).Value2                 'part num list
            .Range("a2:a" & r).Name = "Type"                    'define a name range to use in formula
            .Range("b2:b" & r).Name = "Manu"                    'define a name range to use in formula
    
            .Range("h2").FormulaArray = "=match(f2&""|""&g2,type & ""|"" & manu,0)" 'put the formula. see f2 and g2 - adjust these cells if you use a different cells
            
            For i = 1 To UBound(MyData, 1)
                'put the Type nad Manu in F2 and G2 respectively.adjust f2 and g2 if necessary
                .Range("f2") = MyData(i, 2) 'type
                .Range("g2") = MyData(i, 4) 'manu
                'find match row
                rFound = .Range("h2").Value2
                If Not IsError(rFound) Then
                    'insert part # in the array
                    MyData(i, 5) = MyTable(rFound, 3)
                End If
            Next
        End With
        'write back the array with parts
        rngIniDevices = MyData

    In one of the comments you say to adjust f2 and g2 to the correct cells if needed. I'm confused about what this should actually be. I changed the name of the worksheet to "AUTO FILL PARTS" to fit what I have the sheet name for. I changed that in the set wksPartNumList = thisworkbook.worksheets ("AUTO FILL PARTS")

    Now this is where I get confused
    Code:
    .Range("h2").FormulaArray = "=match(f2&""|""&g2,type & ""|"" & manu,0)"
    I'm assuming that this range is referring to the part number list if that Is the case then the range would be the range for the column Type and Manuf?
    If that is the case then it would change to A2 and b2 correct or is this referring to something else?

    Almost there but still not 100% sure.

  3. #3
    Member
    Join Date
    Jun 2013
    Posts
    52
    Rep Power
    13
    thanks a ton i got the first version of the code to work by changing my range and simply adding the "" that i forgot all about to the name of the worksheet. Wow I feel stupid that's why im far from the expert. It works fine now as far as i can tell thanks once again.

    Great job!

Similar Threads

  1. Replies: 4
    Last Post: 07-08-2013, 05:36 PM
  2. Replies: 3
    Last Post: 05-23-2013, 11:17 PM
  3. Replies: 4
    Last Post: 05-01-2013, 09:49 PM
  4. Replies: 1
    Last Post: 12-04-2012, 08:56 AM
  5. Unmerge Cells and Fill with Duplicate Values
    By princ_wns in forum Excel Help
    Replies: 3
    Last Post: 10-09-2012, 07:36 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •