copy data based on part of item when matching more than 34 sheets and invert value for one item

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
415
Office Version
  1. 2016
Platform
  1. Windows
Hi experts ,
I need macro for about 4000 rows for each sheet and about more than 34 sheets when matching part of item across sheets
so the parts of items waht I want matching across sheets are
(CCS DEL,MASROUFA FGRTERE,SSMT TRYU,PTT REF)
then should copy data to FTT sheet start arrainging date fro old to last with sort items from A-Z and invert the value for just SSMT TRYU from column D to column C and delete any characters before or after parts of items as show the result in FFT sheet and should deal any new data or change in others sheets without repeat copying data have already copied.
MK.xlsm
ABCD
1DATEIDBUYINGSELLING
2
3
4
5
6
7
8
9
FTT


MK.xlsm
ABCD
1DATEIDBUYINGSELLING
207/20/2023CCS DEL CCR1-CMB1 G80000233
307/20/2023CCCH Y8888 HJJJ 7000LM123
402/21/2023CCS DEL CVBBBB20000 NMUI 345000124
502/21/2023CVBBBB20000 NMUI 3450011232
602/21/2023CVBBBB20000 NMUI 345002443
702/21/2023MASROUFA FGRTERE 80000 JKHHGTTT1004
802/22/2023HGJGG PTT REF FGRTERE 90000 455566
902/23/2023SSMT TRYU CCR1-CMB8BB NMJ7000700
1002/24/2023PTT REF MNDFHHHH 50000 JK90088
ASH



MK.xlsm
ABCD
1DATEIDBUYINGSELLING
207/20/2023 CCS DEL CCR1-CMB1 G18000050
307/20/2023CCCH Y8888 HJJJ 17000LM123
407/20/2023CVBBBB20000 123NMUI 34500011
507/20/2023CVBBBB20000 NMUIGH34 345001222
602/21/2023TRYCVBBBB20000 NMUI 345002113
702/21/2023SSMT TRYU NMJFRTERE 80000 JKHHGTTT1004
802/21/2023MTHYHGJGG FGRTERE 90000 455523
902/21/2023ASDFFCCR1-CMB8BB NMJ700077
1002/21/2023MNDFHHHH 50000 JK900 PTTTR50023
GHF2


result should be

MK.xlsm
ABCD
1DATEIDBUYINGSELLING
207/20/2023CCS DEL233
307/20/2023CCS DEL50
402/21/2023CCS DEL124
502/21/2023MASROUFA FGRTERE4
602/21/2023SSMT TRYU4
702/22/2023PTT REF66
802/23/2023SSMT TRYU700
902/24/2023PTT REF88
FTT
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
and should deal any new data or change in others sheets without repeat copying data have already copied.
I don't understand this part, can you give an example?
 
Upvote 0
Hi,
in other meaning : should clear data in FTT sheet before bring data when run the macro every time to make sure for prevention to repeat copying data have already copied every time running the macro .
 
Upvote 0
Book2
ABCDEFG
1DATEIDBUYINGSELLINGCCS DEL
27/20/23CCS DEL233MASROUFA FGRTERE
37/20/23CCS DEL50SSMT TRYU
42/21/23CCS DEL124PTT REF
52/21/23MASROUFA FGRTERE4
62/24/23PTT REF88
72/22/23PTT REF66
82/23/23SSMT TRYU700
92/21/23SSMT TRYU4
FTT


FYR,

Please give a shot @MKLAQ, In order to run the code, you must add list keyword in sheets FTT column G
1690100220557.png


VBA Code:
Option Compare Text
Option Explicit
Sub test()
'Dim dict As New Dictionary
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
dict.CompareMode = vbTextCompare
Dim ws As Worksheet
Set ws = Sheets("FTT")
Dim b As Variant, arrwords As Variant, a As Variant, aword As Variant
Dim K%, i%, c%

ReDim b(1 To 100000, 1 To 4)
'store column G key words
With ws
    arrwords = .Range("g1:g" & .Cells(Rows.Count, "g").End(xlUp).Row).Value
End With

'Loop through Workbook Worksheets
For K = 1 To Worksheets.Count
    If Sheets(K).Name <> "FTT" Then
        With Sheets(K)
            a = .Range("a2:d" & .Cells(Rows.Count, "a").End(xlUp).Row).Value 'Store Sheets Value to array
          
                For i = 1 To UBound(a, 1)
                    For Each aword In arrwords 'loop through each keyword
                        If InStr(a(i, 2), aword) > 0 Then 'If found then row 2 putkeyword
                            c = c + 1
                            b(c, 1) = a(i, 1)
                            b(c, 2) = aword
                            b(c, 3) = a(i, 3)
                            b(c, 4) = a(i, 4)
                            Exit For
                        End If
                    Next
                Next i
        End With
    End If
Next K

With ws 'Sheet FTT
    .[a2:d500000].ClearContents
    .[a2].Resize(UBound(b, 1), UBound(b, 2)).Value = b
    .[a2].Sort Key1:=Range("b2"), key2:=Range("a2"), order1:=xlAscending, order2:=xlDescending, Header:=xlYes
End With

End Sub
 
Upvote 0
Adjustment:

