vba event handler in array

roykana

Active Member
Joined
Mar 8, 2018
Messages
311
Office Version
  1. 2010
Platform
  1. Windows
Dear All master,
I want if I make changes in the "master" sheet it automatically changes in the "trans" sheet.

I want a fast code because there are thousands of records in the "trans" sheet.
vba vlookup Change Event in array and dictionary .xlsm
ABCDEFGHIJ
1Supplier NameCODENEW CODECOMBINEITEM DESCNEW PRICEIsiUNIT IN ACTUALPriceUNIT IN KONVERSI
2STOCK OPNAMEBBK580104-00100101AQUA100018,0001yard20,000mtr
3STOCK OPNAMEBBK580204-00100101AQUA20018,001yard20,001mtr
4STOCK OPNAMEBBK580304-00100101AQUA318,002yard20,002mtr
5STOCK OPNAMEBBK580404-00100101AQUA418,003yard20,003mtr
6STOCK OPNAMEBBK580504-00100101AQUA518,004yard20,004mtr
7STOCK OPNAMEBBK580604-00100101AQUA618,004yard20,004mtr
8STOCK OPNAMEBBK580704-00100101AQUA718,004yard20,004mtr
9STOCK OPNAMEBBK580804-00100101AQUA818,004yard20,004mtr
10STOCK OPNAMEBBK580904-00100101AQUA918,004yard20,004mtr
11STOCK OPNAMEBBK581004-00100101AQUA1018,004yard20,004mtr
MASTER
Cell Formulas
RangeFormula
I2:I11I2=MASTER!$F$2:$F$11/0.9


vba vlookup Change Event in array and dictionary .xlsm
ABCDEFGHIJKLMNOPQRS
1KEY COUNTADDRESS1ADDRESS2CITYNOTERESULTRECEIVE ITEM NOSUPPLIER NAMEDATE INCODENEW CODEITEM DESCQTY IN ACTUALUNIT IN ACTUALQTY IN KONVERSIUNIT IN KONVERSINEW PRICE
20ASTOCK OPNAME1GUDANG HOLISGUDANG HOLISBandung74NOT SAME1000STOCK OPNAME19/05/2016BBK58010116002001AQUA100074yard66.60mtr18,000
30ASTOCK OPNAME1GUDANG HOLISGUDANG HOLISBandung74NOT SAME1002STOCK OPNAME20/05/2016BBK58020116002001AQUA20074yard66.60mtr18,001
40ASTOCK OPNAME1GUDANG HOLISGUDANG HOLISBandung74NOT SAME1002STOCK OPNAME20/05/2016BBK58030116002001AQUA350yard45.00mtr18,002
50ASTOCK OPNAME1GUDANG HOLISGUDANG HOLISBandung74NOT SAME1002STOCK OPNAME20/05/2016BBK58040116002001AQUA450yard45.00mtr18,003
60ASTOCK OPNAME1GUDANG HOLISGUDANG HOLISBandung74NOT SAME1002STOCK OPNAME20/05/2016BBK58050116002001AQUA550yard45.00mtr18,004
70ASTOCK OPNAME1GUDANG HOLISGUDANG HOLISBandung74NOT SAME1002STOCK OPNAME20/05/2016BBK58060116002001AQUA650yard45.00mtr18,004
80ASTOCK OPNAME1GUDANG HOLISGUDANG HOLISBandung74NOT SAME1002STOCK OPNAME20/05/2016BBK58070116002001AQUA750yard45.00mtr18,004
TRANS
Cell Formulas
RangeFormula
Q2:Q8Q2=O2*0.9



