我实际上有一些工作代码,虽然我拥有的数据量和我编写代码的方式需要一个多小时才能运行,而且我仍然需要添加相当多的代码来实际分析数据。我正在使用双循环,在我添加 screenupdating = false 之前,似乎嵌套在内部的循环需要这么长时间。
这是我所拥有的:
Sub LReview()
Dim SecX As Workbook, LipR As Workbook
Dim ws As Worksheet, Xws As Worksheet, Fsheet As Worksheet
Dim i As Long, XwsRows As Long
Path = ThisWorkbook.Path & "\"
Set LipR = ThisWorkbook
Set SecX = Application.Workbooks.Open(Path & "SecurityXtract_Mnthly.csv")
Windows("SecurityXtract_Mnthly.CSV").Activate
Set Xws = Sheets("SecurityXtract_Mnthly")
With Xws
XwsRows = .Range("B" & .Rows.Count).End(xlUp).Row
End With
Windows("LMacro.xlsm").Activate
Sheets.Add.Name = "Funds"
Set ws = Sheets("Funds")
Windows("SecurityXtract_Mnthly.CSV").Activate
Columns("B:B").Select
Selection.Copy
Windows("LMacro.xlsm").Activate
ws.Select
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("$A$1:$A$60000").RemoveDuplicates Columns:=1, Header:= _
xlNo
Application.DisplayAlerts = False
Application.ScreenUpdating = False
With ws
'Change back to 100+
For i = 2 To 5
If ws.Range("A" & i).Value <> "" Then
Sheets.Add(After:=Sheets(Worksheets.Count)).Name = ws.Range("A" & i).Value
Set Fsheet = ActiveSheet
Range("A1").Value = "Fund:"
Range("B1").Value = Fsheet.Name
Range("A2").Value = "Date:"
Range("B2").Value = "=Xtract!R[-1]C"
Windows("SecurityXtract_Mnthly.CSV").Activate
Rows("1:1").Select
Selection.Copy
Windows("LMacro.xlsm").Activate
Rows("4:4").Select
ActiveSheet.Paste
Selection.Font.Bold = True
Application.CutCopyMode = False
For j = 2 To XwsRows
If Xws.Range("B" & j).Value = Fsheet.Range("B1") Then
Windows("SecurityXtract_Mnthly.CSV").Activate
Xws.Range("B" & j).Select
ActiveCell.EntireRow.Select
Selection.Copy
Windows("LMacro.xlsm").Activate
Fsheet.Range("A" & j + 3).EntireRow.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Columns("A:A").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
End If
Next j
Range("C:D, F:F, I:BB, BD:BL, BP:BR, BT:BV, BX:CD, CF:CN, CP:DI").EntireColumn.Select
Selection.Delete Shift:=xlToLeft
End If
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
Next i
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
我还在另一个问题上找到了这段代码,但我不确定它是否可以应用,因为我使用的是两个不同的工作簿。这段代码:
If Range("S1").Offset(i) > 0.005 Then
Range("AC").Offset(i).Resize(1, 2).Value = Range("Z").Offset(i).Resize(1, 2).Value
End If
替换了这个:
If Range("S" & i) > 0.005 Then
Range("Z" & i, "AA" & i).Copy
Range("AC" & i, "AD" & i).PasteSpecial xlPasteValues
End If
我在这里引用的此代码/问题的完整链接是: 有关如何加速循环的建议
提前感谢您提供的任何帮助:)