Consulting

Results 1 to 2 of 2

Thread: Naming Ranges Macro Run Time Error 1004

  1. #1

    Naming Ranges Macro Run Time Error 1004

    Hi Guys, I hope you are all well.

    I am trying to use the below macro to create named ranges on sheet2 using the values in column A Sheet 1. It will also copy the values from Column B & C for each of the named ranges. rANGES.jpg and create a named range for every value in column A sheet 1 like the image attached.

    However Every Time I Try and run this macro i get a " Run Time Error 1004" Application Defined Or Object Defined Error

    I have removed all blanks spaces and anything that might cause an error and this still pops up.

    Sub TransposeRange_new_code()    Dim OutRange              As Range
        Dim x As Long, y As Long
        Dim sKey As String
        Dim maxCount As Long
        Dim data, dic, keys, items, dataout()
    
    
        Application.ScreenUpdating = False
        data = Sheet1.Range("A2:E" & Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row).Value2
    
    
        Set dic = CreateObject("scripting.dictionary")
        Set OutRange = Sheet2.Range("B2")
    
    
        For x = 1 To UBound(data, 1)
            If Trim$(data(x, 1)) <> "_" Then
            sKey = Trim$(data(x, 1)) & Chr(0) & Trim$(data(x, 2))
            If Not dic.exists(sKey) Then dic.Add sKey, CreateObject("Scripting.Dictionary")
            dic(sKey).Add x, Array(data(x, 4), data(x, 5))
            If dic(sKey).Count > maxCount Then maxCount = dic(sKey).Count
            End If
        Next
    
    
        ReDim dataout(1 To maxCount + 1, 1 To dic.Count * 3)
        keys = dic.keys
        items = dic.items
        For x = LBound(keys) To UBound(keys)
            dataout(1, x * 3 + 1) = Split(keys(x), Chr(0))(0)
            dataout(1, x * 3 + 2) = Split(keys(x), Chr(0))(1)
            For y = 1 To items(x).Count
                dataout(1 + y, x * 3 + 1) = items(x).items()(y - 1)(0)
                dataout(1 + y, x * 3 + 2) = items(x).items()(y - 1)(1)
            Next y
        Next
    
    
    
        "OutRange.Resize(UBound(dataout, 1), UBound(dataout, 2)).Value2 = dataout"
    
    
        For x = 1 To UBound(keys)
            OutRange.Offset(0, (x - 1) * 3).Resize(maxCount, 2).Name = "" & validName(Split(keys(x - 1), Chr(0))(0))
            With OutRange.Offset(0, (x - 1) * 3 + 1)
                .Hyperlinks.Add anchor:=.Cells(1), Address:="mailto://" & .Value2, TextToDisplay:=.Value2
            End With
        Next
    
    
    End Sub
    Function validName(ByVal sText As String) As String
        Dim n As Long
        Dim bChars() As Byte
        bChars = sText
        If UCase$(sText) Like "*[!A-Z0-9._]*" Then
            For n = 0 To UBound(bChars) - 1 Step 2
                Select Case bChars(n)
                    Case 46, 48 To 57, 65 To 90, 95
                        ' valid character: _ . number or letter
                    Case Else
                        ' convert to underscore
                        bChars(n) = 95
                End Select
            Next n
        End If
        validName = bChars
    End Function
    any help as always would be greatly appreciated,

    many thanks and all the best

    Jamie

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I don't get an error. Can you post the workbook?
    ____________________________________________
    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
  •