Hi Amrita,
The getWorksheet() method returns a worksheet object based on its name or index, but when it is used, it takes into account the active sheet in the workbook. The method will not return the sheet you intended to access by name, but the one you are currently clicked on.
You can use the 'Worksheets' collection and iterate through the sheets to find the one with the required name, ensuring that you always retrieve the right sheet.
Here's an example-
Dim ws As Worksheet
Dim targetSheetName As String
targetSheetName = "Sheet1" ' Replace with the name of your desired sheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name = targetSheetName Then
' Found the desired sheet
' Your code here to work with the sheet (ws)
Exit For ' Exit the loop since we found the sheet
End If
Next ws
Best Regards.