Updating Excel file using VBA

Hey ya’ll, I’m trying to run the following macro code, which is supposed to just add the current time to a column on an excel sheet:


Sub AddDataToExcel(FilePath As String, RowIndex As Integer)
    Dim xlApp As Object
    Dim xlWorkbook As Object
    Dim xlWorksheet As Object
    Dim currentDateTime As String
    
    ' Get the current date and time
    currentDateTime = Format(Now(), "yyyy-mm-dd hh:mm:ss")
    
    ' Create an instance of Excel and open the workbook
    Set xlWorkbook = Application.ActiveWorkbook
    
    ' Set the worksheet where you want to add the data
    Set xlWorksheet = xlWorkbook.Worksheets("Sheet1")
    
    ' Add current date and time to the specified row and column
    xlWorksheet.Cells(RowIndex, 6).Value = currentDateTime
    
    ' Save and close the workbook
    xlWorkbook.Save
    xlWorkbook.Close
    
    ' Clean up the Excel application object
    xlApp.Quit
    Set xlApp = Nothing
End Sub

And for some reason I’m getting a runtime error on the line

    ' Add current date and time to the specified row and column
    xlWorksheet.Cells(RowIndex, 6).Value = currentDateTime

What am I doing wrong here?

Hello @OolongTeaDrinker - Welcome to the UiPath Forum! What’s the description/code of the Run-Time Error message?

Hi @Yosef_Haim_Meiri

Can you try using the square brackets instead of parenthesis?

xlWorksheet.Cells[RowIndex, 6].Value = currentDateTime

@OolongTeaDrinker

Please check the rowindex value you are passing if it is in the required range

Cheers