Excel:如何复制没有一些指定的单元格?

所以基本上,我想在我的工作表上创build一个超链接,它允许我精确地复制它,没有几个单元格。

我在Microsoft支持网站上发现了这个问题,它允许工作表完全重复:

Sub Copier1() ActiveWorkbook.Sheets("Invoice").Copy _ after:=ActiveWorkbook.Sheets("Invoice") End Sub 

举一个更好的例子,我正在制作一个发票生成器。 我可以input价格和产品,以及计算总数。 我试图做一个简单的button,在一个空白的新工作表中创build一个新的发票,同时使我的发票号码增加1。

透明红色的单元格是不应该被复制的单元格。 尽pipe它们包含应该被复制的公式。 有没有办法复制完整的工作表,同时“重置”它并将发票号码加1? 所有需要“重置”的单元格都可以在macros中硬编码,因为发票的布局总是相同的。

在这里输入图像说明

我怎样才能做到这一点?

这复制工作表,然后清除产品信息

 Sub createNewInvoice() 'this assumes the top portion of the invoice never changes Dim startRow As Integer Dim startCol As Integer Dim invNumber As Integer Dim ws As Worksheet Dim invCol As Integer Dim invRow As Integer invRow = 8 invCol = 6 'F column startRow = 18 '18 is the first line of items startCol = 2 'B 'get the invoice number invNumber = CInt(ActiveWorkbook.Sheets("Invoice").Cells(invRow, invCol).Value) 'set the worksheet object Set ws = ActiveWorkbook.Sheets("Invoice") 'copy after invoice ws.Copy After:=ws 'update our invoice number ws.Cells(invRow, invCol).Value = invNumber + 1 'make the worksheet active ws.Activate 'clear out our cells with the product info 'clear the first line and delete the rest Do While Trim(ws.Cells(startRow, startCol).Value) <> "" If startRow = 18 Then ws.Cells(startRow, startCol).EntireRow.ClearContents Else ws.Cells(startRow, startCol).EntireRow.Delete shift:=Excel.xlShiftUp 'reset the row startRow = startRow - 1 End If 'move to the next row startRow = startRow + 1 Loop 'release the worksheet object Set ws = Nothing End Sub 

我想在你有一个可用的系统之前,你有一段路要走,但是这里有一个如何去做你所问的例子。 请注意,很多这是非常手动(所有的Range东西),这使得它很危险 – 如果你曾经重新安排工作表上的东西,你将不得不修改相应的代码。 我强烈build议Access来完成这样一个任务,这是非常值得的学习曲线。 另外,我没有在下面做,但是你可能想要改变新表的名字。

 Public Sub NewInvoice() Dim wksht As Worksheet Dim newwksht As Worksheet 'Copy the Invoice worksheet Set wksht = ActiveWorkbook.Sheets("Invoice") wksht.Copy after:=wksht 'The new worksheet is active, get a reference to it Set newwksht = ActiveSheet With newwksht 'Clear all the input cells 'Customer Info .Range("C7:C13").Value = "" 'Company/Date .Range("F7").Value = "" .Range("F8").Value = .Range("F8").Value + 1 'Increment Invoice Number .Range("F9").Value = "" 'Upper left Product # all the way to the lower right Line Total, however many there might be. .Range(.Range("B18"), .Range("B18").End(xlDown).End(xlToRight)).Value = "" End With End Sub