请帮我优化此代码..
Public Sub createNextMonthSheets(ByRef io As InputOutput)
Dim WB As Excel.Workbook = getWorkBook(io.newClientReportHandle)
For Each name In clientSheetNames.FindAll(AddressOf findCurrMonthSheetNames)
For Each sheet In WB.Sheets
If (sheet.Name = name) Then
sheet.Name = name.Replace(currMonth, nextMonth)
sheet.Copy(After:=WB.Sheets(1))
End If
Next sheet
Next name
WB.Close()
End Sub
Private Function findCurrMonthSheetNames(ByVal sheetName As String) As Boolean
If sheetName.Contains(" (" + currMonth + ")") Then
Return True
Else
Return False
End If
End Function
我是vb.net的新手,并不知道这种语言的强大功能。
看看你是否可以根据性能对其进行优化!!
看看你是否可以在做同样事情的更少代码行上进行优化。
您可以引入可以使其更具可读性的语言的新方面!
你能以某种方式重载findcurrmonthsheetnames函数来获取WB.sheets吗?
答案 0 :(得分:1)
为了响应您的嵌套循环问题,您可以先将所有必需的名称提取到一个单独的数组中,然后扫描数组中的每个现有名称。
Public Sub createNextMonthSheets(ByRef io As InputOutput)
Dim WB As Excel.Workbook = getWorkBook(io.newClientReportHandle)
Dim CurrMonthSheetNames As String() = clientSheetNames.FindAll(AddressOf findCurrMonthSheetNames)
For Each sheet In WB.Sheets
Dim name As String = sheet.Name
If Array.Contains(CurrMonthSheetNames, name) Then
sheet.Name = name.Replace(currMonth, nextMonth)
sheet.Copy(After:=WB.Sheets(1))
End If
Next sheet
WB.Close()
End Sub