Nombor Invois Seterusnya - Berita

Microsoft Excel menawarkan banyak templat invois yang boleh anda muat turun. Tetapi tidak ada cara terpasang untuk menambah nombor invois seterusnya.

Saya merakam video ini menunjukkan cara menambahkan beberapa baris kod VBA ke buku kerja anda supaya anda dapat menyimpan setiap invois sebagai fail baru. Makro kemudian membersihkan invois dan menambahkan 1 pada nombor invois.

Dengan pandangan 166K dan beratus-ratus komen, saya dapati soalan yang sama berulang kali muncul. Menjadi tidak praktikal untuk meminta orang membaca hingga 800 komen kerana jawapan kepada soalan mereka telah dipaparkan enam kali sebelumnya. Oleh itu - untuk soalan popular, saya menghantar kod di sini.

Soalan Lazim # 1

Bolehkah anda menaip kod untuk saya kerana saya tidak dapat menaip?

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

Soalan Lazim # 2

Saya mahu Simpan Invois sebagai PDF pada PC Windows

Catatan

Kod ini hanya berfungsi dalam versi Windows Excel 2010 atau yang lebih baru. Terdapat kod yang berbeza untuk Mac.

Anda harus memilih julat yang berisi faktur dan melakukan Layout Halaman, Print Area, Set Print Area. Sekiranya anda melangkau langkah ini, butang yang digunakan untuk menjalankan makro akan muncul di invois anda!

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

Soalan Lazim # 3

Saya mahu Simpan Invois sebagai fail Excel dan PDF dalam folder yang berbeza

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

Soalan Lazim # 4

Nombor invois saya mempunyai nombor dan huruf

Anda perlu menyesuaikan kodnya. Berikut adalah beberapa contoh. Heidi mempunyai nombor invois seperti SS15001. Apabila saya melihat nombor invois itu, ia adalah awalan dua huruf diikuti dengan 5 digit. Nombor invois baru harus menggunakan KIRI (, 2) dan MID (, 3,5):

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

Berikut adalah contoh yang lebih kompleks. Nombor invois adalah IN-1234-HA di mana IN bermaksud Invoice. 1234 adalah nombor berurutan. HA adalah huruf pertama dari nama pelanggan yang terdapat di 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

Anda boleh mempermudah empat baris di atas seperti berikut:

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

Soalan Lazim # 5

Saya mempunyai makro lain dalam buku kerja (seperti SpellNumber) dan memerlukan makro untuk disimpan juga.

Untuk membolehkan makro anda yang lain berjalan agar fungsi Numbers-to-Words terus berfungsi, strategi ini sedikit berbeza. Daripada menyalin hanya lembaran invois ke buku kerja baru dan menggunakan SaveAs, anda akan (a) Mengingat jalan dan nama fail buku kerja, (b) Menggunakan SaveAs untuk menyimpan keseluruhan buku kerja dengan nombor Invois dalam nama, (c) ) Padamkan buku kerja yang asal. (d) Gunakan SaveAs untuk menyimpan buku kerja dengan nama asal.

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

Soalan Lazim # 6

Saya perlu melindungi lembaran kerja supaya pekerja saya hanya dapat menukar beberapa sel. Apabila saya menjalankan makro anda, saya mendapat "Sel yang anda cuba ubah dilindungi dan oleh itu hanya baca"

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. Ralat Masa Jalankan 1004. Dokumen Tidak Disimpan. Jawapan: Laluan fail mestilah tepat. Tepat sebelum Menyimpan fail tambahkan baris baru dengan MsgBox NewFN. Jalankan kod. Kotak akan muncul menunjukkan jalan fail dan nama fail. Pastikan terdapat pemisah jalan antara jalan dan nama fail.
  6. Ralat Masa Jalankan '1004'. Kaedah 'SaveAs' dari objek '_Workbook' gagal. Jawapan: FileFormat mestilah FileFormat: = xlOpenXMLWorkbook. Apabila anda membacanya, akan terdengar seperti "Excel Open XML Workbook". Ia bukan Buku Kerja Ex One Open XML. Ya, membingungkan bahawa nombor 1 dan huruf kecil L sama dalam video. 1l. Tukar X1OPENXMLWORKBOOK anda menjadi XLOPENXMLWORKBOOK.

Artikel menarik...