C
curiousgeorge408
I have some basic VBA questions.
I am traversing a worksheet with the following pseudocode. Note: For
my purposes, it is important that I traverse row-by-row.
with worksheets("Sheet1")
for r = 1 to 1092 ' for each row
...determine n... ' last used column in row
for c = 1 to n ' for each column
...some work...
next c
if ...some condition... then
...some work...
if ...some other condition... then goto nextrow
...some more work...
end if
nextrow:
next r
end with
Questions ....
1. Can I replace 1092 (last used row number) with some non-constant
reference, ideally a property?
Something like .Rows.Count; but that always yields 65536 (I'm
using Excel 2003). If I selected the rows first in Excel, I could use
Selection.Rows.Count. But I do not want to select the rows first.
2. What's the "best" way to determine n, the index of the last used
column in a row -- ideally using a property?
Since I know that the longest row extends to column O, I wrote:
n = 15
do while isempty(.cells(r,n)): n = n - 1: if n = 0 then exit do
loop
I would prefer not to know that the longest row extends to column
O. I could search from the left, if I know there are no interstitial
empty cells; namely:
n = 0: while not isempty(.cells(r,n+1)): n = n + 1: wend
3. Can I replace "goto nextrow" with something else?
In C, I would write:
if (...some condition...) continue;
I am traversing a worksheet with the following pseudocode. Note: For
my purposes, it is important that I traverse row-by-row.
with worksheets("Sheet1")
for r = 1 to 1092 ' for each row
...determine n... ' last used column in row
for c = 1 to n ' for each column
...some work...
next c
if ...some condition... then
...some work...
if ...some other condition... then goto nextrow
...some more work...
end if
nextrow:
next r
end with
Questions ....
1. Can I replace 1092 (last used row number) with some non-constant
reference, ideally a property?
Something like .Rows.Count; but that always yields 65536 (I'm
using Excel 2003). If I selected the rows first in Excel, I could use
Selection.Rows.Count. But I do not want to select the rows first.
2. What's the "best" way to determine n, the index of the last used
column in a row -- ideally using a property?
Since I know that the longest row extends to column O, I wrote:
n = 15
do while isempty(.cells(r,n)): n = n - 1: if n = 0 then exit do
loop
I would prefer not to know that the longest row extends to column
O. I could search from the left, if I know there are no interstitial
empty cells; namely:
n = 0: while not isempty(.cells(r,n+1)): n = n + 1: wend
3. Can I replace "goto nextrow" with something else?
In C, I would write:
if (...some condition...) continue;