+ Reply to Thread
Results 1 to 7 of 7

How can I use the .value code?

Hybrid View

  1. #1
    Registered User
    Join Date
    11-12-2007
    Posts
    30

    How can I use the .value code?

    Hi,
    I got the following bit of code from this forum a few days ago:
    Private Sub Worksheet_Calculate()
    Dim TheRange As Range
    Dim TheCell As Range
    
    Set TheRange = Range("H8", Range("AK11"))
    For Each TheCell In TheRange
    If TheCell.Value < 0 Then
                TheCell.Offset(-4, 0).Range("A1").Interior.ColorIndex = 3
            ElseIf TheCell.Value = 0 Then
                TheCell.Offset(-4, 0).Range("A1").Interior.ColorIndex = 6
            ElseIf TheCell.Value >= 1 Then
                TheCell.Offset(-4, 0).Range("A1").Interior.ColorIndex = 4
            End If
        
        Next TheCell
    End Sub
    It works beautifully. I was just wondering about the .value extension, as essentially I want to modify one of the following to check for the letter X, rather than a number

            ElseIf TheCell.Value >= 1 Then
                TheCell.Offset(-4, 0).Range("A1").Interior.ColorIndex = 4
    Can I use the .Value extension or do I need to use another tag? I tried using

    .value = X
    but it doesn't seem to work. What can I do? Is the >=1 bit the problem? If so, can I limit this to >=1 and <=99, and how can I do this?
    Last edited by cannon_lab; 12-20-2007 at 07:02 AM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523
    Quote Originally Posted by cannon_lab
    Hi,
    I got the following bit of code from this forum a few days ago:
    Private Sub Worksheet_Calculate()
    Dim TheRange As Range
    Dim TheCell As Range
    
    Set TheRange = Range("H8", Range("AK11"))
    For Each TheCell In TheRange
    If TheCell.Value < 0 Then
                TheCell.Offset(-4, 0).Range("A1").Interior.ColorIndex = 3
            ElseIf TheCell.Value = 0 Then
                TheCell.Offset(-4, 0).Range("A1").Interior.ColorIndex = 6
            ElseIf TheCell.Value >= 1 Then
                TheCell.Offset(-4, 0).Range("A1").Interior.ColorIndex = 4
            End If
        
        Next TheCell
    End Sub
    It works beautifully. I was just wondering about the .value extension, as essentially I want to modify one of the following to check for the letter X, rather than a number

            ElseIf TheCell.Value >= 1 Then
                TheCell.Offset(-4, 0).Range("A1").Interior.ColorIndex = 4
    Can I use the .Value extension or do I need to use another tag? I tried using

    .value = X
    but it doesn't seem to work. What can I do? Is the >=1 bit the problem? If so, can I limit this to >=1 and <=99, and how can I do this?
    if thecell="X" then
    should work

  3. #3
    Registered User
    Join Date
    11-12-2007
    Posts
    30
    What does .value mean specifically?

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523
    Quote Originally Posted by cannon_lab
    What does .value mean specifically?
    ? value
    specifically?
    http://en.wikipedia.org/wiki/Value

  5. #5
    Registered User
    Join Date
    11-12-2007
    Posts
    30
    Yeah that's really not helpful, if I wanted an answer from Wikipedia I would have gone on there.

    I mean what does it represent? - numbers, strings, letters
    How can it be used?

    I am new to VBA and most of the internet resources I have found on the subject are useless- they tell you how to achieve results, but not why it works.

  6. #6
    Registered User
    Join Date
    10-25-2006
    Posts
    54
    Hi

    Yeah - it's a bit of a minefield!...but good fun (honest!).

    .value is a property of a an object. Not all objects have the value property and the value is not always the same data type.

    In your example .value is the property of a range object so in its simplest form...

    fred=range("H8").value
    ...so fred is a carrier for whatever value is in cell "H8". fred can be anything you can like - it's a bag so that you can report on whatever is in cell "H8" so, later on, if you wanted to report on fred you could write

    msgbox (fred) which is the same as writing msgbox (range("H8").value)

    As a suggestion try and record some simple excercises in Excel using the Tools>Macro>Record tool. Then go and look at what you've recorded in the Visual Basic editor. The simpler you make them the easier it will be to understand the example.

    Hope that's of some help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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