Check if a range has data in condition to a cell

girapas

Board Regular
Joined
Apr 20, 2004
Messages
150
In a sheet a user enters data in a range, say A1 to F5. In column A the user selects an item from a named list (via Data\Validation\List) with text values. Then, in columns B to F the user enters numbers. In any row he may enters 1,2,3,4,5 numbers or none. When he finishes he clicks a button with name 'Confirm' that saves the data and makes some calculations.
What I want is a macro to run after button click that:
1) If the user selects a value in A and doesn't enter number in any of the cells that are in the same row (I mean if he leaves a row only with data in A, without at least one number in adjacent cells of columns B to F), a stop explaining message to be appeared and prompt him to correct it.
2) If the user enters number or numbers in any cell of the range B1 to F5 and omits to select an item in A, again a stop message to be appeared.

Notice that in all the cells there's Conditional Formatting (fonts, patterns etc.) with all 3 conditions (Excel 2003).

Thanks in advance...
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
See if this works for you:-
Code:
Option Explicit
 
Sub CheckRange()
 
  Dim dRange As Range
  Dim sRow As Long
  Dim eRow As Long
  Dim sCol As Long
  Dim eCol As Long
 
  Dim iPtr As Long
  Dim iCell As Long
  Dim bEmpty As Boolean
  Dim bWarning As Boolean
  Dim iErrors As Long
  Dim sMessage As String
 
  Set dRange = Range("[COLOR=red][B]A1:F5[/B][/COLOR]")
  sRow = dRange.Row
  eRow = sRow + dRange.Rows.Count - 1
  sCol = dRange.Column
  eCol = sCol + dRange.Columns.Count - 1
 
  bWarning = False
  sMessage = ""
  iErrors = 0
  For iPtr = sRow To eRow
    bEmpty = True
    For iCell = sCol + 1 To eCol
      If Not IsEmpty(Cells(iPtr, iCell)) Then
        bEmpty = False
        Exit For
      End If
    Next iCell
    Select Case True
      Case IsEmpty(Cells(iPtr, sCol)) And Not bEmpty
        bWarning = True
        iErrors = iErrors + 1
        sMessage = sMessage & ", " & CStr(iPtr)
      Case Not IsEmpty(Cells(iPtr, sCol)) And bEmpty
        bWarning = True
        iErrors = iErrors + 1
        sMessage = sMessage & ", " & CStr(iPtr)
    End Select
  Next iPtr
 
  If bWarning = True Then
    MsgBox "Error" & IIf(iErrors > 1, "s", "") _
       & " detected in row" & IIf(iErrors > 1, "s", "") _
       & ": " & Mid(sMessage, 3) & Space(10), vbOKOnly + vbExclamation
  Else
    MsgBox "No errors detected" & Space(10), vbOKOnly + vbInformation
  End If
 
End Sub
Just change the bit in red to suit.
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,047
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top