Sunday, November 14, 2010

Macro to add new worksheet


If you want to add any new sheet using VBA. Try this code -



Sub Macro_to_add_new_sheet()
    Dim wk As Worksheet
    Dim newsheet As String
    
   'change sheet name here
    newsheet = "Ashish"
   
    'Check if sheet already exists
    For Each wk In ThisWorkbook.Sheets
    If UCase(wk.Name) = UCase(newsheet) Then
        MsgBox "Sheet already exits. Please  choose any other name"
        Exit Sub
    End If
    Next
    
    'Add new sheet at the end
    Sheets.Add after:=Sheets(Sheets.Count)
    'Rename the new sheet
    ActiveSheet.Name = newsheet

End Sub


Steps to Use
  • Copy the below code
  • Press Alt+F11 to open VBA editor
  • Paste it in any public module or module 1
  • Change sheet name and run the macro


No comments:

Post a Comment

Import data from SQL

Macro to import data from SQL using ADO connection string: Sub Import_data_from_SQL() ' Tools -> References -> Microsoft Active...