Copy Multiple Ranges with Criteria Match Paste into Another Sheet with appropriate ranges

nandhavnk

New Member
Joined
Jul 18, 2019
Messages
14
Office Version
  1. 2007
Platform
  1. Windows
Hi,

I am beginner to vba code.

What I expecting in my code,

In Userform

Combo box = fetching Data Sheet values from Column A. This goes to next sheet range B2.

Delivery Name text box = fetching Data Sheet from Column D. This goes to next sheet range D2.

Date Text box = manually entering dates. This goes to next sheet range H2.

When Clicking Confirm button,

Checking in Data Sheet & coping combo box matched row values into next sheet from A3:L3. Here matched row titles will paste like item#1, Item#3, Item#4, etc. if items more than 10, show message box,

And matched rows Column B & C values in Order Form Sheet range A4 & B4, pasting rest values in respective item columns and remove blank cells.

Eg.

Date Sheet Values Sample,
Copy with Criteria & Paste.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAY
1
2
3
4Order NoDateTimeNameItem#1Item#2Item#3Item#4Item#5Item#6Item#7Item#8Item#9Item#10Item#11Item#12Item#13Item#14Item#15Item#16Item#17Item#18Item#19Item#20Item#21Item#22Item#23Item#24Item#25Item#26Item#27Item#28Item#29Item#30Item#31Item#32Item#33Item#34Item#35Item#36Item#37Item#38Item#39Item#40Item#41Item#42Item#43Item#44Item#45Item#46
5104-Aug-2210:36 AMSivakumar55.0055.0055.0055.0055.0055.0055.007
6506-Aug-222:00 PMRamkumar55.0055.0055.0055.0055.0055.0055.007
7315-Aug-221:55 PMVimal55.0055.0055.0055.0055.0055.0055.007
8425-Aug-226:15 PMMohankumar55.0055.0055.0055.0055.0055.0055.007
9530-Aug-229:25 AMRamkumar55.0055.0055.0055.0055.0055.0055.007
10404-Sep-228:56 PMMohankumar55.0055.0055.0055.0055.0055.0055.007
11306-Sep-222:44 PMVimal55.0055.0055.0055.0055.0055.0055.007
12615-Sep-223:25 PMMurugan55.0055.0055.0055.0055.0055.0055.007
13325-Sep-224:35 PMVimal55.0055.0055.0055.0055.0055.0055.007
14404-Oct-2212:12 PMMohankumar55.0055.0055.0055.0055.0055.0055.007
15309-Oct-224:28 AMVimal55.0055.0055.0055.0055.0055.0055.007
16714-Oct-225:55 AMSavitha55.0055.0055.0055.0055.0055.0055.007
Data
Cell Formulas
RangeFormula
AY5:AY16AY5=COUNT(E5:AX5)


Userform
User FOrm.jpg



Order Form Sheet Format Sample
Copy with Criteria & Paste.xlsm
ABCDEFGHIJKLMNO
1
2Order No :Name :Date :
3DateTime
4
5
6
7
8
9
10
11
12
13
14Total            
15
Order Form
Cell Formulas
RangeFormula
C14:N14C14=IF(ISBLANK(C4:C13)="",SUM(C4:C13),"")


Expected Output in Order Form Sheet,
Copy with Criteria & Paste.xlsm
ABCDEFGHIJKLMNO
1
2Order No :0005Name :RamkumarDate :17-Oct-22
3DateTimeItem#2Item#4Item#6Item#7Item#8Item#9Item#10Item#14Item#16Item#19Item#20Item#21
406-Aug-222:00 PM55.0055.0055.0055.0055.0055.0055.00
530-Aug-229:25 AM55.0055.0055.0055.0055.0055.0055.00
6
7
8
9
10
11
12
13
14Total55.0055.0055.0055.0055.0055.00110.00110.0055.0055.0055.0055.00
15
Order Form
Cell Formulas
RangeFormula
C14:N14C14=IF(ISBLANK(C4:C13)=TRUE,"",SUM(C4:C13))



My code is,

VBA Code:
Private Sub cmdconfirmO_Click()
Dim ecol As Long, erow As Long, lastrow As Long, i As Long, lastcol As Long

