Results 1 to 5 of 5
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Posts
    1,119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    User form (2000)

    I am trying to create a User Form and learn VBA at the same time. I have the following code:

    Sub HideMonths()
    '
    ' HideMonths Macro
    ' Macro recorded 03/25/2004 by Linda Rudge
    '

    '
    Dim myFind As Range
    Set myFind = Rows(1).Find(What:="5/1/2004", _
    After:=Range("A1"), _
    LookIn:=xlFormulas, _
    LookAt:=xlWhole, _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, _
    MatchCase:=False)
    Range("c1", myFind.Offset(0, -1)).EntireColumn.Hidden = True

    End Sub

    The problem is I keep getting a run-time error "91" - Object variable or With Block variable not set
    Debug highlights "Range("c1", myFind.Offset(0, -1)).EntireColumn.Hidden = True"

    What is wrong? Your help is appreciated.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: User form (2000)

    "c1" is not a range, but a string. Try replacing it with Range("C1"):

    Range(Range("C1"), myFind.Offset(0, -1)).EntireColumn.Hidden = True

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Posts
    1,119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: User form (2000)

    I still get the same error.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: User form (2000)

    Ah yes, you must also check if the search text was actually found. If not, the myFind object will be invalid. Try this:

    ...
    If Not myFind Is Nothing Then
    Range(Range("C1"), myFind.Offset(0, -1)).EntireColumn.Hidden = True
    End If
    End Sub

  5. #5
    5 Star Lounger
    Join Date
    Jan 2001
    Posts
    1,119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: User form (2000)

    It worked - my problem was the date format wasn't 3/1/2004 - it was Mar-04. I changed the date format (and used you code) and it worked. Thanks

Posting Permissions

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