Consulting

Results 1 to 13 of 13

Thread: Solved: Add sheet & name

  1. #1
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location

    Solved: Add sheet & name

    hi,

    i currently name a sheet by referencing 2 ranges, however if the sheetname already exists, then this will fail of course.

    how can i save any subsequent sheets?
    my thought would be to append an incrementing number but i don't know how to write the code.

    this is what i have so far
    [vba]
    Sub SaveSheet()
    Sheets("test").Select
    Sheets("test").Copy After:=Sheets("test")
    Sheets("test (2)").Select
    PCode = Sheets("test (2)").Range("A1")
    Ver = Sheets("test (2)").Range("A2")
    Sheets("test (2)").Name = Ver & "~" & PCode
    Sheets(Ver & "~" & PCode).Tab.ColorIndex = 5
    End Sub

    [/vba]


    thanks
    zach

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

    Dim fOK As Boolean
    Dim SheetName As String
    Dim Inc As Long

    SheetName = "Report"
    If SheetExists(SheetName) Then

    Do

    Inc = Inc + 1
    SheetName = "Report (" & Inc & ")"
    fOK = Not SheetExists(SheetName)
    Loop Until fOK
    End If
    Worksheets.Add.Name = SheetName



    '-----------------------------------------------------------------
    Function SheetExists(Sh As String, _
    Optional wb As Workbook) As Boolean
    '-----------------------------------------------------------------
    Dim oWs As Worksheet
    If wb Is Nothing Then Set wb = ActiveWorkbook
    On Error Resume Next
    SheetExists = Not wb.Worksheets(Sh) Is Nothing
    On Error GoTo 0
    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

  3. #3
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Try something like this:
    [VBA]Sub SaveSheet()
    Dim PCode As Range, Ver As Range
    Set PCode = Sheets("test").Range("A1")
    Set Ver = Sheets("test").Range("A2")
    Sheets("test").Copy After:=Sheets("test")
    If Sheets(Ver.Text & " - " & PCode.Value) Is Nothing Then
    ActiveSheet.Name = Ver.Text & " - " & PCode.Value
    ActiveSheet.Tab.ColorIndex = 5
    Else
    Sheets("test").Range("A1").Value = Sheets("test").Range("A1").Value + 1
    ActiveSheet.Name = Ver.Text & " - " & PCode.Value
    ActiveSheet.Tab.ColorIndex = 5
    End If
    End Sub[/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  4. #4
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location

    Bill Jelen has a great podcast demo of this too.


  5. #5
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    xld,

    i'm not able to get this to work correctly.i don't understand how to reference A1 & A2.

    Simon,
    if the sheet does not exist, i get "subscript out of range" here:
    [vba]
    If Sheets(Ver.Text & " - " & PCode.Value) Is Nothing Then
    [/vba]
    and if the sheet exists, i get Run-time error 13 : Type mismatch[vba]Sheets("test").Range("A1").Value = Sheets("test").Range("A1").Value + 1[/vba]


    zach

  6. #6
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    david,
    couldn't view the pod...

    zach

  7. #7
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    OK, sorry you can't get that link to work...


    Here is the code from the show! (attachment)

  8. #8
    Chk out whether this code wud help u out:

    http://www.erlandsendata.no/english/...batextexportwb

  9. #9
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    As long as you used my code in its entirity it works fine! it worked for me ok.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by vzachin
    xld,

    i'm not able to get this to work correctly.i don't understand how to reference A1 & A2.
    Same way that you did before, just build it up piecemeal and pass to the function

    [vba]

    SheetName = Sheets("test (2)").Range("A1") & "~" & Sheets("test (2)").Range("A2")
    If SheetExists(SheetName) Then

    Do

    Inc = Inc + 1
    SheetName = "Report (" & Inc & ")"
    fOK = Not SheetExists(SheetName)
    Loop Until fOK
    End If
    Worksheets.Add.Name = SheetName
    [/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

  11. #11
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    hi simon,

    i tried your code again and still no good. in any event, xld's code works

    thanks again
    zach

  12. #12
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    hi XLD,

    thanks again. works for me now

    zach

  13. #13
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    david & rangadu,

    thanks for the links. it didn't work out for me but i'm sure i can use them in the future

    zach

Posting Permissions

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