SHARE
TWEET

Tambah Sheet Excel, Ubah Nama Sheet, Simpan Perubahan

stevennathaniel Jan 28th, 2016 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports Excel = Microsoft.Office.Interop.Excel
  2.  
  3. Imports Microsoft.Office
  4.  
  5. Public Class Form9
  6.  
  7.     Dim appXL As Excel.Application = Nothing
  8.  
  9.     Dim wbXL As Excel.Workbook = Nothing
  10.  
  11.     Dim shXL As Excel.Worksheet = Nothing
  12.  
  13.     Dim raXL As Excel.Range = Nothing
  14.  
  15.     Dim sheetBaru As Excel.Worksheet
  16.  
  17.     Dim nomerSheet As Integer = 3
  18.  
  19.     Dim namaSheetBaru As String = "Kertas Baru"
  20.  
  21.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  22.  
  23.         appXL = New Excel.Application
  24.  
  25.         wbXL = appXL.Workbooks.Open("D:\fileExcel\Latihan100.xlsx")
  26.  
  27.         appXL.Visible = True
  28.  
  29.         With wbXL
  30.  
  31.  
  32.  
  33.  
  34.             'Menambahkan sheet baru ke workbook
  35.             .Worksheets.Add(After:=.Worksheets(nomerSheet))
  36.  
  37.         End With
  38.  
  39.         shXL = wbXL.Worksheets(4)
  40.  
  41.         shXL.Name = namaSheetBaru
  42.  
  43.         'Simpan perubahan di file excel
  44.         wbXL.Save()
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.         appXL.UserControl = True
  52.  
  53.         raXL = Nothing
  54.  
  55.         wbXL = Nothing
  56.  
  57.         shXL = Nothing
  58.  
  59.         appXL = Nothing
  60.  
  61.         Exit Sub
  62.  
  63. Err_Handler:
  64.  
  65.         MsgBox(Err.Description, vbCritical, "Error: " & Err.Number)
  66.  
  67.  
  68.  
  69.     End Sub
  70. End Class
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top