I want VBA code to do this: if cell equals "blah blah", then insert new columns, else move to next sheet. Thing is, that if I run it without
wSheet
then everything works (except for going to next sheet). When I add wSheet, code stays in the first active sheet creates additional columns, even though cell does not equal 2013 06. Any suggestions? Thanks!
Sub Macro2()
'
' Macro2 Macro
'
dim wSheet As Worksheet
For Each wSheet In Worksheets
If wSheet.Range("R1")="2013 06" Then
'If Range("R1") = "2013 06" Then
Columns("R:T").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("L:N").Select
Selection.Copy
Columns("R:R").Select
ActiveSheet.Paste
Selection.Replace What:="2013 04", Replacement:="2013 06", LookAt:=xlPart _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ElseIf wSheet.Range("R1")<>"2013 06" Then
'ElseIf Range("R1") <> "2013 06" Then
End If
Next wSheet
End Sub
Best Answer
You don't really need to state the
else
, just - do nothing, which means let the loop go to the next sheet. Its also not recommended to use the.Select
method so I have optimized your code a bit. See if this works as intended: