+ Reply to Thread
Results 1 to 4 of 4

VBA Multiple Criteria and VLoopUp

Hybrid View

JacobRavn VBA Multiple Criteria and... 07-25-2018, 05:38 PM
GC Excel Re: VBA Multiple Criteria and... 07-25-2018, 06:15 PM
JacobRavn Re: VBA Multiple Criteria and... 07-28-2018, 01:00 PM
mehmetcik Re: VBA Multiple Criteria and... 07-25-2018, 06:22 PM
  1. #1
    Registered User
    Join Date
    09-11-2016
    Location
    Copenhagen
    MS-Off Ver
    2016
    Posts
    3

    VBA Multiple Criteria and VLoopUp

    Hi Guys,

    Im am trying to make a code that can help me automate the making of contracts. Im on the right track but it seems like it's still lacking a bit of code.

    In plain English this is want my code to do:

    1: Read the variable "AptCode" and check if is the C-column on sheet shTenants
    2: If AptCode is found in in the C-column test if that same line has the "ACTIVE" status found in Column F in shTenants
    3: If both of the conditions are met, being AptCode and Active, then VLookup and print Tenant_Name and FavColor to cell A5:G9
    4: Loop for all matches

    I would like the the code to return the following:

    "Batman likes the color Black, and Barbie Girl likes the color Pink"

    Problem no.1: Crocodile Dundee no longer lives in the apartment since the status has been marked as TERMINATED
    Problem no.2: The code only writes the data of Batman and not of Barbie Girl. It lacks the loop.


    Can you guys help me? This is my very first VBA project!

    Here's my code:


    Option Explicit
    Sub ContractGenerator2()
    
    'Defining variables BEGIN
        Dim AptCode As Long
            AptCode = shContract.Cells(4, 3)
        Debug.Print AptCode
        
        Dim Last_Row As Long
            Last_Row = shTenants.Cells(shTenants.Rows.Count, 1).End(xlUp).Row
        Debug.Print Last_Row
            
                Dim iRange As Long
                    For iRange = 2 To Last_Row
                                            
        Dim Tenant_Names As String
            Tenant_Names = Application.VLookup(AptCode, shTenants.Range("C:F"), 2, False)
        Debug.Print Tenant_Names
        
        Dim FavColor As String
            FavColor = Application.VLookup(AptCode, shTenants.Range("C:F"), 3, False)
        Debug.Print FavColor
    'Defining variables END
    
    
    'Code BEGIN
      
                If shTenants.Range("C" & iRange).Value = AptCode Then
                    If shTenants.Range("F" & iRange).Value = "ACTIVE" Then
                    shContract.Range("A5:G6") = Tenant_Names & " likes the color " & FavColor
                    End If
                End If
            Next
    End Sub
    
    'Code END
    Attached Files Attached Files
    Last edited by JacobRavn; 07-28-2018 at 01:02 PM. Reason: Solved

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: VBA Multiple Criteria and VLoopUp

    Hi Jacob and welcome to the forum,

    Here's a suggestion. The code is slightly different then your but I put some comments for your understanding.

    Sub SmallTest2()
       Dim AptCode As Long
       Dim ar
       Dim i As Long
       Dim str As String
       
       'get the app. number
       AptCode = shContract.Cells(4, 3).Value
       
       'store the values in a array for faster processing
       ar = shTenants.Cells(1).CurrentRegion.Value
       
       'loop through the array, starting at row 2 (row 1 = header)
       For i = 2 To UBound(ar, 1)
          'if both conditions are met
          If ar(i, 3) = AptCode And ar(i, 6) = "ACTIVE" Then
             'store in a string
             str = str & ar(i, 4) & " like the color " & ar(i, 5)
             'add " and " for possible other match
             str = str & " and "
          
          End If
       Next i
       
       ' remove last 5 characters ( " and ")
       str = Left(str, Len(str) - 5)
       ' output to cells A5
       shContract.Range("A5").Value = str
       
    End Sub
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Registered User
    Join Date
    09-11-2016
    Location
    Copenhagen
    MS-Off Ver
    2016
    Posts
    3

    Re: VBA Multiple Criteria and VLoopUp

    This actually solved it! I had to add a few things in order to make it fit the real project, but it works like a charm!!!

    Thanks a lot! 5 stars!!!!


    Quote Originally Posted by GC Excel View Post

    Sub SmallTest2()
       Dim AptCode As Long
       Dim ar
       Dim i As Long
       Dim str As String
       
       'get the app. number
       AptCode = shContract.Cells(4, 3).Value
       
       'store the values in a array for faster processing
       ar = shTenants.Cells(1).CurrentRegion.Value
       
       'loop through the array, starting at row 2 (row 1 = header)
       For i = 2 To UBound(ar, 1)
          'if both conditions are met
          If ar(i, 3) = AptCode And ar(i, 6) = "ACTIVE" Then
             'store in a string
             str = str & ar(i, 4) & " like the color " & ar(i, 5)
             'add " and " for possible other match
             str = str & " and "
          
          End If
       Next i
       
       ' remove last 5 characters ( " and ")
       str = Left(str, Len(str) - 5)
       ' output to cells A5
       shContract.Range("A5").Value = str
       
    End Sub

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: VBA Multiple Criteria and VLoopUp

    
    
    Sub test()
    
    With Sheets("Data Tenants")
    LR = .Cells(Rows.Count, 1).End(xlUp).Row
    MyArray = .Range("A2:F" & LR).Value
    End With
    
    For Count = 1 To LR - 1
    
    If MyArray(Count, 3) <> "" And MyArray(Count, 6) = "ACTIVE" Then
    
        Sheets("Contract Template").Copy Before:=Sheets(1)
        ActiveSheet.Name = "Contract - " & MyArray(Count, 3) & " - " & MyArray(Count, 4)
    
        Range("C4").Value = MyArray(Count, 3)
        Range("A5").Value = MyArray(Count, 4) & " Likes the color " & MyArray(Count, 6)
        
    End If
    
    Next
    End Sub
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

+ 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. Want to use 2 IF, IS BLANK, VLOOPUP together
    By anilanilpatni in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-08-2017, 03:42 AM
  2. [SOLVED] VLOOPUP Formula help
    By bordemkills in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-28-2017, 01:23 AM
  3. [SOLVED] aSSISTANCE WITH ROW DELETE BASED ON MULTIPLE VLOOPUP CRITERIA FROM ANOTHER WORKBOOK
    By Sintek in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-21-2016, 04:34 PM
  4. [SOLVED] Index, Match, or VLoopup? Returning a table value based on criteria from two drop down's
    By jkwlvsky in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-18-2015, 06:40 PM
  5. Multiple VLOOPUP Values and Criteria
    By Thomasdaddy1 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-15-2013, 02:20 PM
  6. Vloopup VBA help
    By craigmck in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-17-2011, 10:59 AM
  7. VLOOPUP
    By Fran in forum Excel General
    Replies: 6
    Last Post: 06-08-2006, 09:25 AM

Tags for this Thread

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