Nākamais rēķina numurs - jaunumi

Microsoft Excel piedāvā daudzas rēķinu veidnes, kuras varat lejupielādēt. Bet nav iebūvēta veida, kā palielināt nākamo rēķina numuru.

Es ierakstīju šo video, parādot, kā darbgrāmatai pievienot dažas VBA koda rindas, lai jūs katru rēķinu varētu saglabāt kā jaunu failu. Pēc tam makro notīra rēķinu un rēķina numuram pievieno 1.

Ar 166 000 skatījumiem un simtiem komentāru es uzskatu, ka tie paši jautājumi atkal un atkal nāk klajā. Palūgt cilvēkiem izlasīt 800 komentārus ir kļuvis nepraktiski, jo atbilde uz viņu jautājumu iepriekš tika izlikta sešas reizes. Tātad - par populāriem jautājumiem es šeit ievietoju kodu.

FAQ # 1

Vai varat ierakstīt kodu man, jo es nevaru ievadīt?

Sub NextInvoice() Range("E5").Value = Range("E5").Value + 1 Range("A20:E39").ClearContents End Sub Sub SaveInvoiceWithNewName() Dim NewFN As Variant ' Copy Invoice to a New Workbook ActiveSheet.Copy NewFN = "C:aaaInv" & Range("E5").Value & ".xlsx" ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook ActiveWorkbook.Close NextInvoice End Sub

FAQ # 2

Es vēlos saglabāt rēķinu kā PDF failu uz Windows datora

Piezīme

Šis kods darbojas tikai Excel 2010 vai jaunākās Windows versijās. Mac datoram ir atšķirīgs kods.

Jums ir jāizvēlas diapazons, kurā ir rēķins, un jāizdara Page Layout, Print Area, Set Print Area. Ja izlaidīsit šo darbību, rēķinā tiks parādītas pogas, ko izmanto makro darbināšanai!

Sub SaveInvoiceAsPDFAndClear() Dim NewFN As Variant NewFN = "C:aaaInv" & Range("E5").Value & ".pdf" ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=NewFN, _ Quality:=xlQualityStandard, IncludeDocProperties:=True, _ IgnorePrintAreas:=False, OpenAfterPublish:=False Range("E5").Value = Range("E5").Value + 1 Range("A20:E39").ClearContents End Sub

FAQ # 3

Es vēlos saglabāt rēķinu gan kā Excel failu, gan kā PDF citā mapē

Sub SaveInvoiceBothWaysAndClear() Dim NewFN As Variant ' Create the PDF First NewFN = "C:aaaPDFInvoicesInv" & Range("E5").Value & ".pdf" ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=NewFN, _ Quality:=xlQualityStandard, IncludeDocProperties:=True, _ IgnorePrintAreas:=False, OpenAfterPublish:=False ' Next, Save the Excel File ActiveSheet.Copy NewFN = "C:aaaInv" & Range("E5").Value & ".xlsx" ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook ActiveWorkbook.Close ' Increment the invoice number Range("E5").Value = Range("E5").Value + 1 ' Clear out the invoice fields Range("A20:E39").ClearContents End Sub

FAQ # 4

Manam rēķina numuram ir cipari un burti

Jums būs jāpielāgo kods. Šeit ir daži piemēri. Heidijai ir rēķina numurs, piemēram, SS15001. Kad es aplūkoju šo rēķina numuru, tas ir divu burtu prefikss, kam seko 5 cipari. Jaunajam rēķina numuram jāizmanto LEFT (, 2) un MID (, 3,5):

Range("E5").Value = Left(Range("E5").Value, 2) & 1 + Mid(Range("E5").Value, 3, 5)

Šeit ir sarežģītāks piemērs. Rēķina numurs ir IN-1234-HA, kur IN nozīmē Rēķinu. 1234 ir kārtas numurs. HA ir pirmais pēc klienta vārda burtiem, kas atrodams B10.

LeftPart = Left(Range("E5").Value, 3) MidPart = Left(Range("E5").Value, 4, 4) + 1 EndPart = Left(Range("A10").Value, 2) Range("E5").Value = LeftPart & MidPart & EndPart

Iepriekšējās četras rindas jūs varētu vienkāršot šādi:

Range("E5").Value = Left(Range("E5").Value, 3) & Left(Range("E5").Value, 4, 4) + 1 & Left(Range("A10").Value, 2)

FAQ # 5

Darbgrāmatā ir citi makro (piemēram, SpellNumber), un arī makro ir jāsaglabā.

Lai ļautu darboties citiem jūsu makro, lai funkcija Numbers-to-Words turpinātu darboties, stratēģija ir nedaudz atšķirīga. Tā vietā, lai kopētu tikai rēķina lapu uz jaunu darbgrāmatu un izmantotu SaveAs, jūs (a) atcerēsities darbgrāmatas ceļu un faila nosaukumu, (b) izmantosiet SaveAs, lai saglabātu visu darbgrāmatu ar rēķina numuru nosaukumā, (c) ) Izdzēsiet sākotnējo darbgrāmatu. (d) Izmantojiet SaveAs, lai saglabātu darbgrāmatu ar sākotnējo nosaukumu.

