VBA Copy rows on to same sheet depending on variables

pscofe

New Member
Joined
Jan 24, 2020
Messages
37
Office Version
  1. 365
Platform
  1. Windows
Hi, looking to copy and paste an entire row to the bottom of a table if criteria has been met.
Example:
If Currency is NOT 'USD, EUR or GBP' then copy that row to a few cells below the existing table. Is this possible? Thanks

local_acc_no closing_balance currencystmt_dateFEED_TYPE
AA- 1,064,238.50USD
15/01/2020 00:00​
Stmt Ccy Message
BB- 95,237.29EUR15/01/2020 00:00Stmt Ccy Message
CC- 42,370.41EUR15/01/2020 00:00Stmt Ccy Message
DD- 26,220.93EUR15/01/2020 00:00Stmt Ccy Message
EE- 10,923.28CNY15/01/2020 00:00Stmt Ccy Message
FF- 6,740.20USD15/01/2020 00:00Stmt Ccy Message
HH- 2,364.93EUR15/01/2020 00:00Stmt Ccy Message
II- 0.02EUR15/01/2020 00:00Stmt Ccy Message
JJ- 0.01USD15/01/2020 00:00Stmt Ccy Message
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I would do something like
VBA Code:
Dim dCell as Range
Dim rng as Range: Set rng=Range("C2:C99") 'you can set 99 as dynamic last row or column of a table

For each dCell in rng
   If (dCell<>"USD" and dCell<>"EUR" and dCell<>"GPB") then
      dcell.entirerow.copy
     dcell.offset(100,0).paste                       '100 rows below, you can set up a dynamic variable
  End if
Next dCell
 
Upvote 0
Hi pscofe, welcome to MrExcel!

Here another macro for you to consider:

VBA Code:
Sub Copy_rows()
  Dim i As Long
  For i = 2 To Range("C" & Rows.Count).End(xlUp).Row
    Select Case Range("C" & i).Value
      Case "USD", "EUR", "GBP"
      Case Else
        Rows(i).Copy Range("A" & Range("C" & Rows.Count).End(xlUp)(2).Row)
    End Select
  Next
End Sub
 
Upvote 0
Is it possible to copy those lines and remove them from existing table?
So BEFORE table:

local_acc_no closing_balance currencystmt_dateFEED_TYPE
AA- 1,064,238.50USD15/01/2020 00:00Stmt Ccy Message
BB- 95,237.29EUR15/01/2020 00:00Stmt Ccy Message
CC- 42,370.41EUR15/01/2020 00:00Stmt Ccy Message
DD- 26,220.93EUR15/01/2020 00:00Stmt Ccy Message
EE- 10,923.28CNY15/01/2020 00:00Stmt Ccy Message
FF- 6,740.20USD15/01/2020 00:00Stmt Ccy Message
GG- 2,364.93EUR15/01/2020 00:00Stmt Ccy Message
HH- 0.02EUR15/01/2020 00:00Stmt Ccy Message
II- 0.01USD15/01/2020 00:00Stmt Ccy Message
 
Upvote 0
END table:
local_acc_no closing_balance currencystmt_dateFEED_TYPE
AA- 1,064,238.50USD15/01/2020 00:00Stmt Ccy Message
BB- 95,237.29EUR15/01/2020 00:00Stmt Ccy Message
CC- 42,370.41EUR15/01/2020 00:00Stmt Ccy Message
DD- 26,220.93EUR15/01/2020 00:00Stmt Ccy Message
FF- 6,740.20USD15/01/2020 00:00Stmt Ccy Message
GG- 2,364.93EUR15/01/2020 00:00Stmt Ccy Message
HH- 0.02EUR15/01/2020 00:00Stmt Ccy Message
II- 0.01USD15/01/2020 00:00Stmt Ccy Message
local_acc_no closing_balance currencystmt_dateFEED_TYPE
EE- 10,923.28CNY15/01/2020 00:00Stmt Ccy Message
 
Upvote 0
Try this

VBA Code:
Sub Copy_rows()
  Dim i As Long, lr As Long
  Application.ScreenUpdating = False
  lr = Range("C" & Rows.Count).End(xlUp).Row
  Rows(1).Copy Rows(lr + 2)
  For i = lr To 2 Step -1
    Select Case Range("C" & i).Value
      Case "USD", "EUR", "GBP"
      Case Else
        Rows(i).Copy Range("A" & Range("C" & Rows.Count).End(xlUp)(2).Row)
        Rows(i).Delete
    End Select
  Next
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,330
Members
449,155
Latest member
ravioli44

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