PDA

View Full Version : Error 1004



Resz7
08-08-2014, 10:52 AM
Hi everyone! I'm receiving Error 1004 at these 2 lines:
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
What am I doing wrong? Thanks in advance!



Option Explicit
Private Sub CommandButton1_Click()
'This code pulls only the data we need from Sheet1 and puts it on Sheet6
Dim rngFind As Range, firstAddress As String
Const strFindMe As String = "Ship To:"
'Makes sure cell selections are where they're supposed to be
Worksheets("Sheet6").Select
Worksheets("Sheet6").Range("A2").Select
Worksheets("Sheet1").Select
Worksheets("Sheet1").Range("A1").Select
'Find Next loop. Searches for "Ship To:" and selects data underneath it
With Worksheets("Sheet1").Cells
Set rngFind = .Find(what:=strFindMe, LookIn:=xlValues)
If Not rngFind Is Nothing Then
firstAddress = rngFind.Address
Do
rngFind.Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy

Sheets("Sheet6").Select
ActiveSheet.Paste
Worksheets("Sheet6").Range("A20000").Select
Selection.End(xlUp).Select
ActiveCell.Offset(2, 0).Select


Sheets("Sheet1").Select

Set rngFind = .FindNext(rngFind)

Loop While Not rngFind Is Nothing And rngFind.Address <> firstAddress
End If
End With
Worksheets("Sheet6").Select
Worksheets("Sheet6").Range("A1").Select

mancubus
08-08-2014, 01:48 PM
welcome to vbax.

you don't need to select objects in order to work with them.

i think you have imported data from a non excel application in non contiguous ranges and you want to copy each group of cells (area) that contains "Ship To:" to another worksheet.

this may suffice.
if not, please post your workbook here.



Private Sub CommandButton1_Click()

Dim rngFind As Range, firstAddress As String

With Worksheets("Sheet1").Cells
Set rngFind = .Find(what:="Ship To:")
If Not rngFind Is Nothing Then
firstAddress = rngFind.Address
Do
rngFind.CurrentRegion.Copy Worksheets("Sheet6").Cells(Rows.Count, "A").End(xlUp).Offset(2)
Set rngFind = .FindNext(rngFind.Value)
Loop While Not rngFind Is Nothing And rngFind.Address <> firstAddress
End If
End With
End Sub