+ Reply to Thread
Results 1 to 11 of 11

Unlimited Range for VlookUp in Macro Code

Hybrid View

  1. #1
    Registered User
    Join Date
    01-04-2013
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    42

    Exclamation Unlimited Range for VlookUp in Macro Code

    I am trying to program a Vlookup for Column C, however the macro will be used for more than one report. Can anyone show me how to program the range to be unlimited? When I put C655636 (the max amount of rows for Excel 2003), Excel freezes and will not respond. This is one part of a very large macro but I can not continue until this is solved.

    Your help is appreciated!

    Thanks!

    Sub ConductVlookupCopyOver()
    '
    ' ConductVlookupCopyOver Macro
    ' Macro recorded 1/4/2013 by cac1057
    '
    
    '
        Selection.Insert Shift:=xlToRight
        Range("C2").Select
        ActiveCell.FormulaR1C1 = _
            "=VLOOKUP(RC[-1],'[Previous Hold Transfer Report.XLS]HoldTransfer Over & Under 25 De'!C2,1,FALSE)"
        Range("C2").Select
        Selection.AutoFill Destination:=Range("C2:C943")
        Range("C2:C943").Select
    End Sub
    Last edited by fmfernandez11; 01-11-2013 at 11:16 AM.

  2. #2
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200

    Re: Unlimited Range for VlookUp in Macro Code

    Howdy:

    Do you think that the problem may be memory-related with a range that large?

    When I have that type of thing to do, I only copy the formula into the rows that actually need it. If only 200 cells have data, for instance, the range would be modified to read "range("c2:c200")". Partial prototype code is as follows:

    sub doStuff()
         dim strRange as string
         range("c2").select
         strRange = "c2:c"
    
    '  I usually use Ctrl-End to get to the end of the active range.  I don't have the code to do that with me right now but you can probably get it with record macro.
    
         strRange = strRange & activeCell.row
         range(strRange).select
    end sub
    HTH.

    Bob

  3. #3
    Registered User
    Join Date
    01-04-2013
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    42

    Re: Unlimited Range for VlookUp in Macro Code

    Columns("C:C").Select
        Selection.Insert Shift:=xlToRight
        Range("C2").Select
        ActiveCell.FormulaR1C1 = _
            "=VLOOKUP(RC[-1],'[Previous Hold Transfer Report.XLS]HoldTransfer Over & Under 25 De'!C2,1,FALSE)"
        Range("C2").Select
        Selection.AutoFill Destination:=Range("C2")
        Range("C2").Select
    Last edited by fmfernandez11; 01-11-2013 at 11:14 AM.

  4. #4
    Registered User
    Join Date
    01-04-2013
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    42

    Re: Unlimited Range for VlookUp in Macro Code

    The error message appears in the
     Selection.AutoFill Destination:=Range("C2")
    I have changed the range to "C2:C" and I have also changed the formulas to the ones given above by bstubbs. I recently tried this as well :

    
        Columns("C:C").Select
        Selection.Insert Shift:=xlToRight
        Range("C2").Select
        ActiveCell.FormulaR1C1 = _
            "=VLOOKUP(RC[-1],'[Previous Hold Transfer Report.XLS]HoldTransfer Over & Under 25 De'!C2,1,FALSE)"
        Range("C2").Select
        lastRow = Range("C2").End(xlDown).Row
        Range("C2").AutoFill Destination:=Range(Range("C2" & lastRow))
        Range("C2").Select
    but it did not work. Im in a desperate need of help.
    Last edited by fmfernandez11; 01-11-2013 at 11:15 AM.

  5. #5
    Registered User
    Join Date
    01-04-2013
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    42

    Re: Unlimited Range for VlookUp in Macro Code

    Thanks for your help. I tried using it the first option and I have the Vlookup formula in one cell and I am trying to drag the formula down to the rest of the cells. That number of cells is undefined. Any ideas?

  6. #6
    Valued Forum Contributor
    Join Date
    09-04-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    463

    Re: Unlimited Range for VlookUp in Macro Code

    upload a sample file

  7. #7
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Unlimited Range for VlookUp in Macro Code

    We would love to continue to help you with your query, but first, before we can proceed, please see Forum Rule #3 about code tags and adjust accordingly...
    HTH
    Regards, Jeff

  8. #8
    Registered User
    Join Date
    01-04-2013
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    42

    Re: Unlimited Range for VlookUp in Macro Code

    I have adjusted according to the rules.

  9. #9
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: Unlimited Range for VlookUp in Macro Code

    Try this: The LastRow can be either the lastrow used in a particular column, OR it can be the last row used in your spreadsheet (but only one; you'll need to delete the line you DON'T want to use). After determining your LastRow, the VLookup is inserted in each cell in column C beginning with C2 to C and your LastRow.
    Sub ConductVlookupCopyOver() '
    Dim LastRow As Long
    '*********Choose ONLY one of these two options*********
    'for last row in a particular column
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    
    'for last row used in the worksheet
    LastRow = Cells.Find(What:="*", SearchOrder:=xlRows, _
         SearchDirection:=xlPrevious, LookIn:=xlValues).Row
    '*********Choose one of these two options*********
    
        Selection.Insert Shift:=xlToRight
        Range("C2:C" & LastRow).FormulaR1C1 = _
            "=VLOOKUP(RC[-1],'[Previous Hold Transfer Report.XLS]HoldTransfer Over & Under 25 De'!C2,1,FALSE)"
        
    End Sub
    Last edited by jomili; 01-16-2013 at 09:48 AM.

  10. #10
    Registered User
    Join Date
    01-04-2013
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    42

    Re: Unlimited Range for VlookUp in Macro Code

    Thank you so much! It worked greated.

    I really appreciate it.

  11. #11
    Registered User
    Join Date
    01-16-2013
    Location
    Hungary
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Unlimited Range for VlookUp in Macro Code

    It is very useful! Thank you very much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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