Meaning of simple code

C

courtesio99

Can I know wat does the following statement mean?

For i = 161 To 301
If Trim(Cells(i, 7)) = "1" Then
Rows(i).Hidden = True
ElseIf Trim(Cells(i, 7)) = "True" Then
Rows(i).Hidden = True
End If
If Trim(Cells(i, 7)) = "0" Then
Rows(i).Hidden = False
End If
Next i

Basically, I dunno the meaning of Trim(Cells(i,7))
Can someone pls explain
 
M

macropod

TRIM: Remove both leading and trailing spaces.
CELLS(A,B): Cell at row A, column B.

It's all in the vba help file.

Cheers
 
G

Greg Wilson

The Cells method is used to specify a cell address. The
first argument in the Cells method specifies the cell row
number. The second argument specifies the column number.
For example, Cells(4, 5) simply refers to Cell E4.

Trim (in the form that is used here) is a function used to
remove both leading and trailing blank spaces if they
exist in a cell value. This is usually done to correct
for accidental inclusion of blank space(s) at the
beginning and/or end of an entry. Trim(Cells(4, 5))
removes any leading and trailing blank space(s) if they
exist in Cell E4. Internal blank spaces (e.g. between
words) are not removed by this method.

The code "For i = 161 To 301" followed at the end by "Next
i" is a block code structure that repeatedly runs the code
contained between these two statements for the specified
number of times - in this case 141 times. The "i"
variable starts with an initial value of 161 and is
incremented by 1 for each loop until i has a value of
301. Cells(i, 7) therefore refers first to Cell G161,
then Cell G162 . . . and finally Cell G301. The code in
this manner sequentially references each cell in the range
G161 to G301.

A summary of the code follows:
1) Loops through each cell in the range G161 to G301.
2) For each cell in the above range it first removes any
leading and trailing blank spaces if they exist.
3) After removing blank spaces if they exist, the code
checks to see if the cell value equals "1" (in text form
as opposed to numeric). If found, the row that the cell
is in is therefore hidden.
4) It also checks if the cell value is equal to "True" and
if so also hides the row that the cell is in.
5) Finally, it checks if the cell value is equal to "0"
and if so, in contrast, unhides the row that the cell is
in.

Of note, the values "1" and "0" are expressed as text as
opposed to numeric. This is possibly an error. The code
uses two nested block If statements when only one was
necessary and therefore possibly reflects lack of
experience. Just a guess on my part.

Hope it helps.

Regards,
Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top