VBA Code:
Option Compare Text
Option Explicit
Sub test()
'Dim dict As New Dictionary
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
dict.CompareMode = vbTextCompare
Dim ws As Worksheet
Set ws = Sheets("FTT")
Dim b As Variant, arrwords As Variant, a As Variant, aword As Variant
Dim K%, i%, c%

ReDim b(1 To 100000, 1 To 4)

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

'store column G key words
With ws
    arrwords = .Range("g1:g" & .Cells(Rows.Count, "g").End(xlUp).Row).Value
End With

'Loop through Workbook Worksheets
For K = 1 To Worksheets.Count
    If Sheets(K).Name <> "FTT" Then
        With Sheets(K)
            a = .Range("a2:d" & .Cells(Rows.Count, "a").End(xlUp).Row).Value 'Store Sheets Value to array
         
                For i = 1 To UBound(a, 1)
                    For Each aword In arrwords 'loop through each keyword
                        If InStr(a(i, 2), aword) > 0 Then 'If found then row 2 putkeyword
                            c = c + 1
                            b(c, 1) = a(i, 1)
                            b(c, 2) = aword
                            b(c, 3) = a(i, 3)
                            b(c, 4) = a(i, 4)
                            Exit For
                        End If
                    Next
                Next i
        End With
    End If
Next K

With ws 'Sheet FTT
    .[a2:d500000].ClearContents
    .[a2].Resize(UBound(b, 1), UBound(b, 2)).Value = b
    .[a2].Sort Key1:=Range("b2"), key2:=Range("a2"), order1:=xlAscending, order2:=xlDescending, Header:=xlYes
End With

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub
 
Upvote 0
@RudRud
your code is great!
I have two things
I would for you to see in result for row6,8 is not as what i want and I said
and invert the value for just SSMT TRYU from column D to column C
second I'm surprised from your code is not fast with simple data as in OP . seem slight slow , despite of use array & dic :unsure:
now I'm not at work to test with multiple sheets and much rows , but I will test it tomorrow at work and see how goes.;)
but the running speed gives 1.00 sec with simple data :rolleyes:
thank you .:)
 
Upvote 0
Book2.xlsb
ABCD
1DATEIDBUYINGSELLING
27/20/23CCS DEL233
37/20/23CCS DEL50
42/21/23CCS DEL124
52/21/23MASROUFA FGRTERE4
62/24/23PTT REF88
72/22/23PTT REF66
82/23/23SSMT TRYU700
92/21/23SSMT TRYU4
FTT


Hi @MKLAQ, I've changed ssmt tryu from d to c.

Actually I'm only working with array with Instr, that's why take more time to execute the code,

Hopefully there's other gurus can provide other solutions which's more efficiency

VBA Code:
Option Compare Text
Option Explicit
Sub test()
'Dim dict As New Dictionary
'Dim dict As Object
'Set dict = CreateObject("Scripting.Dictionary")
'dict.CompareMode = vbTextCompare
Dim ws As Worksheet
Set ws = Sheets("FTT")
Dim b As Variant, arrwords As Variant, a As Variant, aword As Variant
Dim K%, i%, c%

ReDim b(1 To 100000, 1 To 4)

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

'store column G key words
With ws
    arrwords = .Range("g1:g" & .Cells(Rows.Count, "g").End(xlUp).Row).Value
End With

'Loop through Workbook Worksheets
For K = 1 To Worksheets.Count
    If Sheets(K).Name <> "FTT" Then
        With Sheets(K)
            a = .Range("a2:d" & .Cells(Rows.Count, "a").End(xlUp).Row).Value 'Store Sheets Value to array
        
                For i = 1 To UBound(a, 1)
                    For Each aword In arrwords 'loop through each keyword
                        If InStr(a(i, 2), aword) > 0 Then 'If found then row 2 putkeyword
                            c = c + 1
                            b(c, 1) = a(i, 1)
                            b(c, 2) = aword
                            If aword = "SSMT TRYU" Then
                                b(c, 3) = a(i, 4)
                                Exit For
                            Else
                                b(c, 3) = a(i, 3)
                            End If
                            b(c, 4) = a(i, 4)
                            Exit For
                        End If
                    Next
                Next i
        End With
    End If
Next K

With ws 'Sheet FTT
    .[a2:d500000].ClearContents
    .[a2].Resize(UBound(b, 1), UBound(b, 2)).Value = b
    .[a2].Sort Key1:=.Range("b2"), key2:=.Range("a2"), order1:=xlAscending, order2:=xlDescending, Header:=xlYes
End With

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub
 
Upvote 0
Hi @MKLAQ, I've changed ssmt tryu from d to c.
thanks again , but gives mismatch error in this line
VBA Code:
For Each aword In arrwords
Hopefully there's other gurus can provide other solutions which's more efficiency
I know you do your best & thank you
I hope from experts see this thread and share us faster code :)
 
Upvote 0
thanks again , but gives mismatch error in this line
VBA Code:
For Each aword In arrwords

I know you do your best & thank you
I hope from experts see this thread and share us faster code :)
Did you put keyvalue in Column G Sheets FTT?

I tried run 38sheets (each sheet around 4k rows) , it takes roughly 5-10second (macos)

Meanwhile Dim c as long ( instead of integer)
1690181392745.png
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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