如何使复制和粘贴循环更加平滑?VBA EXCEL
我希望宏根据一个单元格中的datediff值选择特定的数据选择,然后将其复制并粘贴到另一个工作表中。它可以做到这一点,但它不断地来回闪烁,呈现出一种油滑/起伏的外观。有关于如何修复的提示/想法吗如何使复制和粘贴循环更加平滑?VBA EXCEL,vba,excel,clipboard,Vba,Excel,Clipboard,我希望宏根据一个单元格中的datediff值选择特定的数据选择,然后将其复制并粘贴到另一个工作表中。它可以做到这一点,但它不断地来回闪烁,呈现出一种油滑/起伏的外观。有关于如何修复的提示/想法吗 Sub Invoice() Dim s As Integer s = 2 Dim t As Integer t = 21 Dim Newbook As Workbook Set Newbook = Workbooks.Add Workbooks("Workbook2.xlsm").Sheets("
Sub Invoice()
Dim s As Integer
s = 2
Dim t As Integer
t = 21
Dim Newbook As Workbook
Set Newbook = Workbooks.Add
Workbooks("Workbook2.xlsm").Sheets("Invoice Template (2)").Copy Before:=Newbook.Sheets(1)
ActiveSheet.Name = "Current Invoice"
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
Do Until IsEmpty(Cells(s, 1))
mini = Cells(s, 21).Value 'The datediff value I want to find'
If mini = "2" Then
Cells(s, 10).Copy
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Nextrow = Cells(t, 2).Row
Cells(Nextrow, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
Cells(s, 8).Copy
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Nextrow = Cells(t, 3).Row
Cells(Nextrow, 3).Select
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
Cells(s, 11).Copy
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Nextrow = Cells(t, 7).Row
Cells(Nextrow, 7).Select
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
'Calulating the Premium'
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
If Cells(s, 9) = 1001 Then 'Formula for Life, AD & D, ASI, CI'
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Prem = (Cells(t, 2) * Cells(t, 7)) / 1000
Cells(t, 9).Value = Prem
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
ElseIf Cells(s, 9) = 1103 Then 'Formula for LTD'
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Prem = (Cells(t, 2) * Cells(t, 7)) / 100
Cells(t, 9).Value = Prem
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
ElseIf Cells(s, 9) = 1104 Then 'Formula for STD'
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Prem = (Cells(t, 2) * Cells(t, 7)) / 10
Cells(t, 9).Value = Prem
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
ElseIf Cells(s, 9) = 2112 Then 'General Formula'
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Prem = Cells(t, 2) * Cells(t, 7)
Cells(t, 9).Value = Prem
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
End If
'Calculating Commission'
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
If Cells(s, 15) = 5501 Then
'Add Commission schedule for ACE AND AIG'
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
ElseIf Cells(s, 15) = 5514 Then
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Cells(18, 4) = 0.06 'Commission Rate'
Cells(38, 8) = 0.9 'Front-Load Payment'
Cells(39, 8) = 0.1 'Hold Back Amount'
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
End If
'Business and Insurer Information'
'Insurer Name'
Cells(s, 14).Copy
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Cells(8, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
'Insurer Address'
Cells(s, 16).Copy
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Cells(9, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
'Insert Solution for City, Province, Postal Code'
'Client Name'
Cells(s, 3).Copy
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Cells(13, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
'Client Address'
Cells(s, 4).Copy
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Cells(14, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
'Insert Solution for City, Province, Postal Code'
Cells(s, 1).Copy
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Cells(10, 9).Select
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
'Renewal Date'
Cells(s, 22).Copy
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Cells(11, 9).Select
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
'Anniversary Date'
Cells(s, 20).Copy
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Cells(12, 9).Select
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
t = t + 1
End If
s = s + 1
Loop
Newbook.Activate
Dim Client As String
Client = Cells(13, 2).Value
Dim Presently As String
Presently = " - " & MonthName(Month(Date)) & " " & Year(Date)
'ActiveWorkbook.SaveAs Filename:=Client & "Invoice" & Presently'
End Sub
您可以简化所有代码块,如下所示:
Cells(s, 10).Copy
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Nextrow = Cells(t, 2).Row
Cells(Nextrow, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
改为类似的内容(全部三个块,简化为):
您还可以简化所有这些:
If Cells(s, 9) = 1001 Then 'Formula for Life, AD & D, ASI, CI'
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Prem = (Cells(t, 2) * Cells(t, 7)) / 1000
Cells(t, 9).Value = Prem
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
ElseIf Cells(s, 9) = 1103 Then 'Formula for LTD'
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Prem = (Cells(t, 2) * Cells(t, 7)) / 100
Cells(t, 9).Value = Prem
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
ElseIf Cells(s, 9) = 1104 Then 'Formula for STD'
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Prem = (Cells(t, 2) * Cells(t, 7)) / 10
Cells(t, 9).Value = Prem
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
ElseIf Cells(s, 9) = 2112 Then 'General Formula'
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Prem = Cells(t, 2) * Cells(t, 7)
Cells(t, 9).Value = Prem
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
End If
为此:
Dim wsInvoice as Worksheet
Set wsInvoice = Newbook.Sheets("Current Invoice") 'You could move these lines the the
' beginning of your code and replace
' all references to NewBook.Sheets("CurrentInvoice") with wsInvoice
With wsInvoice
Select Case Cells(s, 9)
Case 1001 'Formula for Life, AD & D, ASI, CI'
Prem = (.Cells(t, 2) * .Cells(t, 7)) / 1000
Case 1103 'Formula for LTD'
Prem = (.Cells(t, 2) * .Cells(t, 7)) / 100
Case 1104 'Formula for STD'
Prem = (.Cells(t, 2) * .Cells(t, 7)) / 10
Case 2112 'General Formula'
Prem = (.Cells(t, 2) * .Cells(t, 7))
End Select
.Cells(t, 9).Value = Prem
End With
您可以简化所有代码块,如下所示:
Cells(s, 10).Copy
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Nextrow = Cells(t, 2).Row
Cells(Nextrow, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
改为类似的内容(全部三个块,简化为):
您还可以简化所有这些:
If Cells(s, 9) = 1001 Then 'Formula for Life, AD & D, ASI, CI'
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Prem = (Cells(t, 2) * Cells(t, 7)) / 1000
Cells(t, 9).Value = Prem
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
ElseIf Cells(s, 9) = 1103 Then 'Formula for LTD'
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Prem = (Cells(t, 2) * Cells(t, 7)) / 100
Cells(t, 9).Value = Prem
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
ElseIf Cells(s, 9) = 1104 Then 'Formula for STD'
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Prem = (Cells(t, 2) * Cells(t, 7)) / 10
Cells(t, 9).Value = Prem
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
ElseIf Cells(s, 9) = 2112 Then 'General Formula'
Newbook.Activate
Newbook.Sheets("Current Invoice").Select
Prem = Cells(t, 2) * Cells(t, 7)
Cells(t, 9).Value = Prem
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
End If
为此:
Dim wsInvoice as Worksheet
Set wsInvoice = Newbook.Sheets("Current Invoice") 'You could move these lines the the
' beginning of your code and replace
' all references to NewBook.Sheets("CurrentInvoice") with wsInvoice
With wsInvoice
Select Case Cells(s, 9)
Case 1001 'Formula for Life, AD & D, ASI, CI'
Prem = (.Cells(t, 2) * .Cells(t, 7)) / 1000
Case 1103 'Formula for LTD'
Prem = (.Cells(t, 2) * .Cells(t, 7)) / 100
Case 1104 'Formula for STD'
Prem = (.Cells(t, 2) * .Cells(t, 7)) / 10
Case 2112 'General Formula'
Prem = (.Cells(t, 2) * .Cells(t, 7))
End Select
.Cells(t, 9).Value = Prem
End With
使用
application.ScreenUpdate=false
停止刷新屏幕。因此,直到A)代码遇到应用程序后,用户才会看到发生了什么。ScreenUpdate=true
或所有代码都已完成。这也应该加快代码时间 使用application.screenUpdate=false
停止刷新屏幕。因此,直到A)代码遇到应用程序后,用户才会看到发生了什么。ScreenUpdate=true
或所有代码都已完成。这也应该加快代码时间 如果有任何其他应用程序监视剪贴板,则代码将失败。您不能复制然后期望能够立即粘贴数据。执行复制后,Windows会立即通知其他应用程序更新,它们可以(也将)抓住剪贴板以获取数据。例如远程桌面、任何剪贴板监视器/扩展器、MS Office剪贴板、各种浏览器扩展,甚至Google Earth
至少,您需要围绕粘贴进行休眠/重试循环。如果有任何其他应用程序监视剪贴板,您的代码将失败。您不能复制然后期望能够立即粘贴数据。执行复制后,Windows会立即通知其他应用程序更新,它们可以(也将)抓住剪贴板以获取数据。例如远程桌面、任何剪贴板监视器/扩展器、MS Office剪贴板、各种浏览器扩展,甚至Google Earth
至少,您需要围绕粘贴进行休眠/重试循环。可能会对您有所帮助……此外,您还需要了解如何使代码更加健壮的提示。可能会对您有所帮助…另外,关于如何使您的代码更健壮的提示。非常感谢!这正是我所需要的!不客气!我做了一次修改,也简化了代码的另一大块。干杯,天哪!非常感谢。在一个动作中,速度快得多!你太棒了!:')非常感谢你!这正是我所需要的!不客气!我做了一次修改,也简化了代码的另一大块。干杯,天哪!非常感谢。在一个动作中,速度快得多!你太棒了!:')