Advertisement
deSantoz

InsertUpdateLoad VBA

Jun 14th, 2020
1,418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 10.99 KB | None | 0 0
  1. Function CekIdTransaksi(nomorid As Range) As Boolean
  2.     Dim idtransaksi As String
  3.     idtransaksi = nomorid.Value
  4.     If idtransaksi <> "" Then
  5.         CekIdTransaksi = True
  6.     Else
  7.         CekIdTransaksi = False
  8.     End If
  9. End Function
  10.  
  11.  
  12. Sub SimpanNoCek(ByVal dana As Range, ByVal pencairan As Range, ByVal nomorcek As Range, ByVal tanggalcek As Range, ByVal nilai As Range, ByVal keterangan As Range, ByVal nomorkode As Range, ByVal tabeltujuan As ListObject)
  13.     If CekIdTransaksi(nomorkode) = True Then
  14.         Dim ceknumber As String
  15.         ceknumber = nomorcek.Value
  16.         If ceknumber = vbNullString Then
  17.             MsgBox ("Nomor Cek belum diisi")
  18.             nomorcek.Select
  19.             Exit Sub
  20.         Else
  21.             If IsError(Application.Match(nomorkode.Value, tabeltujuan.ListColumns("KODE").Range, 0)) Then
  22.                 With tabeltujuan.ListRows.Add
  23.                     .Range(1) = nomorkode
  24.                     .Range(2) = dana.Value
  25.                     .Range(3) = pencairan.Value
  26.                     .Range(4) = nomorcek.Value
  27.                     .Range(5) = tanggalcek.Value
  28.                     .Range(6) = nilai.Value
  29.                     .Range(7) = keterangan.Value
  30.                 End With
  31.             Else
  32.                 For i = 1 To tabeltujuan.ListRows.Count
  33.                     If tabeltujuan.ListRows(i).Range(1) = nomorkode Then
  34.                         With tabeltujuan.ListRows(i)
  35.                             .Range(4) = nomorcek.Value
  36.                             .Range(5) = tanggalcek.Value
  37.                             .Range(6) = nilai.Value
  38.                             .Range(7) = keterangan.Value
  39.                         End With
  40.                     End If
  41.                 Next i
  42.             End If
  43.         End If
  44.     Else
  45.         MsgBox ("Dana atau periode pencairan belum diisi")
  46.         nomorid.Select
  47.         Exit Sub
  48.     End If
  49. End Sub
  50.  
  51. Sub LoadDataCek(ByVal kode As Range, ByVal tabelsumber As ListObject, ByRef nocek As Range, ByRef tglcek As Range, ByRef nilai As Range, ByRef ket As Range)
  52.     If CekIdTransaksi(kode) = True Then
  53.         nocek.Value = ""
  54.         tglcek.Value = ""
  55.         nilai.Value = ""
  56.         ket.Value = ""
  57.         If Not tabelsumber.DataBodyRange Is Nothing Then
  58.             For i = 1 To tabelsumber.ListRows.Count
  59.                 If tabelsumber.ListRows(i).Range(1) = kode.Value Then
  60.                     With tabelsumber.ListRows(i)
  61.                         nocek.Value = .Range(4)
  62.                         tglcek.Value = .Range(5)
  63.                         nilai.Value = .Range(6)
  64.                         ket.Value = .Range(7)
  65.                     End With
  66.                     Exit For
  67.                 End If
  68.             Next i
  69.         Else
  70.             MsgBox ("Data Cek bank belum ada")
  71.             Exit Sub
  72.         End If
  73.     End If
  74. End Sub
  75.  
  76. Sub SimpanDataTransaksi(ByVal asal As ListObject, ByVal target As ListObject, ByVal kode As Range)
  77.     Dim tabelasal As ListObject
  78.     Set tabelasal = asal
  79.     Dim tabeltarget As ListObject
  80.     Set tabeltarget = target
  81.    
  82.     Dim kwit As String
  83.     kwit = kode.Value
  84.  
  85.     If IsError(Application.Match(kwit, tabeltarget.ListColumns("KODE").Range, 0)) Then
  86.         'MsgBox ("Data belum ada, data kwit ditambahkan")
  87.         For i = 1 To tabelasal.ListRows.Count
  88.             If tabelasal.ListRows(i).Range(1) = vbNullString Then Exit Sub
  89.             If IsError(Application.Match(tabelasal.ListRows(i).Range(1), tabeltarget.ListColumns("ID").Range, 0)) Then
  90.                 'MsgBox ("data kwit belum ada, Data ID belum ada")
  91.                 Dim newrow As ListRow
  92.                 Set newrow = tabeltarget.ListRows.Add
  93.                 With newrow
  94.                     .Range(1) = tabelasal.ListRows(i).Range(1)
  95.                     .Range(2) = kwit
  96.                     .Range(3) = tabelasal.ListRows(i).Range(3)
  97.                     .Range(4) = tabelasal.ListRows(i).Range(4)
  98.                     .Range(5) = tabelasal.ListRows(i).Range(5)
  99.                     .Range(6) = tabelasal.ListRows(i).Range(6)
  100.                     .Range(7) = tabelasal.ListRows(i).Range(7)
  101.                     .Range(8) = tabelasal.ListRows(i).Range(8)
  102.                     .Range(9) = tabelasal.ListRows(i).Range(9)
  103.                     .Range(10) = tabelasal.ListRows(i).Range(10)
  104.                     .Range(11) = tabelasal.ListRows(i).Range(11)
  105.                     .Range(12) = tabelasal.ListRows(i).Range(12)
  106.                     .Range(13) = tabelasal.ListRows(i).Range(13)
  107.                     .Range(14) = tabelasal.ListRows(i).Range(14)
  108.                     .Range(15) = tabelasal.ListRows(i).Range(15)
  109.                     .Range(16) = tabelasal.ListRows(i).Range(16)
  110.                 End With
  111.             Else
  112.                'MsgBox ("data kwit belum ada, Data id sudah ada")
  113.                Exit Sub
  114.             End If
  115.         Next i
  116.     Else
  117.         'MsgBox ("Data Kwit sudah ada, tambahkan data ID")
  118.         For l = 1 To tabelasal.ListRows.Count
  119.             If IsError(Application.Match(tabelasal.ListRows(l).Range(1), tabeltarget.ListColumns("ID").Range, 0)) Then
  120.                 MsgBox ("data kwit sudah ada, Data id belum ada")
  121.                 Dim nexrow As ListRow
  122.                 Set nexrow = tabeltarget.ListRows.Add
  123.                 With nexrow
  124.                     .Range(1) = tabelasal.ListRows(l).Range(1)
  125.                     .Range(2) = kwit
  126.                     .Range(3) = tabelasal.ListRows(l).Range(3)
  127.                     .Range(4) = tabelasal.ListRows(l).Range(4)
  128.                     .Range(5) = tabelasal.ListRows(l).Range(5)
  129.                     .Range(6) = tabelasal.ListRows(l).Range(6)
  130.                     .Range(7) = tabelasal.ListRows(l).Range(7)
  131.                     .Range(8) = tabelasal.ListRows(l).Range(8)
  132.                     .Range(9) = tabelasal.ListRows(l).Range(9)
  133.                     .Range(10) = tabelasal.ListRows(l).Range(10)
  134.                     .Range(11) = tabelasal.ListRows(l).Range(11)
  135.                     .Range(12) = tabelasal.ListRows(l).Range(12)
  136.                     .Range(13) = tabelasal.ListRows(l).Range(13)
  137.                     .Range(14) = tabelasal.ListRows(l).Range(14)
  138.                     .Range(15) = tabelasal.ListRows(l).Range(15)
  139.                     .Range(16) = tabelasal.ListRows(l).Range(16)
  140.                 End With
  141.                
  142.             Else
  143.             'MsgBox (" Data kwit sudah ada, Data id sudah ada")
  144.             For j = 1 To tabeltarget.ListRows.Count
  145.                 If tabelasal.ListRows(l).Range(1) = tabeltarget.ListRows(j).Range(1) Then
  146.                     With tabeltarget.ListRows(j)
  147.                         .Range(3) = tabelasal.ListRows(l).Range(3)
  148.                         .Range(4) = tabelasal.ListRows(l).Range(4)
  149.                         .Range(5) = tabelasal.ListRows(l).Range(5)
  150.                         .Range(6) = tabelasal.ListRows(l).Range(6)
  151.                         .Range(7) = tabelasal.ListRows(l).Range(7)
  152.                         .Range(8) = tabelasal.ListRows(l).Range(8)
  153.                         .Range(9) = tabelasal.ListRows(l).Range(9)
  154.                         .Range(10) = tabelasal.ListRows(l).Range(10)
  155.                         .Range(11) = tabelasal.ListRows(l).Range(11)
  156.                         .Range(12) = tabelasal.ListRows(l).Range(12)
  157.                         .Range(13) = tabelasal.ListRows(l).Range(13)
  158.                         .Range(14) = tabelasal.ListRows(l).Range(14)
  159.                         .Range(15) = tabelasal.ListRows(l).Range(15)
  160.                         .Range(16) = tabelasal.ListRows(l).Range(16)
  161.                         End With
  162.                     Exit For
  163.                 End If
  164.                Next j
  165.             End If
  166.         Next l
  167.     End If
  168.    
  169. 'cleanup
  170.     For k = 1 To tabelasal.ListRows.Count
  171.         If tabelasal.ListRows(k).Range(4) = vbNullString Then
  172.             Dim rowtodelete As Long
  173.             rowtodelete = Application.Match(tabelasal.ListRows(k).Range(1), tabeltarget.ListColumns("ID").Range, 0)
  174.             MsgBox (rowtodelete)
  175.             If rowtodelete > 1 Then
  176.                 tabeltarget.ListRows(rowtodelete - 1).Delete
  177.             End If
  178.         End If
  179.     Next k
  180.    
  181.     tabeltarget.Sort.SortFields.Clear
  182.     tabeltarget.Sort.SortFields.Add2 _
  183.         Key:=tabeltarget.ListColumns(1).Range, SortOn:=xlSortOnValues, Order:= _
  184.         xlAscending, DataOption:=xlSortTextAsNumbers
  185.     With tabeltarget.Sort
  186.         .Header = xlYes
  187.         .MatchCase = False
  188.         .Orientation = xlTopToBottom
  189.         .SortMethod = xlPinYin
  190.         .Apply
  191.     End With
  192.    
  193.    
  194. End Sub
  195.  
  196. Sub LoadDataTransaksi(ByVal asal As ListObject, ByVal target As ListObject, ByVal id As Range)
  197.    
  198.  
  199.     Dim tabelasal As ListObject
  200.     Set tabelasal = asal
  201.     Dim tabeltarget As ListObject
  202.     Set tabeltarget = target
  203.    
  204.     Application.ScreenUpdating = False
  205.    
  206.     'If Not Intersect(Target, Range("D1")) Is Nothing Then
  207.         If id.Value <> "" Then
  208.              
  209.             If Not tabelasal.DataBodyRange Is Nothing Then
  210.                 tabelasal.DataBodyRange.Delete
  211.             End If
  212.            
  213.             Dim cekrow, jumlahrow, totalrow As Long
  214.             If Not IsError(Application.Match(id.Value, tabeltarget.ListColumns("KODE").Range, 0)) Then
  215.                 cekrow = Application.Match(id.Value, tabeltarget.ListColumns("KODE").Range, 0) - 1
  216.                 jumlahrow = Application.CountIf(tabeltarget.ListColumns("KODE").Range, id.Value)
  217.                 totalrow = cekrow + jumlahrow - 1
  218.             Else
  219.                 Exit Sub
  220.             End If
  221.            
  222.             For i = cekrow To totalrow
  223.             'For i = 1 To tabeltarget.ListRows.Count
  224.                 'MsgBox (i)
  225.                 If tabeltarget.ListRows(i).Range(2) = id.Value Then
  226.                     With tabelasal.ListRows.Add
  227.                         .Range(1) = tabeltarget.ListRows(i).Range(1)
  228.                         .Range(2) = tabeltarget.ListRows(i).Range(2)
  229.                         .Range(3) = tabeltarget.ListRows(i).Range(3)
  230.                         .Range(4) = tabeltarget.ListRows(i).Range(4)
  231.                         .Range(5) = tabeltarget.ListRows(i).Range(5)
  232.                         .Range(6) = tabeltarget.ListRows(i).Range(6)
  233.                         .Range(7) = tabeltarget.ListRows(i).Range(7)
  234.                         .Range(8) = tabeltarget.ListRows(i).Range(8)
  235.                         .Range(9) = tabeltarget.ListRows(i).Range(9)
  236.                         .Range(10) = tabeltarget.ListRows(i).Range(10)
  237.                         .Range(11) = tabeltarget.ListRows(i).Range(11)
  238.                         .Range(12) = tabeltarget.ListRows(i).Range(12)
  239.                         .Range(13) = tabeltarget.ListRows(i).Range(13)
  240.                         .Range(14) = tabeltarget.ListRows(i).Range(14)
  241.                         .Range(15) = tabeltarget.ListRows(i).Range(15)
  242.                         .Range(16) = tabeltarget.ListRows(i).Range(16)
  243.                     End With
  244.                 End If
  245.             Next i
  246.         End If
  247.     'End If
  248.     Application.ScreenUpdating = True
  249.  
  250. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement