Reputation: 45666
I have :
Snippet 1:
For Each sheet In WB.Sheets
wbSheetNames.Add(sheet.Name)
Next sheet
Snippet 2:
For i = 0 To sheetNames.Count - 1
wb.Sheets(i + 2).Name = sheetNames(i).Replace(currMonth, nextMonth)
Next
Snippet 3:
For Each cell In row
If cell.Formula.Contains(nameString) Then
downRange(sheet, cell.Address()).Value = downRange(sheet, cell.Address()).Value
End If
Next cell
Snippet 4:
For Each sheet In sheetsToDelete
sheet.Activate()
sheet.Delete()
Next sheet
Is there some construct or method that can do these in 1 line?
Note:
All the snippets are different and should not be clubbed. These are the situations where I think there might be a smarter way of doing these simple tasks.
Upvotes: 0
Views: 170
Reputation: 45666
So, found no way to reduce any template.
Used the :
trick at some places.. Thanx for that !
Upvotes: 0
Reputation: 48486
Using some LINQ could possibly make some of them use fewer lines, but most of them would not be very readable. You should be writing code for maintainability, not just for getting the job done. If you're thinking about performance: using as few lines as possible is not really a good indication for optimization.
Snippet 1 by itself could be reduced to a single line, assuming wbSheetNames is a List of String:
wbSheetNames.AddRange(WB.Sheets.Select(Function(x) x.Name))
Improvement? Not really. You could remove the list allocation not part of the snippet by using something like:
wbSheetNames = WB.Sheets.Select(Function(x) x.Name).ToList()
This is arguably a slight improvement over the original.
That being said: it looks like you might be able to combine snippet 1 and 2, saving the allocation of a temporary list. The exact intend of the code is not clear to me however. Somewhere along the way you are adding 2 sheets? Otherwise I would think that wb.Sheets(i + 2)
will go out of range.
Upvotes: 1