New invoice using macro

KAYE04

New Member
Joined
Mar 14, 2019
Messages
21
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have created a form in excel with invoice number which is alpha numeric.
im having a hard time fixing the error about the leading zeros in my invoice number. My invoice number is FIN19-0001.
i want to change it from FIN19-0001 to FIN19-0002, FIN19-0003 and so on.
but every time i click the button intended for that macro it will become FIN19-2. the 3 zeros are not included.
how can i fix it. can somebody help me.
thank you so much in advance.
\here is my code
Sub NextInvoice()
Range("B7").Value = Left(Range("B7").Value, 6) & 1 + Mid(Range("B7").Value, 7, 4)
 
i cant understand what are you referring to. im sorry

In cell B7 enter a number say for example the number 2 but just enter a number. Then run the code I provided as published.

Dave
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
sir, i still have some questions:(
what if i want to locked B7 so that no one can edit it. but the next invoice button will still run.
is that possible?
thank you
 
Upvote 0
sir, i still have some questions:(
what if i want to locked B7 so that no one can edit it. but the next invoice button will still run.
is that possible?
thank you


You would have to protect the sheet for cells locked property to have any effect

Try adding lines of code shown to your procedure

Rich (BB code):
Sub NextInvoice()


    ActiveSheet.Unprotect Password:="YOUR_PASSWORD_HERE"


    With Range("B7")
        .Value = .Value + 1
        .NumberFormat = """FIN19-""0000"
    End With
    Range("D11:I11,D12,B26:I32").ClearContents


    With ActiveSheet
        .EnableSelection = xlUnlockedCells
        .Protect Password:="YOUR_PASSWORD_HERE"
    End With


End Sub

enter your password (case sensitive) where shown in red.

Dave
 
Last edited:
Upvote 0
Hi sir,

i just want that particular cell the B7 be locked and some other cell, not all sheet, is that possibe?
 
Upvote 0
Hi sir,

i just want that particular cell the B7 be locked and some other cell, not all sheet, is that possibe?


You can do something like following


Rich (BB code):
Sub NextInvoice()
    With ActiveSheet
    
        .Unprotect Password:="YOUR_PASSORD_HERE"


    With .Range("B7")
        .Locked = True
        .Value = .Value + 1
        .NumberFormat = """FIN19-""0000"
    End With
    
    With .Range("D11:I11,D12,B26:I32")
        .Locked = False
        .ClearContents
    End With
    
        .EnableSelection = xlUnlockedCells
        .Protect Password:="YOUR_PASSORD_HERE"
    End With
End Sub

Ranges in RED are unlocked - you can add other ranges if required

Dave
 
Upvote 0
an error occurred it says unable to set the locked property of the range class :(
 
Upvote 0
an error occurred it says unable to set the locked property of the range class :(

error suggests sheet is still protected.

Rich (BB code):
Sub NextInvoice()
    With ActiveSheet
    
        .Unprotect Password:="YOUR_PASSORD_HERE"


    With .Range("B7")
        .Locked = True
        .Value = .Value + 1
        .NumberFormat = """FIN19-""0000"
    End With
    
    With .Range("D11:I11,D12,B26:I32")
        .Locked = False
        .ClearContents
    End With
    
        .EnableSelection = xlUnlockedCells


        .Protect Password:="YOUR_PASSORD_HERE"
    End With
End Sub

ensure the line in bold is unprotecting the sheet

Dave
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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