Within a For Each Loop, How can I Cut and Paste an Entire Row using Range and a variable for the cell address?

bpelkey

New Member
Joined
Dec 17, 2019
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Within a For Each loop - code below, I loop down Column V of a dimmed worksheet "wsr", so I assigned "v" as the range variable within Column V, so in essence it is a "For each v in ColV" situation. When a certain set of conditions is met for a given "v", I want to cut-and-paste the entire row to another worksheet.

So I store the address of the "v" as "vaddr", and then below in script attempt to use it to cut-and-paste the entire row to another worksheet. The variable "j" is incremented during the execution of the For Each loop. When I step through the code, the error below generates for the line immediately below it : "Run time error 5: Invalid procedure call or argument".

wsr.Range(vaddr).EntireRow.Cut wsc.Range(Cells(j & "A")) '(Part of the code below) The "vaddr" for example stored "$V$136" in one case.


VBA Code:
[/Dim wbm As Workbook, wbr As Workbook: Dim wsm As Worksheet, wsr As Worksheet, wsc As Worksheet:
Dim LastRowr As Long: Dim Match As Boolean: Dim ColV As Range, v As Range: Dim vaddr As String
Option Explicit
Public Sub Initialize()
Application.ScreenUpdating = False: Application.Calculation = xlManual: Application.EnableEvents = False
Set wbm = Workbooks("Recon Match-and-Clear Macro.xlsm")
Set wsm = wbm.Sheets("Macro")
Set wbr = Workbooks("LST_092019_25590000-25590200_MULTIPLE_ACCRUAL ROLL FORWARD TEMPLATE V1.0.xlsx")
Set wsr = wbr.Sheets("Open Items")
Set wsc = wbr.Sheets("Closed Items")
LastRowr = wsr.Cells(Rows.Count, 1).End(xlUp).Row
Process
Application.ScreenUpdating = True: Application.EnableEvents = True
End Sub

Public Sub Process()
Dim wsrrow As Range, WBSe As Variant, WBSeNext As Variant, Amt1 As Double, Amt2 As Double, i As Integer, j As Integer
j = 6 'The Header row of the Closed Items sheet
Set ColV = wsr.Range(Cells(8, 22).Address, Cells(LastRowr, 22).Address)
For Each v In ColV
    'If Left(v, 4) = "US1-" Then
      
       Set wsrrow = wsr.Range(Cells(v.Row, 1).Address, Cells(v.Row, 36).Address)
       WBSe = v.Value
       WBSeNext = v.Offset(1, 0)
    
       If WBSeNext = WBSe Then
          Amt1 = v.Offset(0, -3)
          Amt2 = v.Offset(1, -3)
       End If
          If Amt2 = -(Amt1) Then
              i = i + 1
              vaddr = v.Address
              wsr.Range(vaddr).EntireRow.Cut wsc.Range(Cells(j & "A"))
              '.Range("A1").Cut Sheet2.Range("A1")
              'wsr.Range(vaddr).Copy Destination:=wsc.Range(j & "A")
          End If
    'End If
   Next
End Sub]
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
How about
Rich (BB code):
Public Sub Process()
Dim wsrrow As Range, WBSe As Variant, WBSeNext As Variant, Amt1 As Double, Amt2 As Double, i As Integer, j As Integer
j = 6 'The Header row of the Closed Items sheet
Set ColV = wsr.Range("V8:V" & LastRowr)
For Each v In ColV
    'If Left(v, 4) = "US1-" Then
    
'       Set wsrrow = wsr.Range(wsr.Cells(v.Row, 1).Address, Cells(v.Row, 36).Address)
       WBSe = v.Value
       WBSeNext = v.Offset(1, 0)
  
       If WBSeNext = WBSe Then
          Amt1 = v.Offset(0, -3)
          Amt2 = v.Offset(1, -3)
       End If
          If Amt2 = -(Amt1) Then
              j = j + 1
'              vaddr = v.Address
              v.EntireRow.Cut wsc.Range("A" & j)
          End If
    'End If
   Next
End Sub
 
Upvote 0
Thank you Rich and XL2BB! This change worked!:
v.EntireRow.Cut wsc.Range("A" & j)
 
Last edited by a moderator:
Upvote 0
You're welcome & thanks for the feed back.

In future, please do not duplicate your posts & do not quotes entire posts. Just use the "Quick reply" window at the bottom of the thread.
Thanks
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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