cell selection code

N

NDBC

I am trying to get the time (lastlap) from Worksheet (A Grade) when i know it
is located in the nth column (lapcol) and the nth row ridercell.row. I have
checked that the row and column co-ordinates are right but I don't think this
code works. It is returning 0 when it should have been the time 0:00:10. Is
it something to do with the number being in a time format.

LastLap = Sheets("A Grade").Cells(riderCell.Row, LapCol).Value

Thanks again
 
M

Mike H

As long as your sure your row/column coordinates are correct then try this

LastLap = Format(Sheets("A Grade").Cells(ridercell.Row, lapcol).Value,
"hh:mm.ss")

Mike
 
N

NDBC

Mike

Thanks for that. In the interim I had thought that maybe my dim statements
for the lastlap and lapcol were wrong. I deleted the dim statement altogether
for those 2 and then it seemed to work. How should they be set.
 
M

Mike H

Hi,

Ridercell is a range so

Dim ridercell as range

Lapcol is a row number so dim as long

Dim LapCol as long

What I didn't post in my reply is how I tested this and did so by setting up
some variables like this

Dim LapCol As Long, RiderCell As Range
LapCol = 1
Set RiderCell = Range("a1")
LastLap = Format(Sheets("A Grade").Cells(RiderCell.Row, LapCol).Value,
"hh:mm.ss")

So with a time of 00:00:10 in A1 it correctly returned 00:00.10
Mike
 
J

JLatham

Kudos to you for using DIM statements before using variables in your code.
Very good programming practice that will help prevent typographic errors from
causing problems you may not easily detect.

You can 'force' yourself to do this by setting the "Require Variable
Declaration" option under Tools --> Options in the VB Editor itself. This
will automatically add an
Option Explicit
statement at the very beginning of all new code modules. If you have not
been doing that, you can add that statement as the first line to existing
modules to get the same results.
 
N

NDBC

JL, I apologise for not realising the importance of this advice earlier. At
the time I was too wrapped up in solving my immediate problems. It is only
after a month of working on this and going back over all my questions to
ensure I have not forgotten anything that I should have learnt that I can
appreciate what you told me.

To any new time programmers, make sure you follow the advice about the DIM
statements. It may seem like a pain at first but it will pay dividends in the
long run.
 
J

JLatham

A lot of people think this means you have to think EVERYthing out in advance,
and it does not. To do so would basically require you to first write a
requirements specification followed by a design specification and then on
into coding. Other than for large, formalized efforts, that's just not going
to happen.

You can get into code and suddenly realize you need a new variable (or even
constant) and declare it right then and there, in line with your code. I
sometimes do that, BUT before leaving the module, I'll take any DIM
statements I created late in the game like that and put them near the top of
the module where they can all reside together and be found easily.

For global or 'Public' variables and constants, I often create a single
module that I typically name Declarations and put all of those into it.
Again, so that they can be found easily come code maintenance or enhancement
time.

Next advice is to actually write MEANINGFUL comments for future reference.
Don't restate the obvious:

X = X+1 ' add one to X
doesn't do much for you 6 months down the road, the code tells you that
you're adding 1 to X. The thing you'll want to know is WHY and WHAT FOR:
X = X+1 ' increment pointer into address array to look at next entry
might be a bit more helpful later in the life of your application.
 

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