Consulting

Results 1 to 2 of 2

Thread: VBA Named Ranges inc Sheet title

  1. #1

    VBA Named Ranges inc Sheet title

    Hi,

    I'm not sure if this has been covered but I'm looking everywhere for a short piece of VBA code to create named ranges from the columns off a sheet and include the sheet name as a prefix for the named range.

    I've managed to find the following:

       Dim wbk As Workbook
        Dim Sht As Worksheet
        Dim rng6, rng7 As Range
        Dim cl As Object
        Dim c As Long
        Dim strAddr As Variant
        Dim strShName, strHdrName, strCol As String
        
        Set wbk = ActiveWorkbook
       Set Sht = Sheets("Raw")
            c = Sht.Cells.SpecialCells(xlLastCell).Column
            Set rng6 = Sht.Range("A1", Sht.Range("A1").Offset(0, c))
            For Each cl In rng6
                If cl.Value <> "" Then
                    strShName = Replace(Sht.Name, " ", "_", 1)
                    strHdrName = Replace(cl.Value, " ", "_", 1)
                    strAddr = Split(cl.Address, "$")
                    strCol = "$" & strAddr(1) & ":$" & strAddr(1)
                    Set rng7 = Sht.Range(cl, cl.End(xlDown))
                    ActiveWorkbook.Names.Add Name:=strShName & strHdrName, _
                        RefersTo:="=OFFSET('" & Sht.Name & "'!" & cl.Address & ",0,0,COUNTA('" & _
                        Sht.Name & "'!" & strCol & "),1)"
                End If
            Next cl


    But if there are blank cells in a column it seems to shorten the range in the column (I suspect this has to do with the COUNTA offset).

    The last row for each of the ranges should be determined from the last row in column A.

    i.e.
    Dim lastrow As Long
    lastrow = Range("A655636").End(xlUp).Row
    Any assistance would be greatly appreciated.
    Last edited by Bob Phillips; 06-26-2017 at 02:02 AM. Reason: Added code tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Const FORMULA_REFERSTO As String = _
        "=OFFSET('<sheet>'!<cell>,0,0,<height>,1)"
    Dim wbk As Workbook
    Dim Sht As Worksheet
    Dim rng6, rng7 As Range
    Dim cl As Object
    Dim c As Long, lastrow As Long
    Dim strAddr As Variant
    Dim strShName, strHdrName, strCol As String
     
        Set wbk = ActiveWorkbook
        Set Sht = wbk.Worksheets("Raw")
        c = Sht.Cells.SpecialCells(xlLastCell).Column
        Set rng6 = Sht.Range("A1", Sht.Range("A1").Offset(0, c))
        For Each cl In rng6
        
            If cl.Value <> "" Then
            
                strShName = Replace(Sht.Name, " ", "_", 1)
                strHdrName = Replace(cl.Value, " ", "_", 1)
                lastrow = Sht.Cells(Sht.Rows.Count, cl.Column).End(xlUp).Row
                Set rng7 = Sht.Range(cl, cl.End(xlDown))
                Sht.Names.Add Name:=strShName & strHdrName, _
                              RefersTo:=Replace(Replace(Replace(FORMULA_REFERSTO, "<sheet>", Sht.Name), "<cell>", cl.Address), "<height>", lastrow)
            End If
        Next cl
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •