Advertisement
eeperry

LibreOffice Basic: Copy Sheet function

Sep 29th, 2012
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '**************** CopySheet ********************
  2. 'copies the contents of the active sheet
  3. 'to a new sheet, named by the user, and placed as
  4. 'the last sheet in the workbook.
  5. '***********************************************
  6. Sub CopySheet
  7.     dim Sheet1 as String
  8.     dim Sheet2 as String
  9.     dim Doc as Object
  10.     dim NewSheet as Object
  11.  
  12.     Doc = ThisComponent
  13.    
  14.     'This checks to make sure that Doc is a spreadsheet
  15.     If NOT Doc.supportsService("com.sun.star.sheet.SpreadsheetDocument") then
  16.         MsgBox "This Macro Only Works with Calc Spreadsheets"
  17.         Exit Sub
  18.     End If
  19.    
  20.     Sheet1 = Doc.CurrentController.ActiveSheet.Name
  21.     Sheet2 = InputBox("Enter Name for Copied Sheet:", "Copy Sheet", Sheet1)
  22.    
  23.     If Sheet2 = "" Then Exit Sub
  24.    
  25.     Do While Doc.Sheets.hasByName(Sheet2)
  26.         Sheet2 = InputBox(Sheet2 + " already exist, select a different name:", "Copy Sheet", Sheet2 + "2")
  27.         If Sheet2 = "" Then Exit Sub
  28.     Loop
  29.    
  30.     Doc.Sheets.CopyByName(Sheet1, Sheet2, Doc.Sheets.Count)
  31.    
  32.     NewSheet = Doc.Sheets.getByName(Sheet2)
  33.     Doc.CurrentController.setActiveSheet(NewSheet)
  34. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement