Consulting

Results 1 to 2 of 2

Thread: Error 1004

  1. #1
    VBAX Newbie
    Joined
    Aug 2014
    Posts
    1
    Location

    Error 1004

    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
    Last edited by Bob Phillips; 08-08-2014 at 12:08 PM. Reason: Added VBA tags

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    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
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •