VBA: Copy-Paste Row Subject to value in column A

Adar123

Board Regular
Joined
Apr 1, 2018
Messages
83
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
Hi all,

I am trying to write a logic, where if cell value in column A = "A", the data from that whole row needs to be copied to the lastrow +1 / the first empty row below. As a step 2, the values of cells in column A within the newly copied range or the original range of cells with value A to replaced with value "D".

Thank you.

The code which is incomplete:
VBA Code:
lastRow = Cells(Rows.Count, 1).End(xlUp).Row 
    ' Loop through each row
    For x = 2 To lastRow
        ThisValue = Cells(x, 4).Value
        If ThisValue = "A" Then
            Cells(x, 1).Resize(Y, 1).Copy 'I am trying to copy the whole range in one go because lines of data with value "A" in cells in Column A will be one after another but haven't yet defined "Y"
                        NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
            Cells(NextRow, 1).Select
            ActiveSheet.Paste
        End If
     Next x

The dataset sample

CommodityPillarPrice
A
Sep20​
3.485​
A
Oct20​
3.5775​
A
Nov20​
3.5775​
B
Jan22​
9.415​
B
Feb22​
9.265​
B
Mar22​
9.265​
B
Apr22​
9.2375​
B
May22​
9.2375​
C
Mar27​
6.1033​
C
Apr27​
6.1101​
C
May27​
6.1101​
C
Jun27​
6.1133​
C
Jul27​
6.1133​
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
See if this does what you want. Test with a copy of your data.

VBA Code:
Sub CopyAndChange()
  Dim oset As Long, Rws As Long
  
  Application.ScreenUpdating = False
  With Range("A1").CurrentRegion
    oset = .Rows.Count + 1
    .AutoFilter Field:=1, Criteria1:="A"
    Rws = .Columns(1).SpecialCells(xlVisible).Count - 1
    If Rws > 0 Then
      .Offset(1).Resize(Rws).Copy Destination:=.Cells(oset, 1)
      .Cells(oset, 1).Resize(Rws).Value = "D"
    End If
    .AutoFilter Field:=1
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you, this seem to do the job but how you disable filter after the job is done? I am mindful that it may create some issues once I start copying the data and doing other changes to it.
 
Upvote 0
Thank you. With further testing I realized that it is picking data from a wrong line. If you run it against Criteria1:="B" or Criteria1:="C" it take values from columns B:C incorrectly.
 
Upvote 0
I realized that it is picking data from a wrong line.
Yes, sorry, my mistake. Test this instead.

VBA Code:
Sub CopyAndChange_v2()
  Dim oset As Long, Rws As Long
  
  Application.ScreenUpdating = False
  With Range("A1").CurrentRegion
    oset = .Rows.Count + 1
    .AutoFilter Field:=1, Criteria1:="A"
    Rws = .Columns(1).SpecialCells(xlVisible).Count - 1
    If Rws > 0 Then
      .Offset(1).Resize(oset - 2).Copy Destination:=.Cells(oset, 1)
      .Cells(oset, 1).Resize(Rws).Value = "D"
    End If
  .Parent.AutoFilterMode = False
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you, this is working perfect now but need to scratch my head a bit to fully understand the syntax.
 
Upvote 0

Forum statistics

Threads
1,215,883
Messages
6,127,544
Members
449,385
Latest member
KMGLarson

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