insert three columns with merge duplicate items and calculation across sheets
hi guys
I need macro to insert three columns in sheet FINISHING after matching column B with the others sheets and merge duplicate items across sheets for columns sales & returns and calculation as the formula in column BALANCE( the formula shouldn't show) with considering two things somtimes there is items are existed in the others sheets but not existed in sheet finishing should be add it into sheet finishing ,will add new sheets every time ( the same structure as in the others) also should show hyphen for empty or zero value for cells with formatting of numbers .
the result should be in columns D,E,F in sheet finshing
Last thing when run the macro repeatedly should not insert columns every times just update data if add new data or change .
Re: insert three columns with merge duplicate items and calculation across sheets
great !
two modifying please
1- sorry I don't mentioned from the beginning .sometimes there is new item into one of sheets but not existed in sheet finshing should be add it and dd hyphen in column C .
2- I want add numberformat foe each value like this "#,##0.00"
thanks
Re: insert three columns with merge duplicate items and calculation across sheets
thanks again
yesterday strange case occurs !!
it gives wrong values it seems arranging the columns are not correct and today occures the same thing
just compare the result in sheet FINISHING when running the code with the right result in last sheet.
Re: insert three columns with merge duplicate items and calculation across sheets
why 30?
The ITEM "TR 1425/148V MN 123H K/L" only exists in the "items" sheet. So the macro is fine. Check your data.
it's existed into two sheets ITEMS,SHEET2 , if as you said 15 then shouldn't give 34 ( it seems 15+15+4) .the right 15+15
the problem when shows the values are wrong .the returns shows me 34 but the right 4 , and the balance shows me 4 but the right is 224 ,did you see the values after run the macro ?
it seems the return add over sale (30+4=34) and the return replace of the balance .
Re: insert three columns with merge duplicate items and calculation across sheets
In sheet2 it is not the same, since it has 2 spaces, between "V" and "MN" instead of 1, that's why they are not equal. You must review your data. The macro is fine.
Re: insert three columns with merge duplicate items and calculation across sheets
ok I clean up the spaces to matching but even if this you still don't understand me ( take CTR 12-200M45 MM 123H K/L as in example )
after run the macro sales gives 280 how? it's just existed in first sheet should be 30 also return gives 34 how? should be 4 also the balance how gives 4 ? should be (250-30+4=224)
I no know why you think your code works ? if I exclude the proplems the spaces otherwise the values are extremely wrong , please take your time and just see the item I gave you as example I'm so confused .
do me favor,please ?
just attach the file and will inform you how works .
Re: insert three columns with merge duplicate items and calculation across sheets
HTML Code:
Option Explicit
Sub test()
Dim lr&, i&, rng, ws As Worksheet
Dim dic As Object, key
Set dic = CreateObject("Scripting.dictionary")
For Each ws In Sheets
If ws.Name <> "FINISHING" Then
lr = ws.Cells(Rows.Count, "A").End(xlUp).Row
rng = ws.Range("B2:D" & lr).Value
For i = 1 To lr - 1
If Not dic.exists(rng(i, 1)) Then
dic.Add rng(i, 1), rng(i, 2) & "|" & rng(i, 3)
Else
On Error Resume Next
dic(rng(i, 1)) = (Split(dic(rng(i, 1)), "|")(0) + rng(i, 2)) & "|" & (Split(dic(rng(i, 1)), "|")(1) + rng(i, 3))
On Error GoTo 0
End If
Next
End If
Next
With Worksheets("FINISHING")
lr = .Cells(Rows.Count, "B").End(xlUp).Row
For i = 2 To lr
For Each key In dic.keys
If .Cells(i, "B").Value = key Then
.Cells(i, "D").Value = Split(dic(key), "|")(0)
.Cells(i, "E").Value = Split(dic(key), "|")(1)
.Cells(i, "F").Value = .Cells(i, "C").Value + .Cells(i, "E").Value - .Cells(i, "D").Value
End If
Next
Next
End With
End Sub
Re: insert three columns with merge duplicate items and calculation across sheets
@bebo021999 thanks
first
also there is new item into one of sheets but not existed in sheet finshing should be add it and dd hyphen in column C .
add numberformat foe each value like this "#,##0.00" .
should show hyphen for empty or zero value for cells with formatting of numbers .
now it doesn't give right result . see the post#9 and compare sheet finishing with last sheet
your code gives the same result as DanteAmore's code.
may you attach the file maybe the problem from my file ,please?
Re: insert three columns with merge duplicate items and calculation across sheets
@bebo021999
very strange !!! it works
do me favor please add this my asking as I has ever said,please?
also there is new item into one of sheets but not existed in sheet finshing should be add it and dd hyphen in column C .
add numberformat foe each value like this "#,##0.00" .
should show hyphen for empty or zero value for cells with formatting of numbers .
Re: insert three columns with merge duplicate items and calculation across sheets
Again:
HTML Code:
Option Explicit
Sub test()
Dim lr&, i&, k&, rng, ws As Worksheet, oldItem As Boolean, Rs, RsNew(1 To 10000, 1 To 6)
Dim dic As Object, key
Set dic = CreateObject("Scripting.dictionary")
For Each ws In Sheets
If ws.Name <> "FINISHING" Then
lr = ws.Cells(Rows.Count, "B").End(xlUp).Row
rng = ws.Range("B2:D" & lr).Value
For i = 1 To lr - 1
If Not dic.exists(rng(i, 1)) Then
dic.Add rng(i, 1), rng(i, 2) & "|" & rng(i, 3)
Else
On Error Resume Next
dic(rng(i, 1)) = (Split(dic(rng(i, 1)), "|")(0) + rng(i, 2)) & "|" & (Split(dic(rng(i, 1)), "|")(1) + rng(i, 3))
On Error GoTo 0
End If
Next
End If
Next
With Worksheets("FINISHING")
lr = .Cells(Rows.Count, "B").End(xlUp).Row
Rs = .Range("A2:F" & lr).Value
For Each key In dic.keys
oldItem = False
For i = 1 To lr - 1
If Rs(i, 2) = key Then
oldItem = True ' indicate new Item NOT found
Rs(i, 4) = Split(dic(key), "|")(0)
Rs(i, 5) = Split(dic(key), "|")(1)
On Error Resume Next
Rs(i, 6) = Rs(i, 3) + Rs(i, 5) - Rs(i, 4)
On Error GoTo 0
End If
Next
If Not oldItem Then ' new Item found
k = k + 1
RsNew(k, 1) = lr - 1 + k
RsNew(k, 2) = key
RsNew(k, 3) = 0
RsNew(k, 4) = Split(dic(key), "|")(0)
RsNew(k, 5) = Split(dic(key), "|")(1)
On Error Resume Next
RsNew(k, 6) = RsNew(k, 3) + RsNew(k, 5) - RsNew(k, 4)
On Error GoTo 0
End If
Next
.Range("A2").Resize(lr - 1, 6).Value = Rs
.Range("A" & lr + 1).Resize(lr - 1, 6).Value = RsNew
lr = .Cells(Rows.Count, "B").End(xlUp).Row
.Range("A2:F" & lr).Borders.LineStyle = xlContinuous
.Range("C2:F" & lr).NumberFormat = "#,##0.00"
End With
End Sub
Re: insert three columns with merge duplicate items and calculation across sheets
jindon
So, you don't want to even test my code, good.
my apologies !!!
the matter is not as what you think , believe me I don't note it just when enter the thread I see the last post for bebo021999
I don't enter by notification in my email if I did it ,then I saw it.thanks for informe me
about your code extremely perfect except one thing when I add new item and contains values in columns sales & returns it will put value under column QTY shouldn't be , when add new item the column QTY is always "-" should put the values under columns sales & returns
see the wrong in last row when I run the macro and the right in column O,P ,Q range also highlighted .
Re: insert three columns with merge duplicate items and calculation across sheets
That's your formatting.
The code copies the cell formats from the last row when the code needs to add new row(s).
The cell format in that column have multiple formatting, so you need to set it as you want for each column before you run the code.
Re: insert three columns with merge duplicate items and calculation across sheets
@jindon thanks for your solution & time
@Dante Amor you're right , your code works but just on your attaching but not mine( strange case!!!) . even if that many thanks
@bebo021999 thanks very much for time & helping
great works guys
You're right, my macro has problems with the "*" considering it as a wildcard.
I changed the focus to search, I append the updated code and in the file you will see the sheet "check" with the verification of the results.
Bookmarks