+ Reply to Thread
Results 1 to 11 of 11

runtime error 1004 vb

Hybrid View

  1. #1
    Registered User
    Join Date
    05-23-2013
    Location
    cambridge
    MS-Off Ver
    Excel 2003
    Posts
    12

    runtime error 1004 vb

    Hi

    Can anyone help me resolve an issue with the code below. I am getting a runtime error 1004, i highlighted the problem in the code. i am running excel 2007
    i appreciate any help

    Sub setNames(theTopLeft As Range)
    Dim theName As Name
    Dim nameStr As String
    Dim theRng As Range
    Dim i As Long
        Application.DisplayAlerts = False
        theTopLeft.CurrentRegion.CreateNames Top:=True, Left:=False, _
                        Bottom:=False, Right:=False
        Application.DisplayAlerts = True
        For Each theName In ThisWorkbook.Names
            With theName.RefersToRange
                For i = .Cells.Count To 1 Step -1
                    If .Cells(i) <> "" Then Exit For
                Next
            End With
            If i <> 0 Then theName.RefersTo = theName.RefersToRange.Resize(i, 1)
        Next
    End Sub

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: runtime error 1004 vb

    The syntax works if the range contains valid names and data in the current region

    Can you create the names manually using the same data?
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    05-23-2013
    Location
    cambridge
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: runtime error 1004 vb

    Andy,

    Thank you for replying, i tired to point it to the sheet, folder and range name, but it did not like RefersToRange and the .cells(i)..
    I am using this code in the module, it is used with a main() to populate a combobox with one of 3 named ranges depending on a cell value. The main() an setNmaes are on the module, and the combobox and the named ranges are in sheet1.
    Can you kindly help?

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: runtime error 1004 vb

    None of that makes sense, to me, in regards to the questions I asked.

    post example file

  5. #5
    Registered User
    Join Date
    05-23-2013
    Location
    cambridge
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: runtime error 1004 vb

    Quote Originally Posted by Andy Pope View Post
    None of that makes sense, to me, in regards to the questions I asked.

    post example file
    I have attached an example, thank you for your time much appreciated
    Attached Files Attached Files

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: runtime error 1004 vb

    Sub MAIN()
    Dim PT As Range
    Dim i As Long
        With ActiveSheet
            setNames .Range("a6")
            Set PT = .Range("b1")
            i = 1
            Do Until PT = ""
                If .Range("a1").Value = PT.Value Then
                    On Error Resume Next
                    Sheet1.ComboBox1.ListFillRange = ThisWorkbook.Names("view" & i).Name
                    If Err.Number = 1004 Then
                        MsgBox "not defined name: view" & i
                    ElseIf Err.Number <> 0 Then
                        MsgBox "unexpected error: " & Err.Description
                    End If
                    On Error GoTo 0
                End If
                i = i + 1
                Set PT = PT.Offset(0, 1)
            Loop
        End With
    End Sub

    I think the problem with the code was the reference to combobox1 , which was not on the active sheet.
    And the setting the ListFillRange range to the cell address rather than the name which had been created. With only the cells part it would have to relative to the sheet with the control on.

  7. #7
    Registered User
    Join Date
    05-23-2013
    Location
    cambridge
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: runtime error 1004 vb

    Thanks Andy, i agree i missed that.
    I tried your code, but i am still getting a runtime error 1004
    this selection is not valid
    theTopLeft.CurrentRegion.CreateNames Top:=True, Left:=False, _
    Bottom:=False, Right:=False
    i suspect it is the refertorange not working, maybe i need an addin?
    I am a novice, so i don't know what i am doing yet
    Please help

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: runtime error 1004 vb

    So you did not bother to read the comment in the code?

    ' remaining code remove for brevity


    Here is the full code
    Sub MAIN()
    Dim PT As Range
    Dim i As Long
        With Sheet2  ' Unique SPP
            setNames .Range("a6")
            Set PT = .Range("b1")
            i = 1
            Do Until PT = ""
                If .Range("a1").Value = PT.Value Then
                    On Error Resume Next
                    Sheet1.ComboBox1.ListFillRange = ThisWorkbook.Names("view" & i).Name
                    If Err.Number = 1004 Then
                        MsgBox "not defined name: view" & i
                    ElseIf Err.Number <> 0 Then
                        MsgBox "unexpected error: " & Err.Description
                    End If
                    On Error GoTo 0
                End If
                i = i + 1
                Set PT = PT.Offset(0, 1)
            Loop
        End With
    End Sub

  9. #9
    Registered User
    Join Date
    05-23-2013
    Location
    cambridge
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: runtime error 1004 vb

    Sorry, i misunderstood. (i read the comment, i just didn't understand it)
    setNames is not defined in the Main()?

    Please excuse my ignorance, but i am trying

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: runtime error 1004 vb

    If you are trying to run this from the command button on sheet3 then you will need to change the reference to Activesheet.

    Sub MAIN()
    Dim PT As Range
    Dim i As Long
        With Sheet2  ' Unique SPP
            setNames .Range("a6")
            Set PT = .Range("b1")
    '
    ' remaining code remove for brevity
    '

  11. #11
    Registered User
    Join Date
    05-23-2013
    Location
    cambridge
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: runtime error 1004 vb

    Thank you Andy,
    I am getting Error 1004 application-define or object define error from this: With theName.RefersToRange
    this is what i have on the module after the changes you recommended
    I think i am getting close, thanks to you. I hope you can help me figure it out

    Sub MAIN()
    Dim PT As Range
    Dim i As Long
        With Sheet2  ' Unique SPP
            setNames .Range("a6")
            Set PT = .Range("b1")
    End With
    End Sub
    Sub setNames(theTopLeft As Range)
    Dim theName As Name
    Dim nameStr As String
    Dim theRng As Range
    Dim i As Long
        Application.DisplayAlerts = False
        theTopLeft.CurrentRegion.CreateNames Top:=True, Left:=False, _
                        Bottom:=False, Right:=False
        Application.DisplayAlerts = True
        For Each theName In ThisWorkbook.Names
            With theName.RefersToRange
                For i = .Cells.Count To 1 Step -1
                   
            If .Cells(i) <> "" Then Exit For
                Next
            End With
            If i <> 0 Then theName.RefersTo = theName.RefersToRange.Resize(i, 1)
        
        Next
        
        
    End Sub

+ 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. [SOLVED] VBA Error: Runtime Error 1004: AutoFilter method of Range class failed
    By jl22stac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-03-2013, 07:27 PM
  2. [SOLVED] Range error in code, runs alone but not inside my full program, giving runtime error 1004
    By charizzardd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-23-2012, 03:34 PM
  3. runtime error 1004
    By crizzle in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-29-2008, 12:54 PM
  4. runtime error 1004
    By valdesd in forum Excel General
    Replies: 2
    Last Post: 10-12-2005, 03:05 PM
  5. Excel 2003 Macro Error - Runtime error 1004
    By Cow in forum Excel General
    Replies: 2
    Last Post: 06-07-2005, 09:05 AM

Tags for this Thread

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