lastrow = Worksheets("Data").Cells(Rows.Count, 1).End(xlUp).row
lastcol = Worksheets("Data").Cells(4, Columns.Count).End(xlToLeft).Column
For i = 4 To lastrow
 For j = 2 To lastcol
    Worksheets("Order Form").Cells(2, 2).Value = Format(Me.cbordnO.Text, "0000")
    Worksheets("Order Form").Cells(2, 4).Value = Me.txtdelO.Value
    Worksheets("Order Form").Cells(2, 8).Value = Me.txtdateO.Value
    If Worksheets("Data").Cells(i + 1, 1).Value = Me.cbordnO.Text Then
     If Worksheets("Data").Cells(i, j) <> "" Then
        Worksheets("Data").Range(Cells(i, j), Cells(i, j)).Copy
        Worksheets("Order Form").Activate
        erow = Worksheets("Order Form").Cells(Rows.Count, 1).End(xlUp).row
        ecol = Worksheets("Order Form").Cells(3, Columns.Count).End(xlToLeft).Offset(0, 1).Column
        Worksheets("Order Form").Cells(3, ecol).Select
        ActiveSheet.Paste Destination:=Worksheets("Order Form").Range(Cells(erow, ecol), Cells(erow, ecol))
        Sheets("Data").Activate
     End If
    End If
 Next j
Next i
Application.CutCopyMode = False
Me.cbordnO.Value = ""
Me.txtdateO.Value = ""
Me.Hide
End Sub

I know this code is not correct. But I dont know how to rectify this issue.

I invite experts look my sheet and give me a solution or a idea. I am stuck here so long.

Thanks
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I put ALL the code for your Userform.
If you want only 10 columns, change ncol to the number 12 on this line.

Rich (BB code):
.Range("A3").Resize(nRow, nCol).Value = c

Put all the code in your userform, replace your existing code.
VBA Code:
Private Sub cbordnO_Change()
  Dim f As Range

  txtdelO.Value = ""
  txtdateO.Value = ""
 
  If cbordnO.ListIndex > -1 Then
    Set f = Sheets("Data").Range("A:A").Find(CDbl(cbordnO.Value), , xlValues, xlWhole)
    If Not f Is Nothing Then
      txtdelO.Value = Sheets("Data").Range("D" & f.Row).Value
    End If
  End If
End Sub

