Copying Addresses to New Sheet

JADownie

Active Member
Joined
Dec 11, 2007
Messages
395
Hi,

I have the code below that I was trying to use to flag addresses where the state was AK or HI (State is in Col J on sheet Address Details). I am trying to copy all of these values to the sheet AK_HI Records but I keep getting a runtime error.

Any help here would be greatly appreciated.



Sub H_Copy_AK_HI()
Dim strArray As Variant
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim NoRows As Long
Dim DestNoRows As Long
Dim I As Long
Dim J As Integer
Dim rngCells As Range
Dim rngFind As Range
Dim Found As Boolean

strArray = Array("AK", "HI")

Set wsSource = ActiveSheet

NoRows = wsSource.Range("A100000").End(xlUp).Row
DestNoRows = 1
Set wsDest = ActiveWorkbook.Sheets("AK_HI Records")

For I = 1 To NoRows

Set rngCells = wsSource.Range("I" & J & ":I" & J)
Found = False
For J = 0 To UBound(strArray)
Found = Found Or Not (rngCells.Find(strArray(J)) Is Nothing)
Next J

If Found Then
rngCells.EntireRow.Copy wsDest.Range("A" & DestNoRows)

DestNoRows = DestNoRows + 1
End If
Next I

Sheets("Address Details").Select




End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
What are you trying to do with the line in red?
 
Upvote 0
Yes but what is it meant to be doing?
 
Upvote 0
What value is J for the line in red? You've declared J at the top as an Integer (should be Long if using Excel 2007 or later), which initially has value 0 so you're effectively setting
Code:
Set rngCells = wsSource.Range("I0:I0")
 
Upvote 0
@JackDanIce
I'm thinking that it should really be
Code:
Set rngCells = wsSource.Range("J"& I)
As the Op is looking for the state in col J.
Hence trying to seek clarification
 
Upvote 0
Hey @Fluff, think you're right, see if that helps OP. I just saw J declared as Integer and first place it was used was in the line in red
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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