+ Reply to Thread
Results 1 to 5 of 5

Help needed to edit macros due to change in data range

Hybrid View

  1. #1
    Registered User
    Join Date
    07-31-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    14

    Help needed to edit macros due to change in data range

    I have a workbook that is used as a booking system and one of the macros inside this workbook enables the name of the person who has booked the room at that time to show up when I hover my mouse over the cell which is highlighted.

    Original.zip

    After feedback from my users, we have decided to switch the columns for "Room" and "Date" such that "Date" is in column B and "Room" is in column C for the "For View; Make Reservation" sheet in the workbook.

    Revised.zip

    The problem now is that the macros doesn't work when these 2 columns are switched. Would anyone know how to modify the existing macro so that it works?

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,521

    Re: Help needed to edit macros due to change in data range

    Give this a try

    Option Explicit
    
    Sub test()
    Dim myDate As Date, x As Double, temp
    Dim Avbl As Long, Bkd As Long, DBkd As Long
    Dim rng As Range, a, w, flg As Boolean
    Dim i As Long, ii As Long, iii As Long
        With Sheets("Check Availability")
            myDate = .Range("D3").Value
            Set rng = .Range("B5", .Range("B" & Rows.Count).End(xlUp)).Resize(, 27)
            Avbl = .Range("J3").Interior.Color
            Bkd = .Range("M3").Interior.Color
            DBkd = .Range("Q3").Interior.Color
        End With
        With rng
            With .Offset(1, 2).Resize(.Rows.Count - 1, .Columns.Count - 2)
                .Interior.Color = Avbl
                .ClearComments
            End With
        End With
        If myDate = 0 Then GoTo Exit_Sub
        a = Sheets("View ; Make Reservation").Range("B3").CurrentRegion.Value
        With CreateObject("Scripting.Dictionary")
            .CompareMode = 1
            For i = 1 To UBound(a, 1)
            If a(i, 1) = "" Then Exit For
                If a(i, 1) = myDate Then
                    If Not .exists(a(i, 2)) Then
                        ReDim w(1 To 3, 1 To 1)
                    Else
                        w = .Item(a(i, 2))
                        ReDim Preserve w(1 To 3, 1 To UBound(w, 2) + 1)
                    End If
                    w(1, UBound(w, 2)) = Round(a(i, 3), 6)
                    w(2, UBound(w, 2)) = Round(a(i, 4), 6)
                    w(3, UBound(w, 2)) = a(i, 5)
                    .Item(a(i, 2)) = w
                End If
            Next
            If .Count = 0 Then GoTo Exit_Sub
            Application.ScreenUpdating = False
            For i = 2 To rng.Rows.Count
                If .exists(rng.Cells(i, 1).Value) Then
                    w = .Item(rng.Cells(i, 1).Value)
                    For ii = 1 To UBound(w, 2)
                        For iii = 3 To rng.Columns.Count
                            x = Round(rng.Cells(1, iii).Value, 6)
                            If x = w(1, ii) Then
                                flg = True
                            ElseIf x = w(2, ii) Then
                                Exit For
                            End If
                            If flg Then
                                If rng.Cells(i, iii).Interior.Color = Avbl Then
                                    With rng.Cells(i, iii)
                                        .Interior.Color = Bkd
                                        .ClearComments
                                        .AddComment w(3, ii)
                                        .Comment.Shape.TextFrame.AutoSize = True
                                    End With
                            Else
                                    With rng.Cells(i, iii)
                                        .Interior.Color = DBkd
                                        temp = .Comment.Text
                                        .Comment.Delete
                                        .AddComment temp & vbLf & w(3, ii)
                                        .Comment.Shape.TextFrame.AutoSize = True
                                    End With
                                End If
                            End If
                        Next
                        flg = False
                    Next
                End If
            Next
        End With
    Exit_Sub:
        Application.ScreenUpdating = True
        Set rng = Nothing
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Registered User
    Join Date
    07-31-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Help needed to edit macros due to change in data range

    Hi Mike,

    Thanks for that! However, I just realised that the "Check Availability" sheet does not update itself automatically when the date in D3 is changed. Instead, every time I changed the date, I have to go to the "View ; Make Reservation" sheet and then back to the "Check Availability" sheet before it can be updated. Is there any macros which I can use to update it automatically?

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,521

    Re: Help needed to edit macros due to change in data range

    Replace the code in your Check Availability sheet to this

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Range("d3"), Target) Is Nothing Then
            If Not IsDate(Range("d3")) Then Exit Sub
            Call test
        End If
    End Sub

  5. #5
    Registered User
    Join Date
    07-31-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Help needed to edit macros due to change in data range

    Thanks Mike! You're a genius!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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