Selecting Cells(r,c)

cmeier7

Board Regular
Joined
Jul 23, 2009
Messages
64
Hello, I am trying to get the below code to work. It determines if cells are grayed out and adds gray to a cell of another worksheet. Currently the code stops at the first selection. What is wrong with how I have it called out <SHEETS("" 3).Select ??).Cells(i, & n>?


Code:
c = 4
n = "Test_STEEL 5in"
j = 5
For i = 4 To 51
    Sheets("" & n & "").Cells(i, 3).Select
    If ActiveCell.Interior.ColorIndex = 15 Then
        Sheets("Test Results_STEEL").Cells(j, c).Select
        ActiveCell.Value = ""
        ActiveCell.Interior.ColorIndex = 15
    Else
    End If
    j = j + 1
Next i

Thanks in advance for this!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
What does the error message say?

If it is subscript out of range, it may be due to Case-sensitive sheet's name

and you can clean up your code by changing to
Code:
c = 4
n = "Test_STEEL 5in"
j = 5
For i = 4 To 51
    If Sheets(n).Cells(i,3).Interior.ColorIndex = 15 Then
        Sheets("Test Results_STEEL").Cells(j, c).Value = ""
        Sheets("Test Results_STEEL").Cells(j, c).Interior.ColorIndex = 15
    End If
    j = j + 1
Next i
 
Last edited:
Upvote 0
You don't need those quote marks if you've already specified the sheetname as one of you variables, and you don't need to select sheets or cells to perform actions on them

Code:
Sub a()
Dim c As Long
Dim n As String
Dim j As Long
Dim i As Long

c = 4
n = "Test_STEEL 5in"
j = 5
For i = 4 To 51


If Sheets(n).Cells(i, 3).Interior.ColorIndex = 15 Then
        With Sheets(c).Cells(j, c)
            .ClearContents
            .Interior.ColorIndex = 15
        End With
    End If
    j = j + 1
Next i
End Sub
 
Upvote 0
Try this

Code:
c = 4
n = "Test_STEEL 5in"
r = "Test Results_STEEL"
j = 5
For i = 4 To 51
    If Sheets(n).Cells(i, 3).Interior.ColorIndex = 15 Then
        With Sheets(r).Cells(j, c)
            .Value = ""
            .Interior.ColorIndex = 15
        End With
    j = j + 1
Next i
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,787
Members
452,942
Latest member
VijayNewtoExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top