below is the vba event code in the "trans" sheet
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, c As Range, m, wsM As Worksheet, arr
    
    Set rng = Application.Intersect(Target, Me.Range("L:L"))
    If rng Is Nothing Then Exit Sub
    
    Set wsM = ThisWorkbook.Worksheets("MASTER")
    On Error GoTo bm_Safe_Exit
    Application.EnableEvents = False
    Application.ScreenUpdating = False            'improve performance...
    Application.Calculation = xlCalculationManual
    For Each c In rng.Cells    'process all changed cells in ColB
        v = c.Value
        If Len(v) > 0 Then
            m = Application.Match(c.Value, wsM.Range("B:B"), 0) 'match much faster than Find()
            If Not IsError(m) Then
                arr = wsM.Cells(m, "E").Resize(1, 6) 'one read
                c.Offset(0, 2).Value = arr(1, 1)
                c.Offset(0, 4).Value = arr(1, 4)
                c.Offset(0, 6).Resize(1, 2).Value = Array(arr(1, 6), arr(1, 2))
            End If
        Else
            c.EntireRow.Range("N1,P1,R1:S1").ClearContents 'clear cells if no code
        End If
    Next c

bm_Safe_Exit:
    Application.EnableEvents = True   'be sure to re-enable events
    Application.Calculation = xlCalculationAutomatic 'reset calculation
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
if i understand your code, by changing a L-cell, you want the N,P and R modified.
And quick.
If you change one cell at a time, there is not very much progress to make.

If you change +10 cells at once, then you can read L:Q as an array, do the modifications in that array and write in 3 steps the columns N, P and R. In that way, you only have 4 interactions, if the columns in between have no formulas even in 2 interactions.

Otherwise, in case of 2-10 changes, you can read wsM.Range("B:B") once in an array and use that array in your match-line
 
Upvote 0
if i understand your code, by changing a L-cell, you want the N,P and R modified.
And quick.
If you change one cell at a time, there is not very much progress to make.

If you change +10 cells at once, then you can read L:Q as an array, do the modifications in that array and write in 3 steps the columns N, P and R. In that way, you only have 4 interactions.

Otherwise, in case of 2-10 changes, you can read wsM.Range("B:B") once in an array and use that array in your match-line
@BSALV
Dear Mr. BSALV
Thank you for your reply. i mean have to change event handler in sheet "trans" and also create event handler in sheet "master". Maybe you can help me?

tHANKS
ROYKANA
 
Upvote 0
you only need 1 handler in the "master", the changes in B, and update in "Trans" columns L, N, P and R.
No handler in the "trans".
As i reread your macro now, it's a little bit mixed, both ways, is it working now ?
 
Upvote 0
you only need 1 handler in the "master", the changes in B, and update in "Trans" columns L, N, P and R.
No handler in the "trans".
As i reread your macro now, it's a little bit mixed, both ways, is it working now ?
@BSALV
actually my vba code is an event handler for the "trans" sheet which refers to changes in the n,p,r,s column if it wants to be changed for the better or faster.

As for the event handler in the "trans" sheet, it works accordingly
 
