+ Reply to Thread
Results 1 to 5 of 5

Auto Populate Cells Based on a Criteria

Hybrid View

  1. #1
    Registered User
    Join Date
    01-02-2014
    Location
    Philippines
    MS-Off Ver
    Excel 2010
    Posts
    95

    Exclamation Auto Populate Cells Based on a Criteria

    i have an excel sheet attached and I need to be able to auto populate the cells based on a criteria (Case Number) using VBA. I am able to do this using excel formula but it takes time to loaod the results so I thought it would be better to use VBA instead. Can somebody help me pls. thanks in advance
    Attached Files Attached Files
    Last edited by danallamas; 05-15-2017 at 08:08 PM.

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,290

    Re: Auto Populate Cells Based on a Criteria

    Hi danallamas

    If I understood your requirement correctly then this solves:
    Option Explicit
    
    Sub CaseNoPop()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim CaseNumber As String, FindNumber As String, rng As Range, cell As Range
    Dim lRow As Long, nRow As Long
    Application.ScreenUpdating = False
    Set ws1 = Sheets("Tracker")
    Set ws2 = Sheets("DumpSheet")
    lRow = ws2.Cells(Rows.Count, "F").End(xlUp).Row
    CaseNumber = ws1.Range("C2")
    Set rng = ws2.Range("F2:F" & lRow)
            For Each cell In rng
                FindNumber = cell.Value
                If FindNumber = CaseNumber Then
                    With ws1
                        nRow = ws1.Cells(Rows.Count, "B").End(xlUp).Row + 1
                        .Cells(nRow, 1) = CaseNumber' I made provision for Case Number in Col A 
                        .Cells(nRow, 2) = cell.Offset(, -5).Value
                        .Cells(nRow, 3) = cell.Offset(, 2).Value
                        .Cells(nRow, 4) = cell.Offset(, 3).Value
                        .Cells(nRow, 5) = cell.Offset(, 4).Value
                        .Cells(nRow, 6) = cell.Offset(, 5).Value
                    End With
                End If
            Next cell
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Auto Populate Cells Based on a Criteria

    You may try something like this to make it more robust.

    Right click on Tracker Sheet Tab --> View Code --> Place the following two codes into the opened code window.
    The selection change event code will insert a drop down list of Case Numbers dynamically when you select the cell C2.
    The change event code will trigger a macro that will fetch the relevant data from the DumpSheet as per the case number selected in C2.

    Codes on Tracker Sheet Module:
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    Dim lr As Long
    If Target.Address(0, 0) = "C2" Then
        If Target <> "" Then
            Application.EnableEvents = False
            Call GetRelevantData
            Application.EnableEvents = True
        Else
            Application.EnableEvents = False
            lr = Cells(Rows.Count, 2).End(xlUp).Row
            If lr > 5 Then Range("B6:F" & lr).Clear
            Application.EnableEvents = True
        End If
    End If
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    Dim sws As Worksheet
    Dim x, dict
    Dim lr As Long, i As Long
    
    If Target.Address(0, 0) = "C2" Then
        Set sws = Sheets("DumpSheet")
        lr = sws.Cells(Rows.Count, 6).End(xlUp).Row
        x = sws.Range("F2:F" & lr).Value
        Set dict = CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(x, 1)
            dict.Item(x(i, 1)) = ""
        Next i
        With Target.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                                xlBetween, Formula1:=Join(dict.keys, ",")
        End With
    End If
    End Sub
    Code on Standard Module:
    Now insert a New Module and place the following code into the new code window.
    Sub GetRelevantData()
    Dim dws As Worksheet, sws As Worksheet
    Dim lr As Long
    Application.ScreenUpdating = False
    Set dws = Sheets("Tracker")
    Set sws = Sheets("DumpSheet")
    lr = dws.UsedRange.Rows.Count
    
    If lr > 5 Then dws.Range("B6:F" & lr).Clear
    
    sws.Range("A1").CurrentRegion.AdvancedFilter Action:=xlFilterCopy, _
                                                CriteriaRange:=dws.Range("C1:C2"), _
                                                CopyToRange:=dws.Range("B5:F5"), _
                                                Unique:=False
    
    
    lr = dws.Cells(Rows.Count, 2).End(xlUp).Row
    If lr > 5 Then
        dws.Range("B5").CurrentRegion.Borders.Color = vbBlack
    End If
    
    Application.ScreenUpdating = True
    End Sub
    For details, refer to the attached. To test the code, select a case number in C2 on tracker sheet.
    Attached Files Attached Files
    Last edited by sktneer; 05-15-2017 at 04:29 AM.
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,290

    Re: Auto Populate Cells Based on a Criteria

    If you are just wanting to filter DumpSheet then add a ComboBox to DumpSheet with the following code:
    In a std module:
    Option Explicit
    
    Sub Auto_Open()
    Dim cell As Range, rng As Range
    Dim FirstCell As String, lRow As Long
    lRow = Sheets("DumpSheet").Cells(Rows.Count, "F").End(xlUp).Row
    Set rng = Sheets("DumpSheet").Range("F2:F" & lRow)
        FirstCell = rng.Cells(2, 6).Address
        For Each cell In rng
        
            If Evaluate("=COUNTIF(" & FirstCell & ":" & cell.Address(0, 0) & ", " & cell.Address & ")") = 1 Then
                Sheets("DumpSheet").ComboBox1.AddItem cell.Value
            End If
        Next cell
    End Sub
    ComboBox code
    Option Explicit
    
    Private Sub ComboBox1_Change()
    Application.ScreenUpdating = False
    Range("F2").Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=6, Criteria1:=ComboBox1.Value, Operator:=xlAnd
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    01-02-2014
    Location
    Philippines
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: Auto Populate Cells Based on a Criteria

    thank you this is exactly what I need. I just need to figure out how to automatically run the code whenever i input a case number. but thank you so much

+ 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. Auto Populate a List Based on Specific Criteria from Another Sheet
    By Asalas2 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-30-2015, 03:40 PM
  2. Auto Populate based on 2 criteria using Array
    By lexusap in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 03-23-2015, 02:56 AM
  3. How to auto populate specific data to a different worksheet based off a criteria
    By excelnewbie716 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-31-2014, 03:27 PM
  4. Auto Populate Ranges based on Criteria
    By wrasser in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-17-2014, 11:07 PM
  5. Replies: 3
    Last Post: 03-11-2014, 02:10 PM
  6. Auto Populate a list based off of certain criteria
    By finance123 in forum Excel General
    Replies: 3
    Last Post: 03-29-2012, 03:50 PM
  7. Replies: 2
    Last Post: 02-10-2012, 02:00 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