How to combine lines of code?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I currently have multiple situations where code resembles the following:

Range("a" & numberrows + 1).Select
Range("a" & numberrows + 1).Formula =
"=date(YEAR(begrevdate)+1,month(begrevdate),day(begrevdate))"
Selection.NumberFormat = "m/d/yyyy"

In order to reduce the size and complexity, I would like to be able to
condense some of the coding to resemble something like:

Range("a" & numberrows + 1).Formula =
"=date(YEAR(begrevdate)+1,month(begrevdate),day(begrevdate))"; NumberFormat =
"m/d/yyyy"

I thought this was possible but my attempts to play with syntax have been
unsuccessful in getting it to work.

Any help would be much appreciated.
TIA
 
It can't be done in one line, but it can be cleaned up a little...

With Range("a" & numberrows + 1)
.Formula = "=date(YEAR(begrevdate)+1,month(begrevdate),day(begrevdate))"
.NumberFormat = "m/d/yyyy"
end with

Note this no longer selects the cell so if you require the active cell to
move then you will need to add a

..select

in the with clause

HTH
 
Papa,
You can use a ":" to write more than statement on a line, but this usually
does not help readability.
As Jim indicated, use of With and also object variables make coding clearer.

NickHK
 

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

Back
Top