If ws.Range("F" & rows.count).end(xlup).row > 2 then
Sub Demo()
Dim ws As Worksheet, r As Range
For Each ws In ThisWorkbook.Worksheets
Set r = ws.Cells(Rows.Count, "F").End(xlUp)
If r.Row > 2 Then
Debug.Print r.Address(external:=True)
Else
End If
Next ws
End Sub
"Is the last item in Column F after Column 2 (B)?Within the Cells object, the 2nd argument specifies column number. In this case, the 6 refers to col 6, aka col F.
The highlighted 2 is just a plain 2. The whole line says to go to last row in col F, and then go up (the End(xlUp) bit) until it finds something. Check to see if the row where we found something is higher than 2.
Or, in plainer English: Is the last item in col F after col 2?
Thank you.6th column of the sheet (F)
It's always good to know there's more than one way and another perspective.FYI
Another way to write it would be:
Code:If ws.Range("F" & rows.count).end(xlup).row > 2 then
That makes it easier to understand.Dear all,
May be we can say or check by code
Is there column F (6) have more then 2 rows with data?
If more then 2 ,then do something .
Regards,
Chirag Raval
To get specific help while in the Visual Basic Editor (VBE), with cursor in or next to the keyword/command, press F1. Press F2 to browse all objects, methods, and properties.
The help is not the best on this one. Why choose Cells() over Range() one might ask. There are trade-offs to both.
1. Cells() is not known to intellisense. If you type a period after a word, if available, you get a dropdown list to choose from. That is if Tools > Options >Editor > Auto List Member, is checked as is the default.
2. Range() allows intellisense to work if option is set. You may have to use string concatenation as Alan did to build your range's input values. Avoid doing lots of string concatenation. That is a slow process. For a few concatenations, the slower speed is not noticed by humans.
3. Cells() lets you use number or index values for rows, the 1st input, and columns, the 2nd input, as you can see while typing it. Unknown to some is that you can also use the column letter. e.g.
Code:Sub Demo() Dim ws As Worksheet, r As Range For Each ws In ThisWorkbook.Worksheets Set r = ws.Cells(Rows.Count, "F").End(xlUp) If r.Row > 2 Then Debug.Print r.Address(external:=True) Else End If Next ws End Sub
Sub DemoTF()
Dim tf As Boolean
MsgBox tf, , "tf with no assignment, always False"
MsgBox tf = False, , "tf=False is True since tf was False by default."
tf = 1 = 1
If tf Then MsgBox tf, , "tf = 1 = 1, is True since 1=1."
End Sub
For post #10, you should ask yourself, what is my goal?
If you remove the "> 2" then your code is saying: IF ANUMBER Then. This makes no sense so it will error.
One normally does not add an equality operator for True/False (boolean) comparisons as True will always be the True for an IF. e.g.
Howsoever, there is nothing wrong using the equality for boolean variables, e.g.: If tf=True then...Code:Sub DemoTF() Dim tf As Boolean MsgBox tf, , "tf with no assignment, always False" MsgBox tf = False, , "tf=False is True since tf was False by default." tf = 1 = 1 If tf Then MsgBox tf, , "tf = 1 = 1, is True since 1=1." End Sub
The row being greater than 2 probably means what post #5 guessed. Let's say one has rows 1 and 2 with titles. Then one needs to check that data is there or else one might overwrite blank title values where data should go below.
Normally, I would use title rows in only row 1. I would then check with the End Up method to find the next non-blank value from bottom up. When found, I add one to a .Row return for the range. My preference is to assign a Range object to the find and then use .Offset(1) to offset one row below. In other words, I want to write to the next empty row in a column from bottom up. Why go bottom up and not End Down? That would stop at the last value before an empty cell. Sometimes, data will follow which is likely not what you wanted. Or worse, End Down could return the last row (1,048,576) if there is no data from the End Down location which is almost never what one wants.
Here is a case where End Down is safe. You built the data. A1 and A2 always exist and no blank values are allowed in a row as these are required values for an ID. I can then safely use End Down from A1 and Offset one row to get my next empty cell to add an ID value.
Here is a scenario. I find with my End Up Offset that row 2 is the next empty row. But, is it really? If the row is 2, then it might have stopped at row 1 whether it was empty or not. In that case, I use Offset(-1) for that range to check if row 1 was empty. Normally, that would be fine though as row one is most always used for column titles, fieldnames in database terminology.
All this End Up/Down and such may be confusing. Just do it manually or record a macro when you do it manually to see what is happening. e.g Press Shift+End in A1. Release and then press the Down arrow key.
No.
I don't know what it does in the True part of the IF(). Let's call it, do stuff.
Let's look at some examples.
1. ws.[F3] is last cell in ws's column F with a value of say, "whatever".
a. IF() is True so do stuff. 3 > 2 = True
2. All cells in ws's column F are empty or = "".
a. IF() is False so goto Next worksheet. 1 > 2 = False
3. ws.[F2] is last cell with a value.
a. IF() is False so goto Next worksheet. 2 > 2 = False
4. ws.[F123] is last cell with a value.
a. IF() is True, do stuff. 123 > 2 = True
No.
I don't know what it does in the True part of the IF(). Let's call it, do stuff.
Let's look at some examples.
1. ws.[F3] is last cell in ws's column F with a value of say, "whatever".
a. IF() is True so do stuff. 3 > 2 = True
2. All cells in ws's column F are empty or = "".
a. IF() is False so goto Next worksheet. 1 > 2 = False
3. ws.[F2] is last cell with a value.
a. IF() is False so goto Next worksheet. 2 > 2 = False
4. ws.[F123] is last cell with a value.
a. IF() is True, do stuff. 123 > 2 = True
Dim r as range, ws as worksheet
Set ws = Worksheets("Sheet1")
set r = ws.[F123]
MsgBox r.Value
MsgBox r.Row
MsgBox r.Address
'etc.