Need to add stop the userform user from continuing if the adjacent cell in the first column is blank

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
572
Office Version
  1. 365
Platform
  1. Windows
Hi,

This is the other code that I was working on. I need to be able to stop this code if the cell directly to the left of the one that is found for the row I'm populating is blank. Maybe give the user a pop-up message that says "There are no more allocated Job Numbers available. Please have additional Job numbers allocated.". Then I want it to close the form when they select OK.

This code currently finds the first empty cell in column B and allows the user to enter the information that populates columns B, C & D. However, if there is no allocated number in column A, I want to stop the user from going further as explained above. Thanks in advance for any help on this.


VBA Code:
Private Sub CommandButton2_Click()


    Dim ws As Worksheet
    Dim LO As ListObject
    Dim Lastrow As Integer
    Dim C As Range

    Set LO = Sheet1.ListObjects("Table1")
    Set ws = ThisWorkbook.Sheets("Sheet1")


With LO.Range.Columns(2) 'column_to_check is relative to the LO.Range
    Set C = .Find(what:="*", after:=.Cells(1), LookIn:=xlValues, _
        searchorder:=xlByRows, searchdirection:=xlPrevious)
    If Not C Is Nothing Then
        'do stuff`
        Debug.Print C.Row + 1 'last empty row
          'If the row is the last in the table, then column is full
    End If



End With

With ws

Lastrow = C.Row + 1

        .Cells(Lastrow, 2).Value = TextBox2.Text
        .Cells(Lastrow, 3).Value = TextBox3.Text
        .Cells(Lastrow, 4).Value = TextBox4.Text

End With

        TextBox2.Value = ""
        TextBox3.Value = ""
        TextBox4.Value = ""


End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
See if this helps
VBA Code:
Private Sub CommandButton2_Click()
    Dim ws As Worksheet
    Dim LO As ListObject
    Dim Lastrow As Long
    Dim C As Range

    Set ws = ThisWorkbook.Sheets("Sheet1")
    Set LO = ws.ListObjects("Table1")

With LO.Range.Columns(2) 'column_to_check is relative to the LO.Range
    Set C = .Find(what:="*", after:=.Cells(1), LookIn:=xlValues, _
        searchorder:=xlByRows, searchdirection:=xlPrevious)
        
    If Not C Is Nothing Then
    
        Debug.Print C.Row + 1 'last empty row
        
        If C.Offset(1, -1).Value = "" Then
            MsgBox "There is no allocated job number available" & vbLf & _
                   "Please have additional Job numbers allocated." & vbLf & _
                   "Will now be exiting this sub"
            Exit Sub
        Else
        'do stuff`
            Lastrow = C.Row + 1
            With ws
                .Cells(Lastrow, 2).Value = TextBox2.Text
                .Cells(Lastrow, 3).Value = TextBox3.Text
                .Cells(Lastrow, 4).Value = TextBox4.Text
            End With
        End If
    End If
End With
        
        TextBox2.Value = ""
        TextBox3.Value = ""
        TextBox4.Value = ""

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,239
Messages
6,123,816
Members
449,127
Latest member
Cyko

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