PDA

View Full Version : Run time error 9 : Subscript out of range



bbarhate
08-22-2016, 03:32 AM
Hi , I am a new learner and started with learning small codes as of now.

I wanted to write a code which will color cell A1 of sheet3 in workbook named Selected Ranges.xlsm. I have written following code and it is giving error mentioned in subject when pressing F8 at line Worksheets("Selecting Ranges.xlsm").select. please help with this. Why is it giving error? and how should you approach the error when similar error comes?

Sub ColorCellA1()


Worksheets("Selecting Ranges.xlsm").select

Worksheets("Sheet3").Select

Cells.ClearFormats

Cells(1, 1) = RGB(75, 139, 203)

End Sub

Jan Karel Pieterse
08-22-2016, 04:08 AM
There is no need to select things and it should be Workbooks, not Worksheets. So change what you have to:

With Worbooks("Selecting Ranges.xlsm").Worksheets("Sheet3")
.Cells.ClearFormats
.Cells(1, 1) = RGB(75, 139, 203)
End With

bbarhate
08-22-2016, 04:30 AM
Hi, Thanks for the reply.

Had to add "cells(1,1).Interior.color" in the second line to add color in addition to use workbooks as suggested by you.

Appreciate your help.

Jan Karel Pieterse
08-22-2016, 04:46 AM
O, yes, I overlooked that :-)