I don't know what I'm doing!

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,904
Office Version
  1. 365
Platform
  1. Windows
Hi,

I've written the following code:

Code:
Sub Simulate()

Application.Calculation = xlCalculationManual

Dim c As Range

For i = 1 To 675

    For Each c In Range("rng")
        If c.Value = Range("tgt").Value Then
            Rows(c).Copy

        End If
    Next c
    Calculate
    
Next

Application.Calculation = xlCalculationAutomatic

End Sub

Basically, all I want is to copy the row when c.value = tgt, but it doesn't like the syntax I'm trying. Once copied I want to move it to a sheet called "output" and paste to the next empty row (but I should be fine with this piece of code).

Cheers,
Jon
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You have your variables in quotes.

Sub Simulate()

Application.Calculation = xlCalculationManual

Dim c As Range

For i = 1 To 675

For Each c In Range("rng")
If c.Value = Range("tgt").Value Then
Rows(c).Copy

End If
Next c
Calculate

Next

Application.Calculation = xlCalculationAutomatic

End Sub
 
Upvote 0
I'm not sure what the 1 to 675 is for.

And, you weren't pasting your data anywhere.

Try:

Sub Simulate()

Application.Calculation = xlCalculationManual

Dim c As Range

For Each c In Range("rng")
If c.Value = Range("tgt").Value Then
c.EntireRow.Copy Sheets("output").Range("A65536").End(xlUp).Offset(1, 0)
End If
Next c
Calculate


Application.Calculation = xlCalculationAutomatic

End Sub
 
Upvote 0
rng & tgt need to be Named Ranges on your sheet or you need to Set these ranges or define there location limits.

Do you have these Ranges Named Using: Toolbar: Insert - Name Define...?
 
Upvote 0
Thanks all of you, got this solved now. I wasn't clear, but yes "tgt" and "rng" are defined range names (i.e. Insert->Names->Define).

I think that the problem was with the row selection syntax, works with EntireRow.Copy.

Thanks again...
 
Upvote 0

Forum statistics

Threads
1,203,348
Messages
6,054,885
Members
444,760
Latest member
TeckTeck

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