OK , merci pour la précision . Du coup j'ai compris ce que tu voulais me dire ;) .
Maintenant , ça ne fait pas évoluer mon problème , mais merci quand même .
Hello Bruno,
The error message "Runtime error 9, subscript does not belong to selection" typically occurs when you try to access an array element, worksheet or workbook that does not exist or is out of range. In your case, it seems that the error is caused by the line Sheets(ProjectSheetName).Select
, where ProjectSheetName
may not refer to a valid worksheet name.
To debug this issue, you can try the following steps:
Check the value of the variable ProjectSheetName
before the Sheets(ProjectSheetName).Select
line is executed. Make sure it is a valid worksheet name.
Check if the worksheet name is spelled correctly and if it matches the name in your Excel file. Note that worksheet names are case-sensitive.
Check if the worksheet is currently active. If it is not active, you may need to activate it before selecting it.
Check if the worksheet exists in the workbook. You can use the Worksheets.Exists
method to determine if the worksheet exists in the workbook.
Here's an example of how you can modify your code to check if the worksheet exists before selecting it:
' Check if the worksheet exists before selecting it
If Not Worksheets.Exists(ProjectSheetName) Then
MsgBox "Worksheet " & ProjectSheetName & " does not exist!", vbCritical
Exit Sub
End If
' Activate and select the worksheet
With Worksheets(ProjectSheetName)
.Activate
.Select
End With
This code will first check if the worksheet exists before activating and selecting it. If the worksheet does not exist, it will display an error message and exit the sub.
I hope this helps you to resolve your issue. If you have any further questions or if the issue persists, please let me know.