+ Reply to Thread
Results 1 to 4 of 4

For Each x In .Range("A1:A" - Any way to select column but shorten the range?

Hybrid View

BPSJACK For Each x In .Range("A1:A" -... 09-02-2014, 08:43 AM
nathansav Re: For Each x In... 09-02-2014, 08:56 AM
AB33 Re: For Each x In... 09-02-2014, 09:19 AM
BPSJACK Re: For Each x In... 09-03-2014, 04:14 AM
  1. #1
    Forum Contributor
    Join Date
    01-06-2012
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    165

    For Each x In .Range("A1:A" - Any way to select column but shorten the range?

    Hi guys,

    I have a macro which looks at columns A & B, looks for matching data, and copies adjacent cells to a new column.

    However once the macro completes, the sheet now contains 1048576 rows. I only need to apply the macro down to row 10'000. I thought if I changed

    For Each x In .Range("A1:A"
    to

    For Each x In .Range("A1:A10000
    then it might solve it but I just get a error 1004 range of object errors.

    Any ideas? Below is the sub in question.
    Sub Macro1()
    Dim x As Range
    
    With Sheet1
        For Each x In .Range("A1:A" & .Cells(Rows.Count, 1).End(xlUp).Row)
            If Not .Range("B1:B" & .Cells(Rows.Count, 2).End(xlUp).Row).Find(x.Value) Is Nothing Then
                .AutoFilterMode = False
                .Range("B1:D" & .Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=1, Criteria1:=x.Value
                With .AutoFilter.Range
                    .Offset(1, 0).Resize(.Rows.Count - 1, 3).Copy
                End With
                .AutoFilterMode = False
                .Cells(Rows.Count, 5).End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValues)
                Application.CutCopyMode = False
                Application.ScreenUpdating = False
            End If
        Next x
    End With
    
    End Sub
    Last edited by BPSJACK; 09-03-2014 at 04:20 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: For Each x In .Range("A1:A" - Any way to select column but shorten the range?

    .Range("A1:A" & .range("a1").End(xlDown).Row).cells

    Although, where are you getting the error?
    Last edited by nathansav; 09-02-2014 at 08:59 AM.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: For Each x In .Range("A1:A" - Any way to select column but shorten the range?

    Option Explicit
    
    Sub Macro1()
    Dim x As Range, Findme As Range
    
    With Sheet1
        For Each x In .Range("A1:A" & .Cells(.Rows.Count, 1).End(xlUp).Row)
            Set Findme = .Range("B1:B" & .Cells(Rows.Count, 2).End(xlUp).Row).Find(x.Value)
            If Not Findme Is Nothing Then
                .AutoFilterMode = False
                .Range("B1:D" & .Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=1, Criteria1:=x.Value
                With .AutoFilter.Range
                    .Offset(1, 0).Resize(.Rows.Count - 1, 3).Copy
                End With
                .AutoFilterMode = False
                .Cells(Rows.Count, 5).End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValues)
                Application.CutCopyMode = False
                Application.ScreenUpdating = False
            End If
        Next x
    End With
    
    End Sub

  4. #4
    Forum Contributor
    Join Date
    01-06-2012
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    165

    Re: For Each x In .Range("A1:A" - Any way to select column but shorten the range?

    That's brilliant, thank you for your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Select range till where column AG has "BB" value
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-11-2014, 10:02 AM
  2. if Range("A1") = "Mike" Then Range("A1") = "michael" Then Exit Sub
    By a8015945 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-30-2013, 10:08 AM
  3. Replies: 3
    Last Post: 04-14-2013, 11:53 PM
  4. Getting "Method 'Range' of Object '_Global Failed" with Range...Select
    By murpat01 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 05-14-2007, 03:51 PM
  5. [SOLVED] "Criteria Range" in the "Data/Filter/Advanced Filter" to select Du
    By TC in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-11-2005, 10:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1