Column reference

tomleitch

Board Regular
Joined
Jan 3, 2012
Messages
189
I have the following code that I'm using to try and count colour fill of cells in a specified range on another worksheet, and I can't get it to quite work how I want.

The code that I can get working how I want is this:

Code:
twowk = Sheets("PROD").Range("2:2").Find(What:="2WK GATE DATE", lookat:=xlWhole, MatchCase:=False).Columnsixwk = Sheets("PROD").Range("2:2").Find(What:="6WK GATE DATE", lookat:=xlWhole, MatchCase:=False).Column
twelvewk = Sheets("PROD").Range("2:2").Find(What:="12WK GATE DATE", lookat:=xlWhole, MatchCase:=False).Column




Lrow = Sheets("PROD").Cells(Rows.Count, 1).End(xlUp).Row


  For nRowIndex = 3 To Lrow
  
    If Sheets("PROD").Range("M" & nRowIndex).Interior.ColorIndex = 3 Then 'red
      nCellNumber = nCellNumber + 1
    End If
    
    If Sheets("PROD").Range("M" & nRowIndex).Interior.ColorIndex = 46 Then 'orange
      nCellNumber2 = nCellNumber2 + 1
    End If
    
  Next nRowIndex


But instead of using column reference "M" in the range, I want to use twelvewk column identifier like this



Code:
twowk = Sheets("PROD").Range("2:2").Find(What:="2WK GATE DATE", lookat:=xlWhole, MatchCase:=False).Columnsixwk = Sheets("PROD").Range("2:2").Find(What:="6WK GATE DATE", lookat:=xlWhole, MatchCase:=False).Column
twelvewk = Sheets("PROD").Range("2:2").Find(What:="12WK GATE DATE", lookat:=xlWhole, MatchCase:=False).Column




Lrow = Sheets("PROD").Cells(Rows.Count, 1).End(xlUp).Row


  For nRowIndex = 3 To Lrow
  
    If Sheets("PROD").Range(twelvewk & nRowIndex).Interior.ColorIndex = 3 Then 'red
      nCellNumber = nCellNumber + 1
    End If
    
    If Sheets("PROD").Range(twelvewk & nRowIndex).Interior.ColorIndex = 46 Then 'orange
      nCellNumber2 = nCellNumber2 + 1
    End If
    
  Next nRowIndex

I can't get it working like this at all!.... it's probably something obvious - can anyone help?


Many thanks
Tom
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Re: Help with column reference

Try
Code:
If Sheets("PROD").Cells(nRowIndex,twelvewk).Interior.ColorIndex = 3 Then 'red
 
Upvote 0
Re: Help with column reference

Can you explain, in detail, what "can't get it working" means? Do you get an error message? If so, on what line and what is the message?

Just glancing at your code, I suspect one of the issues is that you are trying to use twelvewk as a column letter when it is actually a column number. Instead of using:
Range(twelvewk & nRowIndex), try Cells(nRowIndex,twelvewk). There may be other issues as well.
 
Upvote 0
Re: Help with column reference

Many thanks to you both - what you've said has done the trick.

Much appreciated
Tom
 
Upvote 0
Re: Help with column reference

Glad we could help & thanks for the feedback
 
Upvote 0
Re: Help with column reference

Glad we could help & thanks for the feedback


I'm having a similar problem with a very similar thing, any chance you could give me a pointer?

I'm trying to copy a range but it's not working.

Code:
Worksheets("PROD").Cells(3, twelvewk, Sheets("PROD").Cells(Lrow, twelvewk)).Copy

Thanks
Tom
 
Upvote 0
Re: Help with column reference

Try
Code:
With Worksheets("PROD")
   .Range(.Cells(3, twelvewk), .Cells(lrow, twelvewk)).Copy
End With
 
Upvote 0
Re: Help with column reference

You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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