+ Reply to Thread
Results 1 to 5 of 5

If statement doesn't work for range?

Hybrid View

  1. #1
    Registered User
    Join Date
    01-09-2007
    Posts
    54

    If statement doesn't work for range?

    I was wondering if anyone could tell me why this code doesn't work? I'm just playing around with some codes to see what can work.

    Private Sub CommandButton1_Click()
    myRng = Range("a1", Range("a1").End(xlDown)).value
    If myRng = 2 Then
    MsgBox ("yes")
    End If
    End SubThe following code does work

    Range("a1", Range("a1").End(xlDown)).value = 2

    This sets the whole range of cells to 2, so why can't you call an IF statement to see what the value is?

    Alex
    Last edited by deeppurple247; 02-07-2007 at 05:47 PM.

  2. #2
    Registered User
    Join Date
    01-09-2007
    Posts
    54

    if statement doesn't work for range?

    I was wondering if anyone could tell me why this code doesn't work? I'm just playing around with some codes to see what can work.

    Private Sub CommandButton1_Click()
    myRng = Range("a1", Range("a1").End(xlDown)).value
    If myRng = 2 Then
    MsgBox ("yes")
    End If
    End Sub
    The following code does work

    Range("a1", Range("a1").End(xlDown)).value = 2
    This sets the whole range of cells to 2, so why can't you call an IF statement to see what the value is?

    Alex

  3. #3
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565
    Hi Alex,

    Sorry, do you want to know the value of the last cell in your range variable or something else?

    Cheers,

    Robert

  4. #4
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    If you are trying to obtain the value of the sum of your range, then try:
    myRng = Application.WorksheetFunction.Sum(Range("A1", Range("A1").End(xlDown)))
    If you are trying to obtain the value of the last cell in the range, then try:
    myRng = Range("A1").End(xlDown).Value
    HTH

    Jason
    Last edited by jasoncw; 02-07-2007 at 06:53 PM.

  5. #5
    Registered User
    Join Date
    01-18-2007
    Posts
    21
    you have to check each cell individually


    for i = 1 to lastCell(define this)
    activecell.offset(1,0)
    if activecell.value = 2 then
    msgBox("Yes")
    else
    msgBox("NO")
    end if

+ 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