I'm struggling quite a while with possible solutions, but i couldn't find something similar online, and im could use some help. Screenshot example and workbook sample in links under question. I asked same question on different forum, but no answers... https://www.mrexcel.com/board/threads/find-copy-insert-row-on-condition-with-cell-merge-and-formula-copy.1178634/
I have 3 worksheets, ForDelivery
, Document1
and Document2
. From document sheets i print first page or send it via email, i want to fill specific cells in document sheets 1 and 2 (range"G19:K"
) with values from ForDelivery
sheets (values are in range"B2:F"
.
In sheets Document1
and Document2
i have table template, column "B" in both sheets contain product names, i would need macro that would search for those names in sheet ForDelivery
and if name is found in column "A"
then copy all values from range "B:F"
from row where name was found into row in sheet where name is, column range "G:K"
. I menage to complete this job with XLOOKUP fuction
BUT problem happends when there is more then one product name in column "A"
(number can vary from 2 to 8). That function can't handle, but i hope macro can. So for every same name i need to insert new row under orginal row, copy forumlas from above, and paste values to range "G:K"
. I made a code that will merge cells in columns A
,B
, and F
, to prevent duplicates values. So basically, if there are 3 same names in column "A"
in ForDelivery
sheet insert 2 row (2 because there is already one row existing) in Document 1 or 2
sheet where name is found, and paste data from range B:F
into range G:K
. Thanks in advance.
ScreenShot: https://ibb.co/sFh5dRY Workbook sample here: https://easyupload.io/4f7cq2
If its of some use, here is the code i use for merging cells in columns
Sub macro1()
Dim lngLastRow As String
Dim lastRow As Long
Dim lastcolumn As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
lastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
lastcolumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range(Cells(19, 1), Cells(lastRow, 1)), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.Sort
.SetRange Range(Cells(19, 1), Cells(lastRow, lastcolumn))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For i = lastRow To 19 Step -1
If Cells(i, 1).Value = Cells(i - 1, 1).Value Then
Range(Cells(i, 1), Cells(i - 1, 1)).Select
Selection.Merge
Range(Cells(i, 2), Cells(i - 1, 2)).Select
Selection.Merge
Range(Cells(i, 3), Cells(i - 1, 3)).Select
Selection.Merge
Range(Cells(i, 6), Cells(i - 1, 6)).Select
Selection.Merge
End If
Next
Application.ScreenUpdating = True
End Sub