+ Reply to Thread
Results 1 to 7 of 7

Complicated index and match for a booking VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    03-26-2023
    Location
    Geneva
    MS-Off Ver
    16.71
    Posts
    3

    Complicated index and match for a booking VBA

    I am working on a Hotel booking program. When my cell that displays the date is moved forward or backward with buttons, the rooms cell should change colour according to a match in the sheet "Database". In the Sheet "database" there is a table with in column A the room number and in the 1st row the date. So when I display a date I need that the room number colour change if in intersection of the date(row) and the Room number(column) of the Database sheet the cell is "-" = green or if the cell = "customer name" red and if cell ="Locked" yellow.

    I am sorry but I don't know how to explain it better. Hope the images will illustrate my request.

    Capture d??cran 2023-03-26 ? 08.01.39.png
    Capture d??cran 2023-03-26 ? 08.02.21.png

    Thank you for your help
    Attached Files Attached Files
    Last edited by Rafa_24; 03-26-2023 at 03:23 AM.

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,911

    Re: Complicated index and match for a booking VBA

    Hi and welcome

    You don't need VBA to do this. You can use Conditional Formatting with Index/Match formulas to accomplish this the easy way.

    See attached.

    BSB
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    03-26-2023
    Location
    Geneva
    MS-Off Ver
    16.71
    Posts
    3

    Re: Complicated index and match for a booking VBA

    Hello,

    Thank you for your input.

    However it is an assignment and I must use a VBA

    thanks again

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,911

    Re: Complicated index and match for a booking VBA

    Well the INDEX/MATCH formulas you require are in the file I attached. Use those in VBA. Loop through each cell in your range of room numbers and apply them along with the formatting.
    If formula A = true then red, elseif formula B = true then yellow, else green.

    That should be enough of a steer. Can't be doing your homework for you!!

    BSB

  5. #5
    Registered User
    Join Date
    03-26-2023
    Location
    Geneva
    MS-Off Ver
    16.71
    Posts
    3

    Re: Complicated index and match for a booking VBA

    Thank you !

    It helped me but I can't find the bug in my code. The problem seems to be in the red code

    Here is the code:

    Sub Next_button()

    '-------------------------------------------------------
    ' Set the Next button
    '-------------------------------------------------------

    'get the current date in cell A1
    Dim currentDate As Date
    currentDate = Range("AG11").Value

    'add one day to the current date
    Dim nextDate As Date
    nextDate = DateAdd("d", 1, currentDate)

    'update the date in cell A1
    Range("AG11").Value = nextDate

    '-------------------------------------------------------
    ' Set the color match
    ' I need to match the cell AG11 to the row in Database and look for a name in the column if there is a name match with the room a change color
    ' If the cell selected = "name" --> red / if cell = "-" --> green / If cell ="locked" -->yellow
    '-------------------------------------------------------

    Dim Database As Range
    Dim Cell As Range

    ' Define the range of the database
    Set Database = ThisWorkbook.Sheets("Database").Range("C2:ND21")

    ' Loop through all the cells in columns D, I, M, R, and V
    For Each Cell In ThisWorkbook.Sheets("Hotel Management System").Range("D2:D21,I2:I21,M2:M21,R2:R21,V2:V21")

    ' Check if the cell value is not empty
    If Not IsEmpty(Cell.Value) Then

    ' Define the search values for the MATCH functions
    Dim searchValue1 As String
    Dim searchValue2 As String

    searchValue1 = Cell.Value ' The value in the current column
    searchValue2 = ThisWorkbook.Sheets("Hotel Management System").Range("AG11").Value ' The value in cell AG11

    ' Use the INDEX and MATCH functions to get the result
    Dim result As Variant
    result = Application.WorksheetFunction.Index(Database, _
    Application.WorksheetFunction.match(searchValue1, ThisWorkbook.Sheets("Database").Range("A2:A21"), 0), _
    Application.WorksheetFunction.match(searchValue2, ThisWorkbook.Sheets("Database").Range("C1:ND1"), 0))


    ' Check if the result is equal to "-"
    If result = "-" Then
    Cell.Interior.ColorIndex = 4 ' Set the background color to green
    ElseIf result = "Locked" Then
    Cell.Interior.ColorIndex = 6 ' Set the background color to yellow
    ElseIf result <> "-" Then
    Cell.Interior.ColorIndex = 3 ' Set the background color to red
    End If

    End If

    Next Cell

    End Sub

  6. #6
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,911

    Re: Complicated index and match for a booking VBA

    You're variables searchValue1 & searchValue2 are set as strings and therefore the result of the formula is an error.

    Change them to Long data types and it should work.
    Dim searchValue1 As Long
    Dim searchValue2 As Long
    BSB.

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Complicated index and match for a booking VBA

    May I ask why you are looping all cells in ...Range("D2:D21,I2:I21,M2:M21,R2:R21,V2:V21")
    Surely you know the room no's...Just store them in an array and loop the array...
    Also...why oh why merge cells for a room allocation...why not just make use of one cell and increase width & height...
    Or...even better...make use of a Userform...
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

+ 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. [SOLVED] Possibly a complicated index match?
    By jcswaby in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-19-2020, 08:36 AM
  2. Complicated Index/match formula
    By spqr85 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-08-2019, 02:18 PM
  3. Complicated lookup - Index Match
    By roddymc in forum Excel General
    Replies: 4
    Last Post: 11-19-2016, 05:57 AM
  4. [SOLVED] Complicated Index Match
    By willia97 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 11-14-2012, 07:44 PM
  5. Complicated Index Match Offset function
    By Harlan Grove in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 09-06-2005, 11:05 AM
  6. [SOLVED] Complicated Index Match Offset function
    By Harlan Grove in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 09-06-2005, 08:05 AM
  7. more complicated index() / match() function?
    By theillknight in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-15-2005, 07:15 PM

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