Sub SaveInvoiceWithNewName() Dim OrigFN as Variant Dim NewFN As Variant ' Remember the original path and file name OrigFN = ThisWorkbook.FullName NewFN = "C:aaaInv" & Range("E5").Value & ".xlsx" ' Save a copy with the new name ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook ' Delete the original workbook so you can save without warning On Error Resume Next Kill (OrigFN) On Error Goto 0 ' Save again as the original file name ActiveWorkbook.SaveAs OrigFN, FileFormat:=xlOpenXMLWorkbook NextInvoice End Sub

FAQ # 6

Man jāaizsargā darblapa, lai mani darbinieki varētu mainīt tikai dažas šūnas. Palaižot jūsu makro, tiek parādīts paziņojums “Šūna, kuru mēģināt mainīt, ir aizsargāta un tāpēc tikai lasāma”

You can unprotect the sheet in the macro, write the new invoice number, and then protect the sheet. This only has to be done in the NextInvoice macro. The other macro is not making changes to any cells. Here is the code if you are using protection without a password:

Sub NextInvoice() ActiveSheet.Unprotect Range("E5").Value = Range("E5").Value + 1 Range("A20:E39").ClearContents ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True End Sub Here is the code if you have protected the sheet with a password of Tomato. Note that the password appears in this code twice. Change the password to the real password in two places. Sub NextInvoice() ActiveSheet.Unprotect Password:="Tomato" Range("E5").Value = Range("E5").Value + 1 Range("A20:E39").ClearContents ActiveSheet.Protect Password:="Tomato", DrawingObjects:=True, Contents:=True, Scenarios:=True End Sub

FAQ #7

I don't want to have the Save button in the Saved workbook

This is tricky because the name of your shape will change when the worksheet moves to a new workbook. Very carefully follow these steps:

  1. Open the invoice macro workbook
  2. Right-click on the sheet tab that contains your invoice. Choose Move or Copy.
  3. In the To Book: dropdown, choose New Book. Choose the checkbox for Create a Copy. Click OK. This step 3 simulates the ActiveSheet.Copy in the VBA.
  4. Now that you have the invoice worksheet in a new workbook, Ctrl-click on the shape that you want to delete. Ctrl+Click will select the shape without running the macro.
  5. With the shape selected, look to the left of the Formula Bar. The Name Box will show a name such as "Rounded Rectangle 1". Very carefully build a new line of code to delete this shape as shown below just after ActiveSheet.Copy:
Sub SaveInvoiceWithNewName() Dim NewFN As Variant ' Copy Invoice to a New Workbook ActiveSheet.Copy ActiveSheet.Shapes("Rounded Rectangle 1").Delete NewFN = "C:aaaInv" & Range("E5").Value & ".xlsx" ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook ActiveWorkbook.Close NextInvoice End Sub

Troubleshooting & Error Messages

  1. The following features cannot be saved in macro-free workbooks: VB Project. Answer: Your file is currently saved as an XLSX file. I am equally annoyed that Microsoft chose to use a broken file type as the default. Use File, SaveAs and change the file type to either XLSB or XLSM.
  2. Type Mismatch. Answer: The code expects your invoice number to be a number. If you have SS15001 as an invoice number, you will have to figure out how to adapt your code. See FAQ #4 above.
  3. Compile error Expected line number or label or statement or end of statement on NewFN = “F:RobinusinessPCreceiptsInv” & Range(“H10”).Value & “.xlsx”. Answer: VBA does not like slanted quotation marks (also called Typographers quotes). Type the quotation mark in VBA and you will get "F:RobinusinessPCreceiptsInv" & Range("H10").Value & ".xlsx"
  4. We couldn't find C:UserJelenDocuments" Answer: The file path has to be exact. Are you sure you didn't mean C:UsersJelenDocuments? (Note the "s" at the end of users)
  5. Izpildes laika kļūda 1004. Dokuments nav saglabāts. Atbilde: faila ceļam jābūt precīzam. Tieši pirms faila saglabāšanas pievienojiet jaunu rindu ar MsgBox NewFN. Palaidiet kodu. Tiks parādīts lodziņš, kurā būs redzams faila ceļš un faila nosaukums. Pārliecinieties, ka starp ceļu un faila nosaukumu ir ceļa atdalītājs.
  6. Izpildes laika kļūda '1004'. Neizdevās objekta '_Workbook' metode 'SaveAs'. Atbilde: FileFormat ir jābūt FileFormat: = xlOpenXMLWorkbook. Izlasot, tam vajadzētu izklausīties kā "Excel Open XML darbgrāmata". Tā nav Ex One Open XML darbgrāmata. Jā, mulsina tas, ka cipars 1 un mazais burts L videoklipā izskatās vienādi. 1l. Mainiet savu X1OPENXMLWORKBOOK uz XLOPENXMLWORKBOOK.

Interesanti raksti...