+ Reply to Thread
Results 1 to 5 of 5

Code for Multiple Named Ranges for Data Validation

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Talking Code for Multiple Named Ranges for Data Validation

    Hello!

    Dave Hawley helped another user out who wanted to concatenate two named ranges and use that new BigList as a range for data validation.

    I have a named range of Shop personnel and then another for Office.

    In this project, I want to be able to choose Shop + Office.

    The named ranges are on the Lists sheet using this formula: =OFFSET(Lists!$A$2,0,0,COUNTIF(Lists!$A$2:$A$500,">"""),1)

    How can I apply this code to my workbook? It has the BigList being created on Sheet4, but I am already using Sheets 1-21. Is there a way this code to be modified to create the BigList on a hidden Sheet1000 or something like that?

    Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
        With Sheet4 
                .Columns(1).Clear ' Clear MyBigList ready for copy/paste
                Sheets("Lists").Range("Shop").Copy .Range("A1")  'Copy 1st dynamic range and paste to A1 of Sheet4
                Sheets("Lists").Range("Office").Copy .Cells(.Rows.Count, "A").End(xlUp)(2, 1)  'Copy 2nd dynamic range and paste to 1 below last used row of Sheet4
                .Range("A1", .Cells(.Rows.Count, "A").End(xlUp)).Name = "MyBigList" 'Name the new dynamic range
        End With
    End Sub
    I appreciate the help!

    Respectfully,

    Lost
    Last edited by leaning; 06-07-2011 at 02:55 PM. Reason: correct wording

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Code for Multiple Named Ranges for Data Validation

    Lost,

    This code goes through each worksheet to see if there is a sheet named "Big Combined List". If it finds one, it uses that sheet. If it does not find one, it creates that sheet for future use and hides it:
    Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
        
        Dim wsBigList As Worksheet, ws As Worksheet
        Dim wsBigListFound As Boolean: wsBigListFound = False
        For Each ws In ActiveWorkbook.Sheets
            If ws.Name = "Big Combined List" Then
                Set wsBigList = ws
                wsBigListFound = True
                Exit For
            End If
        Next ws
        
        If wsBigListFound = False Then
            Set wsBigList = Sheets.Add(after:=Sheets(Sheets.Count))
            wsBigList.Name = "Big Combined List"
            wsBigList.Visible = xlSheetHidden
        End If
        
        With wsBigList
                .Columns(1).Clear ' Clear MyBigList ready for copy/paste
                Sheets("Lists").Range("Shop").Copy .Range("A1")  'Copy 1st dynamic range and paste to A1 of Sheet4
                Sheets("Lists").Range("Office").Copy .Cells(.Rows.Count, "A").End(xlUp)(2, 1)  'Copy 2nd dynamic range and paste to 1 below last used row of Sheet4
                .Range("A1", .Cells(.Rows.Count, "A").End(xlUp)).Name = "MyBigList" 'Name the new dynamic range
        End With
        
    End Sub


    Hope that helps,
    ~tigeravatar

  3. #3
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Re: Code for Multiple Named Ranges for Data Validation

    TigerAvatar,

    I put your code in a test workbook, but it doesn't seem to be doing anything. From what I can see, it isn't making the Big Combined List worksheet.

    Is there something I am doing wrong?

    Respectfully,

    Lost
    Attached Files Attached Files

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Code for Multiple Named Ranges for Data Validation

    Lost,

    The code only runs when the workbook performs a calcuation, so there'd have to be a cell that has a formula in it, otherwise the workbook is just changing, not calculcating. To run the code manually, change the sub name and put it in a standard module, then run it. Additionally, this line of code:
            wsBigList.Visible = xlSheetHidden


    Makes the worksheet hidden. You can view it by right-clicking any worksheet tab and selecting Unhide.

    Hope that helps,
    ~tigeravatar

  5. #5
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Re: Code for Multiple Named Ranges for Data Validation

    TigerAvatar,

    Got it.

    Incidentally, in the Test3 file posted earlier, I put =Now() (to force a recalculation) in one of the cells. I closed the workbook and then reopened it. The whole thing was flickering and I had to CTRL-ALT-DEL to get it to stop.

    So, I put a button on it. It is an extra step the user has to do, but they still don't have to manually enter names in that cell.

    If you don't see a better way to go, I think this thread is solved.

    Thanks for your help!

    Respectfully,

    Lost
    Attached Files Attached Files
    Last edited by leaning; 06-08-2011 at 12:58 PM. Reason: change wording

+ 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