PDA

View Full Version : Cells(x,x) = 1 ?



KevinPhilips
07-24-2013, 08:57 AM
Hi everyone,

quick question:

What does following code mean?


If ActiveSheet.Cells(i, Range("BB1").Column) = 1 Then

How can you ask if Cells (that has a row and a column value) is a number? I'm lost!

Thank you guys in advance.
Kevin

patel
07-24-2013, 09:13 AM
you can use


If ActiveSheet.Cells(i, "BB") = 1 Then
or

If ActiveSheet.Cells(i, 54) = 1 Then

Paul_Hossler
07-24-2013, 09:14 AM
Range ("BB1").Column = 54, the column number of BB1

.Column returns a number, while .Columns returns a Range

It was probably done that way so the original person could use a more meaningful "BB1" instead of 54 to refer to the i-th row, 54th column

Paul

KevinPhilips
07-24-2013, 11:57 AM
So in this case (since it is a "if" statement) the = 1 refers to one cell being selected?

For example when i = 40 -> (40, 54) = 1 -> true (because it's one cell) ?

Zack Barresse
07-24-2013, 12:03 PM
It should be Range().Value or Cells().Value. Having it left off uses the default method for that object, and in the case of Range and Cells it's the Value property. So following a range with an equal sign is saying if this range is equal to this, so the If condition is saying if that cell equals the number 1.

Paul_Hossler
07-24-2013, 04:36 PM
To spell it out in full without any assumptions as to the sheet being referenced and the property being returned ...




If ActiveSheet.Cells(i, ActiveSheet.Range("BB1").Column).Value = 1 Then
'Do the True since the entry in Cell (i, 54) = 1
Else
'Do the False
Endif


Paul

SamT
07-24-2013, 05:20 PM
Hi everyone,

quick question:

What does following code mean?


If ActiveSheet.Cells(i, Range("BB1").Column) = 1 Then

How can you ask if Cells (that has a row and a column value) is a number? I'm lost!

Thank you guys in advance.
Kevin


Cells(1,1).Address = "A1"
Cells(1,2).Address = "B1"
Cells(5,13).Address = "M5"


Dim i As Long
Dim ColNum As long
i = 3
ColNum = Range("BB1").Column
Cells(i,ColNum).Address = "BB3"


Dim i As Long
'ColNum = Range("BB1").Column
For i = 1 to 5
'If Rows 1 to 5 in Column "BB" = 1 then
If Cells(i,Range("BB1").Column)=1 Then 'The "BB1" address can be any row in Column"BB", ie Range("BB99") , but 1 is easier to type ;)
'Set The corresponding cell in Column "BC" = to the Row number + 42
Cells(i, Range("BC1").Column) = i+42
End If ' Else leave the cell blank
Next i

Cells(r,c) Where r is the Row number and c is the Column number is very handy when you need to manipulate Row and Column numbers.