+ Reply to Thread
Results 1 to 9 of 9

what is the vba reference for newly added sheet

  1. #1
    Forum Contributor
    Join Date
    06-02-2016
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    327

    what is the vba reference for newly added sheet

    Dear All,

    i have added a new sheet to my workbook by the following code.

    ActiveWorkbook.Sheets.Add after:=Worksheets(Worksheets.Count)

    now, what is the reference for that added sheet.

    because I have to code some more with/in the newly added sheet.

    thank you.

  2. #2
    Forum Contributor
    Join Date
    06-02-2016
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    327

    Re: what is the vba reference for newly added sheet

    i'm using the below, but it's not working..

    Sheets(""sheet"&worksheets.count + 1").Select

    help me guys

  3. #3
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: what is the vba reference for newly added sheet

    Hi,

    The new sheet will be activated automatically so you can use ActiveSheet. You can also refer to it by position since you added it as the last sheet using Worksheets(Worksheets.Count).
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  4. #4
    Forum Contributor
    Join Date
    06-02-2016
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    327

    Re: what is the vba reference for newly added sheet

    getting subscript out of range error

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: what is the vba reference for newly added sheet

    Using which code? You must not add 1 to Worksheets.Count.

  6. #6
    Forum Contributor
    Join Date
    06-02-2016
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    327

    Re: what is the vba reference for newly added sheet

    Sub SR()
    ActiveWorkbook.Sheets.Add after:=Worksheets(Worksheets.Count)
    Sheets("stock").Select
    Cells.Select
    Selection.Copy
    Worksheets("sheet" & Worksheets.Count).Select
    Selection.PasteSpecial Paste:=xlPasteValues
    Columns("B:B").EntireColumn.AutoFit
    Rows("1:2").Select
    Selection.delete Shift:=xlUp
    Columns("C:F").Select
    Selection.delete Shift:=xlToLeft
    Columns("D:H").Select
    Selection.delete Shift:=xlToLeft
    Columns("A:A").Select
    Selection.delete Shift:=xlToLeft
    Columns("B:B").Select
    With Selection
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    End With
    Range("A51:B100").Select
    Selection.Cut
    Range("D1").Select
    ActiveSheet.Paste
    Range("A101:B150").Select
    Selection.Cut
    Range("A51").Select
    ActiveSheet.Paste
    Range("A151:B200").Select
    Selection.Cut
    Range("D51").Select
    ActiveSheet.Paste
    Range("A201:B241").Select
    Selection.Cut
    Range("A101").Select
    ActiveSheet.Paste
    Columns("D:D").EntireColumn.AutoFit
    Rows("1:1").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1:E1").Merge
    Range("a1").Select
    With Selection
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    End With
    Range("A1") = "STOCK RECORD FROM " & Worksheets("STOCK").Range("C1").Value & " TO " & Worksheets("STOCK").Range("F1").Value
    End Sub

    -

    please correct the code in line 6

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: what is the vba reference for newly added sheet

    It is, as I posted, simply:
    Please Login or Register  to view this content.
    However it is preferable to avoid selecting objects when using VBA. To wit:
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    06-02-2016
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    327

    Re: what is the vba reference for newly added sheet

    Thanks, it's working brother ..

  9. #9
    Forum Contributor
    Join Date
    06-02-2016
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    327

    Re: what is the vba reference for newly added sheet

    And also thanks for your simplification of the code ..

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 09-06-2005, 12:05 PM
  2. Replies: 0
    Last Post: 09-06-2005, 10:05 AM
  3. Replies: 0
    Last Post: 09-06-2005, 03:05 AM
  4. Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  5. Replies: 0
    Last Post: 09-06-2005, 01:05 AM
  6. Replies: 0
    Last Post: 09-05-2005, 11:05 PM
  7. Replies: 1
    Last Post: 06-23-2005, 03:05 PM
  8. Replies: 1
    Last Post: 06-23-2005, 03:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1