+ Reply to Thread
Results 1 to 7 of 7

Macro for Dynamic Ranges

Hybrid View

  1. #1
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Macro for Dynamic Ranges

    I end up making dynamic ranges in most workbooks, and tire of typing then in. The code below allows you to select a range and it makes dynamic ranges using the header row, for numbers or text, according to the contents of the cell below the header.

           --B--- C --D---
       2   valNam   valNum
       3   Abel          5
       4   Bill          5
       5   Cain         34
       6   Drew         38
       7   Eric         74
       8   Fred         76
       9   Gary           
      10   Hank           
      11   Ivan
    Here 'tis, hope you find it useful.
    Sub MakeDynRanges()
        ' shg 2011-06
    
        ' Adds dynamic ranges using the names in the top row of the selection
        Dim r           As Range
        Dim cell        As Range
    
        If Not TypeOf Selection Is Range Then
            MsgBox Prompt:="Select a range and try again.", _
                   Title:="Oops!"
            Exit Sub
        End If
    
        ActiveWorkbook.Names.Add Name:="conBig", RefersTo:=9.99999999999999E+307
        ActiveWorkbook.Names.Add Name:="conZzz", RefersTo:="=rept(""z"", 255)"
        
        Set r = Selection
    
        For Each cell In Intersect(r.Rows(1).EntireRow, r.EntireColumn).Cells
            If Len(cell.Text) Then
                MakeDynRange cell, VarType(cell.Offset(1).Value2)
            End If
        Next cell
    End Sub
    
    Function MakeDynRange(cell As Range, iType As VbVarType) As Boolean
        ' shg 2011-06
        Dim sName       As String
        Dim sAdrCel     As String
        Dim sAdrCol     As String
    
        With cell.Worksheet
            sName = Replace(Replace(cell.Text, " ", ""), Chr(160), "")
            sAdrCel = cell.Address
            sAdrCol = cell.EntireColumn.Address
    
            If IsValidRangeName(sName) Then
                Select Case iType
                    Case vbDouble
                        .Names.Add _
                                Name:=sName, _
                                RefersTo:="=index(" & sAdrCol & ", row(" & sAdrCel & ") + 1):" & _
                                          "index(" & sAdrCol & ", match(conBig, " & sAdrCol & "))"
                        MakeDynRange = True
                    
                    Case vbString
                        .Names.Add _
                                Name:=sName, _
                                RefersTo:="=index(" & sAdrCol & ", row(" & sAdrCel & ") + 1):" & _
                                          "index(" & sAdrCol & ", match(conZzz, " & sAdrCol & "))"
                        MakeDynRange = True
                    
                    Case Else
                        Select Case InputBox(Prompt:="Data below cell " & cell.Address & " will be: " & vbLf & vbLf & _
                                                     "1. Numbers" & vbLf & _
                                                     "2. Text", _
                                             Title:="Dynamic Column Ranges")
                            Case "1"
                                MakeDynRange = MakeDynRange(cell, vbDouble)
                            Case "2"
                                MakeDynRange = MakeDynRange(cell, vbString)
                            Case Else
                                Exit Function
                        End Select
                End Select
            Else
                MsgBox Prompt:="""" & sName & """ in " & _
                               cell.Address(False, False) & _
                               " is not a valid name.", _
                       Title:="Oops"
            End If
        End With
    End Function
    
    Function IsValidRangeName(sInp As String) As Boolean
        ' shg 2011-06
        
        Dim sTest As String
        
        With CreateObject("VBScript.RegExp")
            .IgnoreCase = True
            .Pattern = "^[A-Z_][\w]{0,254}$"
            If Not .test(sInp) Then Exit Function
        End With
        
        ' verify that the name doesn't look like an A1 or R1C1 address
        On Error Resume Next
        With Application
            sTest = LCase(.ConvertFormula(sInp, xlA1, xlR1C1, True))
            If Err.Number = 0 Then
                If sTest <> LCase(sInp) Then Exit Function
            Else
                Err.Clear
            End If
            
            sTest = LCase(.ConvertFormula(sInp, xlR1C1, xlA1, True))
            If Err.Number = 0 Then
                If sTest <> LCase(sInp) Then Exit Function
            Else
                Err.Clear
            End If
        End With
    
        IsValidRangeName = True
    End Function
    Last edited by shg; 09-28-2011 at 11:29 AM. Reason: delete spaces & NBS from names
    Entia non sunt multiplicanda sine necessitate

  2. #2
    Forum Contributor
    Join Date
    06-15-2011
    Location
    Chester, England
    MS-Off Ver
    Excel 2013
    Posts
    117

    Re: Macro for Dynamic Ranges

    Love this, it's a huge help. I can't figure out how to make the scope of the ranges cover the whole workbook - they are also greyed out when I try and edit them - is it possible?

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Macro for Dynamic Ranges

    I added a prompt to let the user select workbook or worksheet scope.
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    04-15-2008
    Location
    Tamil Nadu, India.
    MS-Off Ver
    Microsoft Office 2016
    Posts
    582

    Re: Macro for Dynamic Ranges

    Dear Sir,

    I am in need of such a macro. But the file extension is .bas

    How to apply in a common file and make use of the codes. Please guide us with a sample excel file.

    Please...
    Good friends are hard to find, harder to leave, and impossible to forget.

    acsishere.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Macro for Dynamic Ranges

    Put the bas file in a directory, open the VBE, and drag the file into the VBA project.

  6. #6
    Registered User
    Join Date
    04-15-2008
    Location
    Tamil Nadu, India.
    MS-Off Ver
    Microsoft Office 2016
    Posts
    582

    Re: Macro for Dynamic Ranges

    Dear Sir,

    Thank you for your guidance.

  7. #7
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019
    Posts
    436

    Re: Macro for Dynamic Ranges

    This macro is very cool. Well respected

+ 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