Replace fixed range in code with Table 23 ?

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,699
Office Version
  1. 2007
Platform
  1. Windows
Hi,
I am using the code shown below.
The code has been working fine for me but today its decided to not work correctly.

I have a database of which in column P are invoice numbers but now & then some cells will be empty.
I use the code to search for an empty cell, show me the customers name then i either search for the next empty cell or select the correct customer shown to me.
The code has now decided to start showing me empty cells after my last row with text then all the cells down the page.

As opposed to a fixed range currently P6:P5000 can with have it so when i add customers to my database table of which is Table23 the code still works for within the table range.
Not sure whats happend as my last row with values is Row452

Rich (BB code):
Private Sub BlankInvoiceCell_Click()
Unload DatabaseOpeningForm
  Dim myCell As Range
  For Each myCell In Range("P6:P5000")
    If IsEmpty(myCell) Then
      With Range("A" & myCell.Row)
        If MsgBox("EMPTY INVOICE CELL LOCATED AT ROW: " & myCell.Address(0, 0) & vbCr & vbCr & _
          "THE CUSTOMER IS : " & .Value & vbCr & vbCr & _
          "IS THIS THE CUSTOMER YOU ARE LOOKING FOR ?", vbCritical + vbYesNo, "CUSTOMER INVOICE SEARCH") = vbYes Then
          Unload DatabaseOpeningForm
          .Select
          Exit Sub
        End If
      End With
    End If
  Next myCell
  MsgBox "THE SEARCH IS NOW COMPLETE", vbInformation, "CUSTOMER SEARCH NOW COMPLETE"
  
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
See example.

Ive added the line of code shown in Blue BUT how do i then apply it to only look in column P as shown in Red ?

Rich (BB code):
Private Sub BlankInvoiceCell_Click()
Unload DatabaseOpeningForm
  Dim myCell As Range
  Set myRange = Range("Table23")
  For Each myCell In Range("Column P")
    If IsEmpty(myCell) Then
      With Range("A" & myCell.Row)
        If MsgBox("EMPTY INVOICE CELL LOCATED AT ROW: " & myCell.Address(0, 0) & vbCr & vbCr & _
          "THE CUSTOMER IS : " & .Value & vbCr & vbCr & _
          "IS THIS THE CUSTOMER YOU ARE LOOKING FOR ?", vbCritical + vbYesNo, "CUSTOMER INVOICE SEARCH") = vbYes Then
          Unload DatabaseOpeningForm
          .Select
          Exit Sub
        End If
      End With
    End If
  Next myCell
  MsgBox "THE SEARCH IS NOW COMPLETE", vbInformation, "CUSTOMER SEARCH NOW COMPLETE"
  
End Sub
 
Upvote 0
One option would be:
VBA Code:
  Dim myRange As Range
  
  Set myRange = Range("Table23").ListObject.DataBodyRange
  For Each myCell In Intersect(Columns("P"), myRange)

Typically you would use the name of the column in the table:
VBA Code:
  Dim myRange As Range
  
  Set myRange = Range("Table23").ListObject.ListColumns("Col3").DataBodyRange
  For Each myCell In myRange
 
Upvote 0
Solution
I used the following & works well thanks

Rich (BB code):
  Dim myRange As Range
  
  Set myRange = Range("Table23").ListObject.DataBodyRange
  For Each myCell In Intersect(Columns("P"), myRange)
 
Upvote 0
Just a thought request since I’ve been using this today.
Currently in the code it refers to (Columns(“P”)

Row 5 is hidden & running the code shows me P5 each time.

Can & if so apply something like (Columns(“P6:P”) ?

Please advise thanks.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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