+ Reply to Thread
Results 1 to 6 of 6

Create a list according what i choose in A1!

Hybrid View

  1. #1
    Registered User
    Join Date
    06-15-2012
    Location
    Greece
    MS-Off Ver
    Excel 2007
    Posts
    85

    Create a list according what i choose in A1!

    Hi all.

    I need your help on this.

    In A1 of the first sheet i choose a customer. In column D of this sheet i need to see ALL the products that this customer works.

    IN the priceList sheet i have in column A the Customers Names.

    Starting from B1 and in the next columns i have my Product Names.

    From b2 and down and for all the columns to the right i have the prices for eatch customer for eatch product.

    If one customer does not use any product then the cell is blank.

    I believe that looking in my example sheet everything will be clear for you.

    Thanks in advance for any advice.
    Attached Files Attached Files

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Create a list according what i choose in A1!

    Hi alpa

    This Code in the attached creates a Dynamic Named Range called "Customers" which is used for Data Validation in A1
    Option Explicit
    
    Private Sub Workbook_Open()
        ActiveWorkbook.Names.Add Name:="Customers", RefersTo:= _
                "=OFFSET(PriceList!$A$2,0,0,(COUNTA(PriceList!$A:$A)-1),1)"
    End Sub
    This Code in the attached then fires each time A1 changes
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim ws1 As Worksheet, ws2 As Worksheet
        Dim Rng As Range, c As Range, cel As Range
        Dim LC As Long, cnt As Long
    
        Set ws1 = ActiveSheet
        Set ws2 = Sheets("PriceList")
        If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
        If Target.Address = "$A$1" Then
    
            Application.EnableEvents = False
            Columns(4).ClearContents
            cnt = 4
            With ws2
                LC = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, _
                        SearchDirection:=xlPrevious).Column
            End With
    
            With ws2.Columns(1)
                Set c = .Find(Target.Value, LookIn:=xlValues)
                If Not c Is Nothing Then
                    Set Rng = .Range(.Cells(c.Row, 2), .Cells(c.Row, LC))
                    For Each cel In Rng
                        If Not cel.Value = "" Then
                            ws1.Cells(cnt, "D").Value = .Cells(1, cel.Column).Value
                            cnt = cnt + 1
                        End If
                    Next cel
                End If
            End With
            Application.EnableEvents = True
        End If
    End Sub
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    06-15-2012
    Location
    Greece
    MS-Off Ver
    Excel 2007
    Posts
    85

    Re: Create a list according what i choose in A1!

    Thank you John and apologize for the delay in my reply.The difference of the hours!!

    This works great in my example.

    Unfortunately for me i am not able to modify your excellent code for my real workbook.

    I need the validation list in B6. Not in A1 and i can not fix this.

    Could you pls explain me which part of the code i have to modify for using the code?

    Also can you explain a little the code so to be able to use it another time with different cell and different columns?

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Create a list according what i choose in A1!

    Hi alpa

    To do this
    I need the validation list in B6
    Change this line of Code
    If Target.Address = "$A$1" Then
    to
    If Target.Address = "$B$6" Then
    Here's the Commented Code
    Option Explicit
    
    Private Sub Workbook_Open()
        'add a Dynamic Named Range for all Customers in PriceList Column A
        ActiveWorkbook.Names.Add Name:="Customers", RefersTo:= _
                "=OFFSET(PriceList!$A$2,0,0,(COUNTA(PriceList!$A:$A)-1),1)"
    End Sub
    and
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim ws1 As Worksheet, ws2 As Worksheet      ')
        Dim Rng As Range, c As Range, cel As Range  ') Dimension all Variables
        Dim LC As Long, cnt As Long                 ')
    
        Set ws1 = ActiveSheet                       ')Assign Sheet Names
        Set ws2 = Sheets("PriceList")               ')to Variables
        If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub 'If more than one Cell changes get out
        If Target.Address = "$B$6" Then             ' If Cell B6 changes then do stuff
    
            Application.EnableEvents = False        'Turn off Event Code
            Columns(4).ClearContents                'Clear the landing zone (Column D)
            cnt = 4                                 'This is the first Row of the landing zone
            With ws2                                'Using ws2
                LC = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, _
                        SearchDirection:=xlPrevious).Column ')Find the last Column in ws2
            End With
    
            With ws2.Columns(1)
                Set c = .Find(Target.Value, LookIn:=xlValues) 'Using ws2, first Column find the value of Cell B6
                If Not c Is Nothing Then                      'if you find it
                    Set Rng = .Range(.Cells(c.Row, 2), .Cells(c.Row, LC)) 'set the Search Range
                    For Each cel In Rng 'look at each Cell to the right of c
                        If Not cel.Value = "" Then 'if it's not blank
                            ws1.Cells(cnt, "D").Value = .Cells(1, cel.Column).Value 'place the Header in the landing zone
                            cnt = cnt + 1 'set the landing zone to the next Row
                        End If
                    Next cel                'find the next item in the Search Range
                End If
            End With
            Application.EnableEvents = True 'turn Event Code back on
        End If
    End Sub

  5. #5
    Registered User
    Join Date
    06-15-2012
    Location
    Greece
    MS-Off Ver
    Excel 2007
    Posts
    85

    Re: Create a list according what i choose in A1!

    Thank you very mutch! Specially for your instructions. I'll study these!

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Create a list according what i choose in A1!

    You're welcome...glad I could help. Thanks for the Rep.

+ 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. How to create a macro that will choose the last sheet on the right and...
    By feejo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-27-2013, 03:10 PM
  2. Replies: 1
    Last Post: 05-17-2011, 08:44 PM
  3. Replies: 5
    Last Post: 07-08-2009, 01:12 PM
  4. Replies: 5
    Last Post: 03-12-2008, 05:01 PM
  5. Choose from List
    By NNothard in forum Excel General
    Replies: 4
    Last Post: 10-09-2006, 11:53 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