VBA: ClearNotes in another Table

zero269

Active Member
Joined
Jan 16, 2023
Messages
253
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm working on a VBA macro that will update a cells value and/or cell note in TableName "t_Books" based on a selection from another table.
I was able to get the cell values to work, but I'm unable to figure out how to work with the cells notes.

Using the below Sample Data - all in one sheet for testing.
If I select the green cell in the Student Table and run the macro, the Book Status in the Books Table will be updated based on the following criteria:
Cell value will change to library and the cell note will be cleared. The cell note is not showing in the MiniSheet, but the cellnote = 'library'.

VBA Testing.xlsm
ABCDEFGHI
1Books TableStudent Table
2QuizTitleAuthorBook StatusQuizTitleAuthorBook Status
3176888A Funny Thing Happened on the Way to School...Cali, Davideon hold176888A Funny Thing Happened on the Way to School...Cali, Davideon hold
4162306A Tale of Two SistersLagonegro, Melissaon hold162306A Tale of Two SistersLagonegro, Melissaon hold
5503552Bad Kitty: Searching for SantaBruel, Nickon hold503552Bad Kitty: Searching for SantaBruel, Nickon hold
627484Bathtime for BiscuitCapucilli, Alyssa Satinon hold27484Bathtime for BiscuitCapucilli, Alyssa Satinon hold
7503651Beneath the Bed and Other Scary StoriesBrallier, Maxavailable192899Dog Man and Cat KidPilkey, Davlibrary
8187861Better Call Batman!Bright, J.E.available511040Dog Man: Mothering HeightsPilkey, Davlibrary
9192899Dog Man and Cat KidPilkey, Davlibrary168808Fly Guy's Amazing TricksArnold, Teddavailable
10511040Dog Man: Mothering HeightsPilkey, Davlibrary9018Foot BookSeuss, Dr.library
11168808Fly Guy's Amazing TricksArnold, Teddavailable124813Mind Your Manners, Biscuit!Capucilli, Alyssa Satinavailable
129018Foot BookSeuss, Dr.library171841Poky Little PuppyDepken, Kristenlibrary
13124813Mind Your Manners, Biscuit!Capucilli, Alyssa Satinavailable
14171841Poky Little PuppyDepken, Kristenlibrary
MatchUpdate
Cell Formulas
RangeFormula
I3:I12I3=XLOOKUP([@Quiz],t_Books_MatchUpdate[Quiz],t_Books_MatchUpdate[Book Status],"")


Here's what I've got so far:

VBA Code:
Sub TEST_Update_Book_Status()

  'Select visible cells only if selection is > 1
  If Selection.Cells.Count > 1 Then
    Selection.SpecialCells(xlCellTypeVisible).Select
  Else
  End If
  
  'Declarations
  Dim cell As Range

  'LOOP Selection
  For Each cell In Selection
  
    'Store Selected Values in 'Student' table to Match in 'Books' table
    Dim BookStatus As String: BookStatus = cell.Value
    Dim Quiz As Long: Quiz = cell.Offset(0, -3).Value
    
    'Get Matching Row & Column Index in 'Books' table
    Dim lRow As Long, lCol As Long
    lRow = Application.WorksheetFunction.Match(Quiz, Range("t_Books_MatchUpdate[Quiz]"), 0)
    lCol = wsMatchUpdate.ListObjects("t_Books_MatchUpdate").ListColumns("Book Status").Index
  
    'Declare Books table
    Dim myTbl As ListObject
    Set myTbl = wsMatchUpdate.ListObjects("t_Books_MatchUpdate")
    
    'Update 'Book Status' and Notes in 'Books' table
    If myTbl.DataBodyRange.Cells(lRow, lCol) = "library" Then
      myTbl.DataBodyRange.Cells(lRow, lCol) = "on hold"
      
    ElseIf myTbl.DataBodyRange.Cells(lRow, lCol) = "on hold" Then
      myTbl.DataBodyRange.Cells(lRow, lCol) = "available"
      
    ElseIf myTbl.DataBodyRange.Cells(lRow, lCol) = "available" Then
      myTbl.DataBodyRange.Cells(lRow, lCol) = "library"

    ElseIf myTbl.DataBodyRange.Cells(lRow, lCol).Value = "on order" And myTbl.DataBodyRange.Cells(lRow, lCol).NoteText = "library" Then
      myTbl.DataBodyRange.Cells(lRow, lCol).Value = "library"
      myTbl.DataBodyRange.Cells(lRow, lCol).ClearNotes

    Else
    End If
  
  Next cell
  
  wsMatchUpdate.Calculate

End Sub

Below is what I'm currently using if I'm on the "Books" table. However, I'm trying to get the same functionality from any of the Student tables instead of having to manually search for the book by Quiz number and then updating the Book Status.

VBA Code:
Sub Books_Update_Book_Status()

  'Select visible cells only if selection is > 1
  If Selection.Cells.Count > 1 Then
    Selection.SpecialCells(xlCellTypeVisible).Select
  Else
  End If
  
  'Declarations
  Dim cell As Range

  'LOOP Selection
  For Each cell In Selection
  
    'Update 'Book Status'
    If cell.Value = "library" Then
      cell.Value = "on hold"
      
    ElseIf cell.Value = "on hold" Then
      cell.Value = "available"
      
    ElseIf cell.Value = "available" Then
      cell.Value = "library"

    ElseIf cell.Value = "on order" And LCase(cell.NoteText) = "library" Then
      cell.Value = "library"
      cell.ClearNotes

    Else
    End If
  
  Next cell
  
  wsBooks.Calculate

End Sub

Any help would be greatly appreciated…
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
After more testing and research, I was able to find a solution to my problem. It turns out that I needed to use Range instead of ListObject for the myTbl variable.

Using ListObject:
myTbl.Value returned an error
myTbl returned the cells value

Eventually, I found others using Range with their code and gave it a try… and it worked the same as my original code (2nd one in my OP).

Failed Option using ListObject:
VBA Code:
    'Declare Books table
    Dim myTbl As ListObject
    Set myTbl = wsMatchUpdate.ListObjects("t_Books_MatchUpdate")
Successful Option using Range:
VBA Code:
'Set Working Range
Dim myRange As Range
Set myRange = wsMatchUpdate.ListObjects("t_Books_MatchUpdate").DataBodyRange(lRow, lCol)
My Final Test Code:
VBA Code:
Sub Update_Book_Status()

  Dim cell As Range, myRange As Range
  Dim Quiz As Long, lRow As Long, lCol As Long
 
  For Each cell In Selection
 
    'Store Lookup Value
    Quiz = cell.Offset(0, -3).Value
  
    'Get Matching Row & Column in 'Books' table
    lRow = Application.WorksheetFunction.Match(Quiz, Range("t_Books_MatchUpdate[Quiz]"), 0)
    lCol = wsMatchUpdate.ListObjects("t_Books_MatchUpdate").ListColumns("Book Status").Index
  
    'Set Working Range
    Set myRange = wsMatchUpdate.ListObjects("t_Books_MatchUpdate").DataBodyRange(lRow, lCol)
  
    'Update Book Status
    If myRange.Value = "available" And LCase(myRange.NoteText) = "library" Then
      myRange.Value = "library"
      myRange.ClearNotes
    End If
 
  Next cell

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,862
Messages
6,181,465
Members
453,045
Latest member
Abraxas_X

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