Consulting

Results 1 to 8 of 8

Thread: Solved: Why is this not working?

  1. #1

    Question Solved: Why is this not working?

    I am new to vba and creating a macro to determine whether there are no, 1 or 2 or more used cells in column A. I have done the following but can't understand why it is working if there is 1 used cell. If there is an easier way that would be good, but I would also like to know why this doesn't work

    [VBA]If IsEmpty(Cells(1, 1)) Then
    MsgBox "No cells"
    End If
    If (IsEmpty(Cells(2, 1)) And IsEmpty(Cells(1, 1) = False)) Then
    MsgBox "1 cell"
    End If
    If (IsEmpty(Cells(1, 1)) = False And IsEmpty(Cells(2, 1)) = False) Then
    MsgBox "2 or more cells"
    End If[/VBA]
    Muchas gracias everyone !!

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Well, your structure isn't exactly correct for one and if statements get executed and exit the sub sometimes if not stuctured correctly.

    This shows how to check for false in your if statement:
    [vba]If IsEmpty(Cells(1, 1)) Then
    MsgBox "No cells"
    End If
    If IsEmpty(Cells(2, 1)) And Not IsEmpty(Cells(1, 1)) Then
    MsgBox "1 cell"[/vba]

    To check a column though, I would use a worksheet function:
    [vba]Sub CountNonBlankCells()
    Dim CellCount As Long
    CellCount = Application.Evaluate("=SUMPRODUCT(--(LEN(A1:A100)>0))")
    MsgBox CellCount & " non blank cells", vbOKOnly
    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

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You could also use Application.CountA
    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'

  4. #4
    Cheers lucas good work!

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    As Malcolm points out, this doesn't have to be done with code. You can easily use a formula on the sheet.

    If you're happy, mark your thread solved using the thread tools at the top of the page please.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    Quote Originally Posted by lucas
    As Malcolm points out, this doesn't have to be done with code. You can easily use a formula on the sheet.

    If you're happy, mark your thread solved using the thread tools at the top of the page please.
    Sure. Just to let you know the thread tools don't work in Google chrome, because the drop down arrow doesn't appear. I used IE to mark this solved. Although I suppose that's not an issue for 99.9% of forum viewers!

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Not even if you click on "Thread Tools"?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  8. #8
    Quote Originally Posted by Aussiebear
    Not even if you click on "Thread Tools"?
    No Google chrome doesn't seem to recognise the thread tools text as a drop down menu. The triangular button doesn't appear in google chrome. If you click on thread tools it directs you to the thread tools at the bottom of the page

Posting Permissions

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