VBA how to copy paste specific cells if condition is met, not all row

aksent1344

New Member
Joined
Feb 15, 2022
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I have a code to copy paste entire row which met condition, but how to copy not entire row, but specific cells from that rows? For example just A, C and D cells from row.

Sub CopyRow_Item()
Application.ScreenUpdating = False
Dim LastRow As Long
Dim last_row As Long
Item = ThisWorkbook.Sheets("Sheet1").Range("B1").Value
LastRow = Sheets("Actuals").Cells.Find("*", SearchOrder:=xlByRows,
SearchDirection:=xlPrevious).Row
last_row = Cells(Rows.Count, "A").End(xlUp).Row
Dim x As Long
x = 1
Dim rng As Range
For Each rng In Sheets("Actuals").Range("A2:A" & LastRow)
If rng = Item Then
rng.EntireRow.Copy
Sheets("Sheet1").Cells(last_row + x, 1).PasteSpecial xlPasteValues
x = x + 1
End If
Next rng


Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try:
VBA Code:
Sub CopyRow_Item()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    Dim last_row As Long
    Item = ThisWorkbook.Sheets("Sheet1").Range("B1").Value
    LastRow = Sheets("Actuals").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    last_row = Cells(Rows.Count, "A").End(xlUp).Row
    Dim x As Long
    x = 1
    Dim rng As Range
    For Each rng In Sheets("Actuals").Range("A2:A" & LastRow)
        If rng = Item Then
            Intersect(rng.Row, Range("A:A,C:D")).Copy
            Sheets("Sheet1").Cells(last_row + x, 1).PasteSpecial xlPasteValues
            x = x + 1
        End If
    Next rng
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub CopyRow_Item()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    Dim last_row As Long
    Item = ThisWorkbook.Sheets("Sheet1").Range("B1").Value
    LastRow = Sheets("Actuals").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    last_row = Cells(Rows.Count, "A").End(xlUp).Row
    Dim x As Long
    x = 1
    Dim rng As Range
    For Each rng In Sheets("Actuals").Range("A2:A" & LastRow)
        If rng = Item Then
            Intersect(rng.Row, Range("A:A,C:D")).Copy
            Sheets("Sheet1").Cells(last_row + x, 1).PasteSpecial xlPasteValues
            x = x + 1
        End If
    Next rng
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
Got error that is mismatch and Row, is marked
 
Upvote 0
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach screenshots (not a pictures) of your sheets. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
So In cell B2 I select Item number

Forecast data (version 1).xlsm
ABCDEFGHIJK
1Item No.00017133
2Item No.TypeItem DescriptionHFBPAUnit VolumeYearMonthCountryPOSDiscount Amount
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Sheet1
Cells with Data Validation
CellAllowCriteria
B1List=Settings!$A$83:$A$35378
D1List=Settings!$B$83:$B$104


And I want that macros take all yellow columns with item number that I selected and paste it in first sheet.

Forecast data (version 1).xlsm
ABCDEFGHIJKLMN
1No.Product Area No.Sales Start Date LTEnd Date Sale LTKey article FY22 T2|T3 LTQuality article FY 22 T2|T3Recommended Add-on FY22 T2|T34A1K FY22 T2|T3 LTPrice FY22 T2 LTSold Quantity till W1 LTUpdated 2 Quantity LT T2|T3Turnover LT T1Turnover LT T2|T3Total turnover LT
22022144907312013-04-01XX375 96611 716147 917358 258506 175
39034932605222017-04-01XX2798561 327162 003305 978467 981
49020463907312013-04-01XX493 7577 064124 198286 063410 261
58052488207312022-01-01XX101 23146 8369 156387 074396 231
67038913970152018-08-01XX279606932124 706214 899339 605
74050734203512021-10-01XX1696071 59874 746223 192297 938
87021356907112013-04-01XX303 8207 09394 711175 860270 570
95021456004212013-04-012022-04-01603 3691 863167 05892 380259 438
102048138105112021-10-01XX591 1353 88955 343189 629244 972
118027588702152014-06-01XX54,992 0203 45483 454156 971240 426
126049995470232021-04-01XX57916333073 956157 909231 865
139049344604232020-10-01XX29935159080 933145 793226 726
140026385002142014-06-01XX392 2204 67571 554150 682222 236
158021456804212013-04-012022-04-01503 5761 764147 76972 893220 661
169023422617182014-02-01XX22,991 85010 14227 505192 698220 203
171024630804222014-02-01XX205 4957 77786 285128 545214 831
184054005404212022-04-01XX60-4 200-208 264208 264
194047557270312020-10-0139919145059 826148 388208 214
203021450404222013-04-01XX109 97417 00365 944140 521206 464
214044539201132018-12-012023-04-01XX34938238294 395110 180204 575
222022388201112013-04-01XX28039950382 438116 397198 835
230047354603412021-04-01X691 6171 86292 209106 180198 389
244040792204232019-04-01XX14966398676 163121 417197 579
256020566407112013-04-01XXX99 37217 03869 709126 729196 438
267026115003522015-04-01Back-offXX1496171 05765 779130 160195 939
277049637570232021-04-01XX89910016970 165125 563195 728
Sheet3
 
Upvote 0
So In cell B2 I select Item number
Did you mean B1 of Sheet1 instead of B2? Is that item number found in column A of Sheet3? Do you want to copy the yellow data from Sheet3 to Sheet1? If that is what you want, the column headers in Sheet3 don't match the headers in Sheet1. Please clarify in detail.
 
Upvote 0
Ohh sorry, Yes B1. This number should be found in Sheet 3 A column. I want to copy yellow data columns from Sheet 3 to Sheet 1, but just those which has item number equal to Sheet 1 B1 cell.
I know that headers doesn't match exactly.
Adding full table:

Forecast data (version 1).xlsm
ABCDEFGHIJKLMNOPQRSTU
1Item No.00017133
2Item No.TypeItem DescriptionHFBPAUnit VolumeYearMonthCountryPOSDiscount AmountNet AmountQuantityCost AmountSales PriceFranchice feeGPGMGM1GM1%Deductions
3
4
5
6
7
8
9
10
11
12
13
Sheet1
Cells with Data Validation
CellAllowCriteria
B1List=Settings!$A$83:$A$35378
D1List=Settings!$B$83:$B$104


A column from sheet 3 should be paste in A column in sheet1
B column from sheet 3 should be paste in E column in sheet1
I column from sheet 3 should be paste in O column in sheet1
K column from sheet 3 should be paste in M column in sheet1
M column from sheet 3 should be paste in L column in sheet1
 
Upvote 0
Do you want to copy the data automatically when you enter an item number in B1 or do you want o run the macro manually?
 
Upvote 0
Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, desWS As Worksheet, fnd As Range, colArr As Variant, i As Long
    colArr = Array("A", "A", "B", "E", "I", "O", "K", "M", "M", "L")
    Set desWS = Sheets("Sheet1")
    Set srcWS = Sheets("Sheet3")
    Set fnd = srcWS.Range("A:A").Find(desWS.Range("B1").Value, LookIn:=xlValues, lookat:=xlWhole)
    If Not fnd Is Nothing Then
        For i = LBound(colArr) To UBound(colArr) Step 2
            srcWS.Range(colArr(i) & fnd.Row).Copy desWS.Cells(desWS.Rows.Count, colArr(i + 1)).End(xlUp).Offset(1)
        Next i
    Else
        MsgBox desWS.Range("B1") & " not found."
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,660
Latest member
Zatman

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