Is it possible to optimize this VB.net code ? Can there be a better implementation? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this questionPlease help me optimize this code..
Public Sub createNextMonthSheets(ByRef io As InputOutput)
Dim WB As Ex开发者_开发问答cel.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
I am new to vb.net and don't know the power of this language.
See if you can optimize this on performance !!
See if you can optimize this on fewer lines of code that does the same thing.
You can introduce new aspects of the language that can make this more readable !
Can you overload the findcurrmonthsheetnames function in some way to get the WB.sheets ?
In response to your nested loops question, you can extract all of the required names into a separate array first, then scan the array for each existing name.
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
精彩评论