+ Reply to Thread
Results 1 to 6 of 6

How to reference a cell within a dynamic range

  1. #1
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    How to reference a cell within a dynamic range

    Below is my current code which is not working. The variable "SH" is not being read and the result is no date is being picked up from the data. What I am trying to do with this data is reference a file for each row of the data, a file that is being referenced from the date in column K for each row. With the referenced file for each row, execute a VLOOKUP. If the file doesn't exist for that row, enter a 0 and move on to the next row -- each loop should only bring in one data point, the results of the VLOOKUP for that row.

    Please Login or Register  to view this content.
    Last edited by xcelnovice101; 03-13-2013 at 03:57 PM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,598

    Re: How to reference a cell within a dynamic range

    worksheetfuntion is redundant, fix:
    DTS = Range("K" & Rows.Count).End(xlUp).Row

    Suppose you use 100 rows in column K then 100 will be loaded into the variable DTS, then the line:

    Set DT = Sheets("Dividends").Range("K2" & DTS)

    will append 100 to K2 resulting in range("K2100") note that this is a single cell

    fix:
    Set DT = Sheets("Dividends").Range("K2:K" & DTS)
    Last edited by protonLeah; 03-13-2013 at 02:01 PM.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: How to reference a cell within a dynamic range

    Great! Thanks for your help protonLeah! I have updated the code to reflect your recommendations and adjusted a couple of other lines as well. Now, I need to be able to reference the cell to the right of SH in the formula section of the code, highlighted in red.

    Please Login or Register  to view this content.

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,598

    Re: How to reference a cell within a dynamic range

    To access the cell to the right of the current SH cell, use:
    SH.Offset(0,1).Formula...
    Where "0" means the same row & 1 means 1 cell to the right.

  5. #5
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: How to reference a cell within a dynamic range

    Thank you so much protonLeah! I have one last question, at least I think just one. Would the following line of code work, or what would be the best way to reference the appropriate cell in column "A" for each loop's SH? I keep getting the saem results which tells me the code was just pulling in the data tied to cell $A2 in my original code.

    Please Login or Register  to view this content.

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,598

    Re: How to reference a cell within a dynamic range

    try:
    SH.offset(0, 1).Formula= "=IFERROR(VLOOKUP(" & SH.Offset(0, -10).Value & ",'" & Folder1 & "[" & Eagle & "]" & SHTNM1 & "'!$A$2:$E$1000,5,0),0)"

+ 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