+ Reply to Thread
Results 1 to 15 of 15

VBA code adding sheets need to be modified

  1. #1
    Forum Contributor
    Join Date
    12-01-2022
    Location
    Rhodes,Greece
    MS-Off Ver
    office 2021
    Posts
    204

    VBA code adding sheets need to be modified

    This is a vba code with an input box.if I run it and put a number then I will get the number of the sheets that I have put.the sheets will go before the active sheet.

    Sub AddSheets()

    Dim ShtCount As Integer, i As Integer

    ShtCount = Application.InputBox("HOW MANY SHEETS DO YOU WANT TO ADD?", "NUMBER OF SHEETS", , , , , , 1)

    If ShtCount = False Then
    Exit Sub
    Else
    For i = 1 To ShtCount
    Worksheets.Add
    Next i
    End If

    End Sub

    How can I modify the code so the sheets that will be added come after the active sheet?
    Thank you in advance

  2. #2
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,481

    Re: VBA code adding sheets need to be modified

    PHP Code: 
    Worksheets.Add after:=activesheet 
    Quang PT

  3. #3
    Forum Contributor
    Join Date
    12-01-2022
    Location
    Rhodes,Greece
    MS-Off Ver
    office 2021
    Posts
    204

    Re: VBA code adding sheets need to be modified

    Quote Originally Posted by bebo021999 View Post
    PHP Code: 
    Worksheets.Add after:=activesheet 
    worked perfectly.thank you very much

  4. #4
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA code adding sheets need to be modified

    Hi kvemperor
    You can shorten your code and avoid the For-Next loop as follows:

    PHP Code: 
    Sub AddSheets()
    Dim ShtCount As Integer
    ShtCount 
    Application.InputBox("HOW MANY SHEETS DO YOU WANT TO ADD?""NUMBER OF SHEETS", , , , , , 1)
    If 
    ShtCount 0 Then Exit Sub Else Worksheets.Add ActiveSheet, , ShtCount
    End Sub 
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

  5. #5
    Forum Contributor
    Join Date
    12-01-2022
    Location
    Rhodes,Greece
    MS-Off Ver
    office 2021
    Posts
    204

    Re: VBA code adding sheets need to be modified

    Quote Originally Posted by beyond Excel View Post
    Hi kvemperor
    You can shorten your code and avoid the For-Next loop as follows:

    PHP Code: 
    Sub AddSheets()
    Dim ShtCount As Integer
    ShtCount 
    Application.InputBox("HOW MANY SHEETS DO YOU WANT TO ADD?""NUMBER OF SHEETS", , , , , , 1)
    If 
    ShtCount 0 Then Exit Sub Else Worksheets.Add ActiveSheet, , ShtCount
    End Sub 
    thank you very much for your response .i am doing smth wrong?you code is not working.i did copy/paste

  6. #6
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA code adding sheets need to be modified

    Without seeing your implementation, it's a bit hard to guess where you've gone wrong.

    But compare yourself with what follows and comment:


    \_________________/

  7. #7
    Forum Contributor
    Join Date
    12-01-2022
    Location
    Rhodes,Greece
    MS-Off Ver
    office 2021
    Posts
    204

    Re: VBA code adding sheets need to be modified

    Quote Originally Posted by beyond Excel View Post
    Without seeing your implementation, it's a bit hard to guess where you've gone wrong.

    But compare yourself with what follows and comment:


    \_________________/
    thank you very much for you effort.in your sample is working ok but in mine it adds only one sheet ,whatever number i put.do you think it gets confused because of all the codes?
    Attached Files Attached Files

  8. #8
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA code adding sheets need to be modified

    I did not suggest this:
    Please Login or Register  to view this content.
    Take a good look at my suggestion... Better copy...

  9. #9
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA code adding sheets need to be modified

    Quote Originally Posted by kvemperor View Post
    thank you very much for you effort.in your sample is working ok but in mine it adds only one sheet ,whatever number i put.do you think it gets confused because of all the codes?
    Have you already realized what your 'small' but significant mistake is?...

  10. #10
    Forum Contributor
    Join Date
    12-01-2022
    Location
    Rhodes,Greece
    MS-Off Ver
    office 2021
    Posts
    204

    Re: VBA code adding sheets need to be modified

    Quote Originally Posted by beyond Excel View Post
    I did not suggest this:
    Please Login or Register  to view this content.
    Take a good look at my suggestion... Better copy...
    hallo from sunny Greece.still same thing .only one sheet adds again unfortunately ,whatever number i put.but is ok.is working the previous code with the loop so no worries

  11. #11
    Forum Contributor
    Join Date
    12-01-2022
    Location
    Rhodes,Greece
    MS-Off Ver
    office 2021
    Posts
    204

    Re: VBA code adding sheets need to be modified

    Quote Originally Posted by beyond Excel View Post
    Have you already realized what your 'small' but significant mistake is?...
    no sorry

  12. #12
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA code adding sheets need to be modified

    You seriously don't see the difference between:
    If ShtCount = 0 Then Exit Sub Else Worksheets.Add ActiveSheet, , ShtCount

    And this other:
    If ShtCount = 0 Then Exit Sub Else Worksheets.Add ActiveSheet

  13. #13
    Forum Contributor
    Join Date
    12-01-2022
    Location
    Rhodes,Greece
    MS-Off Ver
    office 2021
    Posts
    204

    Re: VBA code adding sheets need to be modified

    Quote Originally Posted by beyond Excel View Post
    You seriously don't see the difference between:
    If ShtCount = 0 Then Exit Sub Else Worksheets.Add ActiveSheet, , ShtCount

    And this other:
    If ShtCount = 0 Then Exit Sub Else Worksheets.Add ActiveSheet
    No i didnt see it.my bad

  14. #14
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA code adding sheets need to be modified

    Quote Originally Posted by kvemperor View Post
    No i didnt see it.my bad
    Ok... Until next time.

  15. #15
    Forum Contributor
    Join Date
    12-01-2022
    Location
    Rhodes,Greece
    MS-Off Ver
    office 2021
    Posts
    204

    Re: VBA code adding sheets need to be modified

    Quote Originally Posted by beyond Excel View Post
    Ok... Until next time.
    thank you brother for all your effort

+ 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. [SOLVED] modified code loop throughout multiple sheets instead of one sheet
    By tubrak in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-18-2021, 06:17 AM
  2. [SOLVED] modified code or alternative to sum values across multiple sheets based on three columns
    By MKLAQ in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 12-12-2021, 03:31 AM
  3. modified code mr .jindon merge duplicated data across multiple sheets(update inventory)
    By abdo meghari in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-02-2021, 03:54 AM
  4. Slowness issue with macro code (adding sheets in excel 2013)
    By vibhor in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-16-2017, 08:43 PM
  5. Adding sheets on my payslip printing code
    By Zoeyzack in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-17-2016, 07:06 AM
  6. VBA Code for Adding % Diff Column to Numerous Sheets?
    By kestefon in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-21-2013, 01:50 PM
  7. Adding in BODY: Author and Last Modified Date (Automatically Updates)
    By T86157 in forum Word Formatting & General
    Replies: 1
    Last Post: 06-02-2012, 06:22 AM

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