Consulting

Results 1 to 6 of 6

Thread: Solved: variable not defined in

  1. #1
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location

    Solved: variable not defined in

    I'm getting the error at x. How do I define this to go through each cell in the range?
    [VBA]Option Explicit
    Sub dumpts()
    Dim myRange As Object


    'Dim yrend As Integer
    'yrend = 7

    'Do

    ' Sheets("P200" & yrend).Select

    Set myRange = Application.InputBox("Range?", "Range?", Type:=8)

    For Each x In myRange
    If (x.Value) = "T" Then
    x.Select
    Selection = "0"

    End If
    'yrend = yrend + 1

    'Loop Until yrend = 9

    Next x

    End Sub[/VBA]

  2. #2
    VBAX Regular LinkND's Avatar
    Joined
    May 2008
    Location
    Rotterdam
    Posts
    29
    Location
    Quote Originally Posted by grichey
    I'm getting the error at x. How do I define this to go through each cell in the range?
    [vba]Option Explicit
    Sub dumpts()
    Dim myRange As Object


    'Dim yrend As Integer
    'yrend = 7

    'Do

    ' Sheets("P200" & yrend).Select

    Set myRange = Application.InputBox("Range?", "Range?", Type:=8)

    For Each x In myRange
    If (x.Value) = "T" Then
    x.Select
    Selection = "0"

    End If
    'yrend = yrend + 1

    'Loop Until yrend = 9

    Next x

    End Sub[/vba]
    [vba]Dim x As Long[/vba]

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You must dim it as variant or as an object.......I think variant in this case. If you just use dim x it will be dimmed as variant.
    [VBA]Option Explicit
    Sub dumpts()
    Dim myRange As Object
    Dim x


    'Dim yrend As Integer
    'yrend = 7

    'Do

    ' Sheets("P200" & yrend).Select

    Set myRange = Application.InputBox("Range?", "Range?", Type:=8)

    For Each x In myRange
    If (x.Value) = "T" Then
    x.Select
    Selection = "0"

    End If
    'yrend = yrend + 1

    'Loop Until yrend = 9

    Next x

    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    LinkND,
    You must not have tried this. Long will not work:
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    yeah i dim 'd as object and it worked fine.
    Thanks!

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    They are ranges, so why not Dim as ranges. I would go with
    [vba]
    Option Explicit
    Sub dumpts()
    Dim myRange As Range
    Dim x As Range
    Set myRange = Application.InputBox("Range?", "Range?", Type:=8)
    For Each x In myRange
    If x = "T" Then x = "0"
    Next x
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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