+ Reply to Thread
Results 1 to 10 of 10

checking whether a number of cells have values

Hybrid View

  1. #1
    Registered User
    Join Date
    07-21-2010
    Location
    quezon
    MS-Off Ver
    Excel 2007/2010
    Posts
    27

    checking whether a number of cells have values

    Hi, is there a way to use IF statement to check whether cells have values in them? I'm using this code which doesn't work

    If Worksheets("Main").Range("O5:O16, P5:P16").Range <> "" Then
    'code here
    end if
    The alternate i'm thinking of is individually checking each cell

    If Worksheets("Main").Range("O5").Range <> "" Then
    if worksheets("Main").range("O6").range<> "" then
    'code here
    end if
    end if
    but it will make the codes look messy.
    Last edited by LAazsx; 08-05-2010 at 12:42 PM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: checking whether a number of cells have values

    I don't know what you want to do if you find an empty cell but tinker with this
    Sub vCheck()
    For Each c In Range("O5:O16, P5:P16")
        If c.Value <> "" Then c.Select
    Next c
    End Sub
    For the "c.Select" part of the code is where you put your own code.
    Last edited by Mordred; 08-05-2010 at 12:13 PM.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    07-21-2010
    Location
    quezon
    MS-Off Ver
    Excel 2007/2010
    Posts
    27

    Re: checking whether a number of cells have values

    Sorry for not specifying. I want is that excel checks the ranges ("O5:O16, P5:P16") for values. If one cell has a value, it executes a code (only once for all the cells). If there is none, it will do nothing

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: checking whether a number of cells have values

    Not know what code you want to execute depending on value makes this a little difficult to understand but try this code but replace the c.select with your executable code.

    Sub vCheck()
    For Each c In Range("O5:O16, P5:P16")
        If c.Value <> "" Then c.Select
        If c.Value = "" Then c.Select
    Next c
    End Sub
    Sorry if this doesn't help but it works for my understanding.

  5. #5
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: checking whether a number of cells have values

    If c.Value <> "" Then c.Select
    runs through and finds the cells that have values. If you change it to:
    If c.Value <> "" Then c.Copy
    it will copy the cell with a value
    if the cell has no value,
    If c.Value = "" Then c.Select
    the code will skip past the empty cell.

  6. #6
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: checking whether a number of cells have values

    Try this
        If Application.CountA(Sheets("Main").Range("O5:P16")) > 0 Then
            'code here
        End If
    This will run your code if there is any value at all in Range("O5:P16")

    Hope this helps
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  7. #7
    Registered User
    Join Date
    07-21-2010
    Location
    quezon
    MS-Off Ver
    Excel 2007/2010
    Posts
    27

    Re: checking whether a number of cells have values

    Thanks! Tried editing the code but an error comes out. It says that there is no object defined. I think C was not declared? anyways here's my code

    For Each c In Range("O5:O16, P5:P16")
    Next c
    If c.Value = "" Then c.Select
    If c.Value <> "" Then
    saveRecords
    ActiveWorkbook.Save
    End If

  8. #8
    Registered User
    Join Date
    07-21-2010
    Location
    quezon
    MS-Off Ver
    Excel 2007/2010
    Posts
    27

    Re: checking whether a number of cells have values

    Thanks! It works fine!

  9. #9
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: checking whether a number of cells have values

    If you just want to save the workbook if there is a value in any cell in the range O5:P16 and the workbook is already saved

    Then
        If Application.CountA(Sheets("Main").Range("O5:P16")) > 0 Then
            ActiveWorkbook.Save
        End If

    No loop required

    Hope this helps

    [EDIT]

    Posts may have crossed

    What works fine?

  10. #10
    Registered User
    Join Date
    07-21-2010
    Location
    quezon
    MS-Off Ver
    Excel 2007/2010
    Posts
    27

    Re: checking whether a number of cells have values

    yeah. thanks

+ 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