Advertisement
stevennathaniel

Tambah Sheet Excel, Ubah Nama Sheet, Simpan Perubahan

Jan 28th, 2016
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.21 KB | None | 0 0
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement