+ Reply to Thread
Results 1 to 8 of 8

Check if current (active) cell is empty

Hybrid View

  1. #1
    Registered User
    Join Date
    08-11-2015
    Location
    NH, USA
    MS-Off Ver
    2010
    Posts
    4

    Check if current (active) cell is empty

    This macro looks for the last non-empty cell in column C, then selects the cell to left of it (column B).
    I wanted the macro to then check if this neighboring cell is empty or not and display a message box if True, however when I run it the box never shows up.

    When I replace the X in IsEmpty(X) to a regular cell location like IsEmpty(B22) it works fine. Reason why it has to be non-fix location is because the rest of the macro I'm making is combining rows from many workbooks into a master workbook so the last row location keeps moving down.

    Thanks for the help.

    Sub Sample()

    Range("C1").Select
    Selection.End(xlDown).Select
    X = ActiveCell.Offset(0, -1).Select

    ' Test if the value in neighboring cell is blank/empty
    If IsEmpty(X) = True Then
    MsgBox "Cell is empty"
    End If

    End Sub
    Last edited by Lycan004; 08-20-2015 at 04:33 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,069

    Re: Check if current (active) cell is empty

    Option Explicit
    
    Sub sCheckColumnB()
    
    With Range("C" & Rows.Count).End(xlUp).Offset(0, -1)
        MsgBox .Value
        If .Value <> "" Then
            MsgBox "The value is " & .Value
        Else
            MsgBox "The cell is empty"
        End If
    End With
    
    End Sub

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    08-11-2015
    Location
    NH, USA
    MS-Off Ver
    2010
    Posts
    4

    Re: Check if current (active) cell is empty

    Quote Originally Posted by TMS View Post
    Option Explicit
    
    Sub sCheckColumnB()
    
    With Range("C" & Rows.Count).End(xlUp).Offset(0, -1)
        MsgBox .Value
        If .Value <> "" Then
            MsgBox "The value is " & .Value
        Else
            MsgBox "The cell is empty"
        End If
    End With
    
    End Sub

    Regards, TMS
    This worked just right, thank you.
    Last edited by Lycan004; 08-21-2015 at 09:03 AM.

  4. #4
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Check if current (active) cell is empty

    IsEmpty doesn't check what you think it does, but instead merely verifies if a given variable has been initialized.

    Instead, check for an empty string.

    Sub Lateralus()
    
    Range("C1").Select
    Selection.End(xlDown).Select
    X = ActiveCell.Offset(0, -1).Select
    
    ' Test if the value in neighboring cell is blank/empty
    If x = "" Then MsgBox "Cell is empty"
    
    End Sub
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,069

    Re: Check if current (active) cell is empty

    Which solution are you referring to?

  6. #6
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Check if current (active) cell is empty

    Point TMS!

    Better luck next time daff!

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,069

    Re: Check if current (active) cell is empty

    @daffodil: you'd really need to go for this (without the third Select)

    Sub Lateralus()
    
    Range("C1").Select
    Selection.End(xlDown).Select
    x = ActiveCell.Offset(0, -1)
    
    ' Test if the value in neighboring cell is blank/empty
    If x = "" Then MsgBox "Cell is empty"
    
    End Sub
    And the OP's original code would have worked too, without that third Select.

    Sub Sample_TMS1()
    
    Range("C1").Select
    Selection.End(xlDown).Select
    x = ActiveCell.Offset(0, -1)
    
    ' Test if the value in neighboring cell is blank/empty
    If IsEmpty(x) = True Then
        MsgBox "v1: Cell " & ActiveCell.Offset(0, -1).Address & " is empty"
    End If
    
    End Sub
    
    ' or, making x a range:
    
    Sub Sample_TMS2()
    
    Range("C1").Select
    Selection.End(xlDown).Select
    Set x = ActiveCell.Offset(0, -1)
    
    ' Test if the value in neighboring cell is blank/empty
    If IsEmpty(x) = True Then
        MsgBox "v2: Cell " & x.Address & " is empty"
    End If
    
    End Sub

    Regards, TMS

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,069

    Re: Check if current (active) cell is empty

    Sorry daff, not intending to make it a competition. As the two approaches were a bit different, yours nearer to the original, I was interested to see what option the OP had gone for.

    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 12
    Last Post: 12-10-2013, 02:10 AM
  2. [SOLVED] copy and paste in current active cell, and need current date then scroll down 140 lines
    By vengatvj in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-04-2013, 03:40 AM
  3. VBA - Can I select a new active cell based on the value of the current active cell?
    By GrumpyOldBastard in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-08-2012, 07:05 PM
  4. Identify the Current Active Cell
    By staanley in forum Excel General
    Replies: 2
    Last Post: 02-08-2008, 05:34 AM
  5. Populate current active cell - Help Please!!
    By Larry in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 07-19-2006, 05:25 PM
  6. [SOLVED] Check current cell is empty
    By bobocat in forum Excel General
    Replies: 3
    Last Post: 07-06-2006, 06:20 PM
  7. Identify current active cell
    By reades in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-10-2006, 08:55 AM
  8. Check if Active Workbook is Empty
    By scantor145 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-26-2005, 11:05 AM

Tags for this Thread

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