vba formula in yyymmdd

ummjay

Board Regular
Joined
Oct 1, 2010
Messages
193
hi! how can i set this formula (today+91) as a general, in yyyymmdd format? So that I can then copy and paste it over to AF:?

VBA Code:
'Maturity formula
    Range("AM" & lastrowadj & ":AM" & lastrowfinal).Formula = "=today()+91"
    Range("AM" & lastrowadj & ":AM" & lastrowfinal).Copy
    Range("AF" & lastrowadj & ":AF" & lastrowfinal).PasteSpecial xlPasteValues
    Range("AM7:AM" & lastrowfinal).Clear
    Application.CutCopyMode = False
  
    'sort by symbol
    Range("AD7:AL" & lastrowfinal).Sort key1:=Range("AG7:AG" & lastrow), _
    order1:=xlAscending, Header:=xlNo
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Your overall approach can be simplified greatly. Replace the code above with this:
Rich (BB code):
'Maturity formula
    Range("AF" & lastrowadj & ":AF" & lastrowfinal).Value = Date + 91
    Range("AF" & lastrowadj & ":AF" & lastrowfinal).NumberFormat = "yyyymmdd"

    'sort by symbol
    Range("AD7:AL" & lastrowfinal).Sort key1:=Range("AG7:AG" & lastrow), _
    order1:=xlAscending, Header:=xlNo

You could just format column AF in the worksheet to have the desired date format then you wouldn't have to do it in the code.
 
Upvote 1
Solution
how would I convert this to a general format?
Range("AF" & lastrowadj & ":AF" & lastrowfinal).NumberFormat = "yyyymmdd"
 
Upvote 0
This is a great example of how you can easily use the Macro Recorder to get what you need.
Turn on your Macro Recorder, and record yourself manually changing the format of any cell to "General".
Then stop the Macro Recorder and look at the code you recorded, i.e.
VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    Range("B3").Select
    Selection.NumberFormat = "General"
End Sub
Then, just take what you need out of it and apply it to your code, i.e.
Rich (BB code):
Range("AF" & lastrowadj & ":AF" & lastrowfinal).NumberFormat = "General"

The Macro Recorder is a great little tool to get snippets of code like this.
 
Upvote 1

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top