Private Sub cmdconfirmO_Click()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, nRow As Long, nCol As Long
  Dim have_data As Boolean
  Dim tot As Double
 
  'Validations
  If cbordnO.Value = "" Or txtdelO.Value = "" Or txtdateO.Value = "" Then
    MsgBox "complete the data"
    Exit Sub
  End If
 
  a = Sheets("Data").Range("A4:AX" & Sheets("Data").Range("A" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  ReDim c(1 To UBound(a, 1), 1 To UBound(a, 2))
 
  nRow = 2
  c(1, 1) = "Date"
  c(1, 2) = "Time"
  For i = 2 To UBound(a, 1)
    If a(i, 1) = CDbl(cbordnO.Value) Then
      have_data = False
       
      For j = 5 To UBound(a, 2) - 1
       
        If a(i, j) <> "" Then
          have_data = True
          b(1, j - 2) = a(1, j)
          b(nRow, 1) = a(i, 2)
          b(nRow, 2) = a(i, 3)
          b(nRow, j - 2) = a(i, j)
         
          c(nRow, 1) = a(i, 2)
          c(nRow, 2) = a(i, 3)
        End If
     
      Next
      If have_data Then
        nRow = nRow + 1
      End If
    End If
  Next
 
  nCol = 3
  For j = 3 To UBound(b, 2)
    tot = 0
    If b(1, j) <> "" Then
      c(1, nCol) = b(1, j)
      For i = 2 To UBound(b, 1)
        c(i, nCol) = b(i, j)
        tot = tot + b(i, j)
      Next
      c(nRow, nCol) = tot
      nCol = nCol + 1
    End If
  Next
 
  With Sheets("Order Form")
    .Range("A4", .Cells(Rows.Count, Columns.Count)).ClearContents
    .Range("C3", .Cells(3, Columns.Count)).ClearContents
 
    .Range("B2").Value = cbordnO.Value
    .Range("D2").Value = txtdelO.Value
    .Range("H2").Value = txtdateO.Value
   
    .Range("A3").Resize(nRow, nCol).Value = c
  End With
  Unload Me
End Sub

Private Sub UserForm_Activate()
  Dim dic As Object
  Dim i As Long
 
  Set dic = CreateObject("Scripting.Dictionary")
  With Sheets("Data")
    For i = 5 To .Range("A" & Rows.Count).End(3).Row
      dic(.Range("A" & i).Value) = Empty
    Next
  End With
  cbordnO.List = dic.keys
End Sub
 
Upvote 0
Solution
Hi Mr.DanteAmor,

Thank you so much kindness. I very thankful to you.

The code is working properly.

Here I have 3 questions,
1. I expecting Sum values in Row14(fixed).
2. In combo box, how to arrange numbers in ascending.
3. after selection Order No.4 in combo box and click confirm button. That last value ie Item#46, 55.00 not shown.
Here images
Copy with Criteria & Paste.xlsm
ABCDEFGHIJKLMNOPQRS
1
2Order No :0004Name :MohankumarDate :18-Oct-22
3DateTimeItem#2Item#3Item#6Item#7Item#8Item#9Item#12Item#16Item#17Item#18Item#19Item#22Item#27Item#43Item#44Item#45
425-Aug-226:15 PM55.0055.0055.0055.0055.0055.0055.00
504-Sep-228:56 PM55.0055.0055.0055.0055.0055.00
604-Oct-2212:12 PM55.0055.0055.0055.0055.0055.0055.00
755.0055.00110.0055.00110.0055.0055.0055.0055.00110.00110.0055.0055.0055.0055.0055.00
8
9
10
11
12
13
14
Order Form


Actually output expect is,
Copy with Criteria & Paste.xlsm
ABCDEFGHIJKLMNOPQRST
1
2Order No :0004Name :MohankumarDate :18-Oct-22
3DateTimeItem#2Item#3Item#6Item#7Item#8Item#9Item#12Item#16Item#17Item#18Item#19Item#22Item#27Item#43Item#44Item#45Item#46
425-Aug-226:15 PM55.0055.0055.0055.0055.0055.0055.00
504-Sep-228:56 PM55.0055.0055.0055.0055.0055.0055.00
604-Oct-2212:12 PM55.0055.0055.0055.0055.0055.0055.00
7
8
9
10
11
12
13
1455.0055.00110.0055.00110.0055.0055.0055.0055.00110.00110.0055.0055.0055.0055.0055.0055.00
Order Form


Thanks in advance
 
Upvote 0
1. I expecting Sum values in Row14(fixed).
remove these lines from the macro
c(nRow, nCol) = tot
.Range("A4", .Cells(Rows.Count, Columns.Count)).ClearContents
.Range("C3", .Cells(3, Columns.Count)).ClearContents
2. In combo box, how to arrange numbers in ascending.
Sort the numbers in another column and load those numbers or sort your data by number.

3. after selection Order No.4 in combo box and click confirm button. That last value ie Item#46, 55.00 not shown.
Change this line:
For j = 5 To UBound(a, 2) - 1

For this:
For j = 5 To UBound(a, 2)
 
Upvote 0
Its working perfectly. Thank you!!!

I have one more clarification,
If i have add UOM below item Numbers, this worksheet forumat vlookup. Here Example,
Copy with Criteria & Paste.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
1
2Order No :0003Name :VimalDate :18-Oct-22
3DateTimeItem#1Item#2Item#3Item#4Item#5Item#6Item#7Item#8Item#10Item#11Item#13Item#14Item#17Item#20Item#21Item#23Item#24Item#25Item#26Item#29Item#30Item#32Item#38Item#42
4UOM1UOM2UOM3UOM4UOM5UOM6UOM7UOM8UOM9UOM10UOM11UOM12UOM13UOM14UOM15UOM16UOM17<==== This is Default row with vlookup function based item type.
515-Aug-221:55 PM55.0055.0055.0055.0055.0055.0055.00
606-Sep-222:44 PM55.0055.0055.0055.0055.0055.0055.00
725-Sep-224:35 PM55.0055.0055.0055.0055.0055.0055.00
809-Oct-224:28 AM55.0055.0055.0055.0055.0055.0055.00
9
10
11
12
13
1455.0055.0055.00110.0055.0055.00110.0055.00110.0055.0055.0055.0055.0055.0055.0055.0055.00
Order Form


Is this possible, Kindly clarify this.

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,231
Messages
6,123,754
Members
449,118
Latest member
kingjet

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