Consulting

Results 1 to 11 of 11

Thread: Solved: use input box chara to create new woorksheet name

  1. #1
    VBAX Regular
    Joined
    May 2008
    Posts
    11
    Location

    Solved: use input box chara to create new woorksheet name

    Hi everyone,

    i am working on a vba for a excel, which if user need to create new worksheet with inputbox chara.
    which means whatever is key into the input box, it will be the new worksheet name ..

    anyone can help?

    Thanks

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    a simple code:

    [VBA]
    Sub add_sht()
    Sheets.Add.Name = InputBox("Please Enter the Name of the Worksheet")
    End Sub[/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    to add the worksheet after last sheet:

    [vba]
    Sub add_sht()
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = _
    InputBox("Please Enter the Name of the Worksheet")
    End Sub
    [/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Regular
    Joined
    May 2008
    Posts
    11
    Location
    Quote Originally Posted by mancubus
    to add the worksheet after last sheet:

    [vba]
    Sub add_sht()
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = _
    InputBox("Please Enter the Name of the Worksheet")
    End Sub
    [/vba]
    Thanks mancubus, this code works fine, however when click on cancel,there is error msg..
    and if the input box is empty ( no input from user) or same input twice, it will also show error
    is there a way to improve it?

    hope you can help..

    Thanks

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    sure. try this.

    [vba]
    Sub add_sht()
    Dim sht_name As Variant
    On Error Resume Next
    sht_name = InputBox("Please Enter the Name of the Worksheet")
    If sht_name <> "" Then
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sht_name
    Else
    MsgBox "Cancelled / Blank"
    End If
    End Sub

    [/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub AddSheet()
    Dim shName As String
    shName = InputBox("Please Enter the Name of the Worksheet")
    If shName <> "" Then

    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = shName
    End If
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i added control for checking the sheet name entered by user already exists.

    [vba]
    Sub add_sht()

    Dim sht_name As Variant
    Dim sht As Worksheet

    On Error Resume Next
    sht_name = InputBox("Please Enter the Name of the Worksheet")
    If Len(sht_name) > 0 Then
    For Each sht In ThisWorkbook.Worksheets
    If StrComp(sht.Name, sht_name) = 0 Then
    MsgBox "Sheet already exists!", vbCritical, "Warning!"
    Exit Sub
    End If
    Next sht
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sht_name
    Else
    MsgBox "Cancelled / Blank"
    End If
    End Sub
    [/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    @xld:

    is it better to declare the variable for worksheet name as "string"?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I think so mancubus, as it can only be a string. Always better to be specific IMO.

    I would use a simple function for checking if sheet already exists, rather than a loop, like so

    [vba]

    Public Sub AddSheet()
    Dim shName As String
    Dim shExists As Boolean

    Do

    shName = InputBox("Please Enter the Name of the Worksheet")
    If shName <> "" Then

    shExists = SheetExists(shName)
    If Not shExists Then

    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = shName
    Else

    MsgBox "Worksheet " & shName & " already exists", vbOKOnly + vbInformation, "Add Sheet"
    End If
    End If
    Loop Until Not shExists Or shName = ""
    End Sub

    Private Function SheetExists(ByVal SheetName As String, _
    Optional ByVal wb As Workbook)

    If wb Is Nothing Then Set wb = ActiveWorkbook
    On Error Resume Next
    SheetExists = Not wb.Worksheets(SheetName) Is Nothing

    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    thanks.

    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  11. #11
    VBAX Regular
    Joined
    May 2008
    Posts
    11
    Location
    so sorry did not check back on the msg.. but some how i got it..
    actually i was doing some for of tracking of sale using VBA
    it will allows user to key in new customer and update RFQ Number, quotation No, part number .. etc in row A5... stuff like that.. anyway it ok no.. i am not a VBA user by nature.. so i mostly copy and paste using others VBA code.. so the file is quite messy ... and at 1 point of time it is like 39mb hahaha.. now reduce to 300kb ++.. this is a great web for VBA newbie like me to learn and improve .. thanks guys for the help..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •