VBA to Increment Invoice Number

JAlbers

Board Regular
Joined
Sep 9, 2011
Messages
60
I am having some difficulty getting a routine to work and I am very new at trying to write macros so I don't quite understand what it means when it bombs out.

What I am attempting to do is to write a macro that will increment the last number by 1, combine the current year to the number and enter this combined number to a cell in the spreadsheet.

The number (less the current year part) would be written back and saved with the spreadsheet in an out of the way place to use the next time it was needed, then the current date would be entered into the date cell with a copy paste value to keep the =now() function from updating if the file is reopened.

Here is the code (and I use the term loosely) I have so far:

Sub New_MTR_Number()
LastMTR = Worksheets("OldMTRNumber").Range("A1").Value
NextMTR = LastMTR + 1
MTRNumber = "=year(now())" & "NextMTR"
'this appears to be where the problem lies
Worksheets("MTR").Range("D6").Value = MTRNumber
'
Worksheets("OldMTRNumber").Range("A1") = NextMTR
ActiveWorkbook.Save
Range("J6").Select
Selection.FormulaR1C1 = "=NOW()"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Range("J6").Select
Application.CutCopyMode = False
Range("c10").Select
End Sub

Thanks for this forum and all the knowledge that roams these halls!
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I'm not sure I follow exactly what you want to do but here's a guess.

Code:
Sub New_MTR_Number()

    Dim NextMTR As Long

    NextMTR = Worksheets("OldMTRNumber").Range("A1").Value + 1
    Worksheets("OldMTRNumber").Range("A1").Value = NextMTR
    Worksheets("MTR").Range("D6").Value = Year(Date) & NextMTR
    ActiveWorkbook.Save
    
    Range("J6").Value = Date
    Range("C10").Select
    
End Sub
 
Upvote 0
Wow! Thanks! That worked just the way I was shooting for.:) It seems I have a long way to go to be able to get something so concise and simple.

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,216,102
Messages
6,128,846
Members
449,471
Latest member
lachbee

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