+ Reply to Thread
Results 1 to 6 of 6

Thread: Selecting Range with Variable Number of Columns

  1. #1
    Registered User
    Join Date
    02-08-2012
    Location
    NJ
    MS-Off Ver
    Excel 2010
    Posts
    2

    Selecting Range with Variable Number of Columns

    I am trying to apply a formula on one worksheet that references cells on a previous worksheet. all cells have the same formula (relative references) but the number of columns may change depending on the data inputted in the "Data" tab.

    i would like to know if there is a cleaner way to have a formula applied to a range with variable number of columns.

    currently i am using two loops and applying the formula to each cell until Column A is empty and Row 1 is empty:

    ...
    
    Dim c As Integer, d As Integer
    
    c = 1
    d = 0
    
    Application.ScreenUpdating = False
    
    Sheets("Log Return").Select
    Range("B7").Select
    
    Do Until IsEmpty(ActiveCell.Offset(-6, 0))
        Do Until IsEmpty(ActiveCell.Offset(0, -c))
        ActiveCell.FormulaR1C1 = "=LOG('Data'!RC/'Data'!R[-5]C)"
        ActiveCell.Offset(1, 0).Select
        d = d + 1
        Loop
        ActiveCell.Offset(-d, 1).Select
        d = 0
        c = c + 1
        Loop
    c=1
    Application.ScreenUpdating = True
    
    ...
    what i want is to apply this formula:
    FormulaR1C1 = "=LOG('Data'!RC/'Data'!R[-5]C)"
    to a range that may have 4 columns or may have 104 columns (or any number)

    my current code takes a while to run because it runs through each cell independently. any help to make this smoother is much appreciated!
    Last edited by tallandpoofy; 02-13-2012 at 12:24 PM.

  2. #2
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,384

    Re: Selecting Range with Variable Number of Columns

    You can check for the last column using =
     lcol=range("XFD1").End(xltoLeft).Column
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    02-08-2012
    Location
    South Suffolk
    MS-Off Ver
    Excel 2007
    Posts
    102

    Re: Selecting Range with Variable Number of Columns

    Is it possible for your data to have named ranges? The formula could then reference a range name. This could be either worksheet-specific or at the workbook level.
    A named range of variable size can be created using the following as an example
    Sub test()
    Dim rWR1 As Range
    Dim lColumns As Long
    
    lColumns = 30
    Set rWR1 = Worksheets("Sheet1").Range("A2").Resize(1, lColumns)
    
    Worksheets("Sheet1").Names.Add Name:="MyTest", RefersTo:=rWR1
    
    End Sub
    If refering to a sheet-level name in a folmula on a different sheet, the sheet name needs to be added.
    Having a relevant name in a formula makes the logic easier to follow.

    Also, using copy & paste in code works well - set up the first column of formulae, establish the target range, copy first column then paste to target.
    Hope this helps.
    Last edited by AndyPS; 02-13-2012 at 10:05 AM.

  4. #4
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Selecting Range with Variable Number of Columns

    Perhaps
    Dim lRow as long, lCol as long
    With Sheets("Log Return")
    lRow = .cells(.rows.Count, "A").end(xlup).row
    lCol = .cells(1, .columns.count).End(xltoleft).column
    .range("B7", .cells(lrow, lcol)).formular1c1 = "=LOG('Data'!RC/'Data'!R[-5]C)"
    End With
    Good luck.

  5. #5
    Registered User
    Join Date
    02-08-2012
    Location
    NJ
    MS-Off Ver
    Excel 2010
    Posts
    2

    [SOLVED] Re: Selecting Range with Variable Number of Columns

    Thanks for the help everyone!

    great tips and solutions. the ".end(xlup/xltoleft)" function is good to know!

    -Ramy

  6. #6
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Selecting Range with Variable Number of Columns

    Glad to assist.

    Please see the FAQ link at the top of the page for instructions on marking the thread Solved. (you need to change the first post, not the last)
    Good luck.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0