Use nested with statement : With « Language Basics « VBA / Excel / Access / Word






Use nested with statement

 

Sub NewWorkbook4()
  Dim myWorkbook As Workbook, myWorksheet As Worksheet

  Set myWorkbook = Workbooks.Add
  With myWorkbook
    Set myWorksheet = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
    With myWorksheet
      .Name = "January"
      .Range("A1").Value = "Sales Data"
    End With
    .SaveAs Filename:="JanSales.xls"
  End With
End Sub

 








Related examples in the same category

1.The With/End With Structure
2.Use a With...End With construct with fully qualified properties