dave3009

GuaravGarg2

May 24th, 2020
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. ' Define the maximum number of sheets you suggest,
  4. ' Excel is limited by memory, depending on you available
  5. ' system resources and version you want to limit this
  6. Const MaxCopies As Long = 50
  7.  
  8. Sub CopysheetMultipleTimes()
  9. Dim NoOfCopies As Variant
  10. Dim pCounter As Integer
  11. Dim tWB As Workbook
  12. Dim sh As Worksheet
  13. Dim TooMany As VbMsgBoxResult
  14. Dim msg As String
  15. Dim OKGo As Boolean
  16.  
  17. xlSettings False ' turn off xl settings
  18.  
  19. Set tWB = ThisWorkbook ' we are using thisworkbook
  20. Set sh = tWB.ActiveSheet ' we want to copy activesheet
  21.  
  22. OKGo = True ' set this to true now, will change if too many sheets are requested
  23. ' create a messge
  24. msg = "Excel is limited by resources!" & vbNewLine
  25. msg = msg & "Are you sure you want to add |SHEETS| copies?"
  26.  
  27. ' let user request number of copies
  28. StartPoint:
  29. NoOfCopies = Application.InputBox("How many copies do you what?", "Rochem Inspectors", , , , , 1)
  30.  
  31.  
  32.     If NoOfCopies <> False Then ' test if x on inputbox has been pressed
  33.        If NoOfCopies > MaxCopies Then
  34.             TooMany = MsgBox(Replace(msg, "|SHEETS|", NoOfCopies), vbYesNo + vbCritical, "Too Many Sheets")
  35.             If TooMany = vbNo Then GoTo StartPoint
  36.         End If
  37.         pCounter = 0
  38.         Do ' create the desired number of sheets
  39.            sh.Copy After:=tWB.Sheets(sh.Name)
  40.             pCounter = pCounter + 1
  41.         Loop Until pCounter = NoOfCopies
  42.     Else
  43.         ' x was pressed, give user feedback
  44.        MsgBox "Copy Was Cancelled", , "Rochem Inspectors"
  45.     End If
  46. xlSettings True ' restore xl settings
  47. End Sub
  48.  
  49. Sub xlSettings(t As Boolean) ' sets alerts and udating to desired setting
  50. With Application
  51.     .ScreenUpdating = t
  52.     .DisplayAlerts = t
  53. End With
  54. End Sub
Add Comment
Please, Sign In to add comment