How can I translate the VBA code to excel office script (type script)?

Inbar Shlomo 0 Reputation points
2023-12-21T01:58:26.2866667+00:00

Hey!

I need help translating VBA code to office script. my excel sheets are set up like this: GESAC50mAvailability

LaneBookings

I used VBA code to return 1 next to time intervals that were booked out and 0 next to time intervals that weren't booked out for each clubZoneId. The problem is that I want the code to run daily at the same time every day using powerAutomate. How can I translate it to type script? This is the VBA code I used: Thanks in advance :)

Sub MarkAvailabilityWithBookings()
    Dim LastRow As Long
    Dim i As Long
    Dim StartDate As Date
    Dim EndDate As Date
    Dim AvailabilityData As Worksheet
    Dim Bookings As Worksheet
    
    ' Set your worksheet name
    Set AvailabilityData = ThisWorkbook.Sheets("GESAC50mAvailability") ' Replace with the actual sheet name
    Set Bookings = ThisWorkbook.Sheets("LaneBookings") ' Replace with the actual sheet name
    
    ' Define the start and end dates
    StartDate = AvailabilityData.Cells(2, "A").Value ' Assuming the start date is in column A
    EndDate = AvailabilityData.Cells(2, "B").Value ' Assuming the end date is in column B
    
    ' Find the last row of the AvailabilityData worksheet
    LastRowA = AvailabilityData.Cells(AvailabilityData.Rows.Count, "A").End(xlUp).Row
    LastRowB = Bookings.Cells(Bookings.Rows.Count, "A").End(xlUp).Row
    ' Create a dictionary to store the ClubZoneID and corresponding column letter
    Dim ClubZoneIDs As Object
    Set ClubZoneIDs = CreateObject("Scripting.Dictionary")
    
    ' Define your ClubZoneIDs and column letters
    Dim IDs() As Variant
    Dim ColumnLetters() As Variant
    IDs = Array(139, 129, 130, 131, 132, 133, 134, 135, 136, 128, 118, 281, 282, 283, 284, 285, 286, 287, 288, 189, 290, 119, 269, 120, 271, 270, 121, 272, 411, 122, 274, 275, 123, 124, 125, 126, 127, 227, 228, 273, 267, 268, 229, 230, 231, 232, 197)
    ColumnLetters = Array("C", "D", "E", "F", "G", "H", "I", "J", "K", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY")
    
    ' Loop through each ClubZoneID and corresponding column letter
    For i = LBound(IDs) To UBound(IDs)
        Dim ClubZoneID As Long
        Dim ColumnLetter As String
        
        ClubZoneID = IDs(i)
        ColumnLetter = ColumnLetters(i)
        
        ' Add a new column "HasBooking" next to the time intervals column
        AvailabilityData.Cells(1, LastRowA + 1).Value = "HasBooking" & ClubZoneID
        
        ' Loop through each row in the AvailabilityData worksheet
        For j = 2 To LastRowA ' Assuming data starts from the second row
            Dim IntervalStart As Date
            Dim IntervalEnd As Date
            Dim HasBooking As Integer

            
            ' Get the start and end times for the current interval
            IntervalStart = CDate(AvailabilityData.Cells(j, "A").Text) ' Assuming the time intervals start in column A
            IntervalEnd = CDate(AvailabilityData.Cells(j, "B").Text)
            
            ' Initialize the HasBooking flag to 0
            AvailabilityData.Cells(j, ColumnLetter).Value = 0
            
            ' Loop through each row in the Bookings worksheet
            For k = 2 To LastRowB ' Assuming data starts from the second row
                
                If Bookings.Cells(k, "E").Value = ClubZoneID Then ' Assuming clubZoneId is in column E
                    ' Check if startDate(From) is between startDate and endDate
                    BookingStart = CDate(Bookings.Cells(k, "B").Text)
                    BookingEnd = CDate(Bookings.Cells(k, "C").Text)
                    If ((IntervalStart < BookingEnd) And (IntervalEnd > BookingStart)) Then
                        AvailabilityData.Cells(j, ColumnLetter).Value = 1
                        Exit For
                    End If
                End If
            Next k
            
            ' Update the "HasBooking" column

        Next j
    Next i
End Sub

Microsoft 365 and Office Development Office JavaScript API
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.