Upvote 0
the tables in master and trans are called "TBL_Master" and 'TBL_Trans"
The macro effects the whole table "TBL_Trans", so do this first in a auxiliary file, in case of errors and to know how quick it is.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
     Dim LoT, LoM, cTcode, cMcode, ArrC, ArrM, ArrT

     t = Timer

     Set LoT = Sheets("trans").ListObjects("TBL_Trans")         'listobject in the "trans"
     Set cTcode = LoT.ListColumns("Code").DataBodyRange         'range ofr code in trans
     Set LoM = Sheets("Master").ListObjects("TBL_Master")       'listobject in "master"
     Set cMcode = LoM.ListColumns("code").DataBodyRange         'range for code in master
     ArrM = cMcode.Resize(, 9).Value                            'read 9 columns of importance in Master
     ArrC = Application.Transpose(Application.Index(ArrM, 0, 1))     '1 array to sarch in from the master
     ArrT = cTcode.Resize(, 8).Value                            'read 8 columns of importance in trans, and in this one, we make the changes

     If Intersect(Target, cTcode) Is Nothing Then Exit Sub

     For i = 1 To UBound(ArrT)                                  'loop through all the rows in Arrt
          r = Application.Match(ArrT(i, 1), ArrC, 0)            'find corresponding row in the master, column "Code"
          If Not IsNumeric(r) Then                              'not found = make these values empty
               ArrT(i, 3) = ""                                  'item desc
               ArrT(i, 5) = ""                                  'units in actual
               ArrT(i, 7) = ""                                  'units in konversi
               MsgBox ArrT(i, 1) & " not found"
          Else                                                  'found
               ArrT(i, 3) = ArrM(r, 4)                          'item desc
               ArrT(i, 5) = ArrM(r, 7)                          'units in actual
               ArrT(i, 7) = ArrM(r, 9)                          'units in konversi
          End If
     Next


     Application.EnableEvents = False
     cTcode.Resize(, 7).Value = ArrT                            'write the 7 columns back to trans (there are NO FORMULAS in these 7 columns !!!
     Application.EnableEvents = True

     MsgBox "done in " & Timer - t & " seconds for " & UBound(ArrT) & " rows"     ' the time to do so for that table in trans
    
End Sub
 
Upvote 0
the tables in master and trans are called "TBL_Master" and 'TBL_Trans"
The macro effects the whole table "TBL_Trans", so do this first in a auxiliary file, in case of errors and to know how quick it is.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
     Dim LoT, LoM, cTcode, cMcode, ArrC, ArrM, ArrT

     t = Timer

     Set LoT = Sheets("trans").ListObjects("TBL_Trans")         'listobject in the "trans"
     Set cTcode = LoT.ListColumns("Code").DataBodyRange         'range ofr code in trans
     Set LoM = Sheets("Master").ListObjects("TBL_Master")       'listobject in "master"
     Set cMcode = LoM.ListColumns("code").DataBodyRange         'range for code in master
     ArrM = cMcode.Resize(, 9).Value                            'read 9 columns of importance in Master
     ArrC = Application.Transpose(Application.Index(ArrM, 0, 1))     '1 array to sarch in from the master
     ArrT = cTcode.Resize(, 8).Value                            'read 8 columns of importance in trans, and in this one, we make the changes

     If Intersect(Target, cTcode) Is Nothing Then Exit Sub

     For i = 1 To UBound(ArrT)                                  'loop through all the rows in Arrt
          r = Application.Match(ArrT(i, 1), ArrC, 0)            'find corresponding row in the master, column "Code"
          If Not IsNumeric(r) Then                              'not found = make these values empty
               ArrT(i, 3) = ""                                  'item desc
               ArrT(i, 5) = ""                                  'units in actual
               ArrT(i, 7) = ""                                  'units in konversi
               MsgBox ArrT(i, 1) & " not found"
          Else                                                  'found
               ArrT(i, 3) = ArrM(r, 4)                          'item desc
               ArrT(i, 5) = ArrM(r, 7)                          'units in actual
               ArrT(i, 7) = ArrM(r, 9)                          'units in konversi
          End If
     Next


     Application.EnableEvents = False
     cTcode.Resize(, 7).Value = ArrT                            'write the 7 columns back to trans (there are NO FORMULAS in these 7 columns !!!
     Application.EnableEvents = True

     MsgBox "done in " & Timer - t & " seconds for " & UBound(ArrT) & " rows"     ' the time to do so for that table in trans
   
End Sub
@BSALV
dear Mr. BSALV,
Thanks for your reply. Sorry I'm late to reply to you.
I try to edit in the sheet "master" in the column "item desc" appears error as below and error in line code below
run-time error 6 overflow
VBA Code:
ArrT = cTcode.Resize(, 8).Value                            'read 8 columns of importance in trans, and in this one, we make the changes

thanks
roykana
 
Upvote 0

Forum statistics

Threads
1,215,438
Messages
6,124,873
Members
449,192
Latest member
MoonDancer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top