+ Reply to Thread
Results 1 to 3 of 3

Dynamic Range in For Loop

Hybrid View

phbryan Dynamic Range in For Loop 11-16-2022, 05:06 PM
Crooza Re: Dynamic Range in For Loop 11-17-2022, 03:31 AM
BadlySpelledBuoy Re: Dynamic Range in For Loop 11-17-2022, 03:35 AM
  1. #1
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Dynamic Range in For Loop

    Hello,

    I have the below code:
    Sub GoalSeekMultipleCells()
    Dim StartDate As Date
    Dim Find As Range
    Dim ReqSheet As Worksheet
    
    
    Set ReqSheet = Worksheets("REQ SHEET")
    StartDate = ReqSheet.Range("D1").Value
    Set Find = ReqSheet.Range("A3:AH3").Find(What:=StartDate)
    
    For p = 4 To 30
    Cells(p, "t").ClearContents
    If Cells(p, "x") < Range("X2").Value Then
    On Error Resume Next
    Cells(p, "t").Value = Cells(p, "i").Value * Range("X2").Value - Cells(p, "w").Value
    Cells(p, "t").Value = Round(Cells(p, "t").Value, 0)
    End If
    Next p
    End Sub
    This code works fine in the set ranges of T4:T30, W4:W30, and X4:X30 but now I would like to make these ranges dynamic.
    For example, based on the Date in D1, find that date in Row 3, and work in that area. Would this be possible using Offset?
    Attached Files Attached Files

  2. #2
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: Dynamic Range in For Loop

    Hi, Not sure I can get your macro working as I'm not sure exactly what it does BUT as far as converting the fixed references to columns to variable references maybe try this. I set variables for the MOH value in the same month, the MOH column in the prior month and this column value for the chosen month. I assumed column "i" stays fixed and the others move relative to the chosen date. The dates were 2023 but your field in D1 was 2022 so I changed that and it at least seems to make amendments to the correct columns.

    Sub GoalSeekMultipleCells()
    Dim StartDate As Date
    Dim Findcell, f As Range
    Dim newpo, moh, mohcol As String
    Dim ReqSheet As Worksheet
    
    
    Set ReqSheet = Worksheets("REQ SHEET")
    StartDate = ReqSheet.Range("D1").Value
    
     
    Set Findcell = ReqSheet.Range("A3:AH3").Find(what:=StartDate, LookIn:=xlValues)
    
    
    thiscol = Mid(Findcell.Address, 2, Len(Findcell.Address) - InStr(2, Findcell.Address, "$"))
    
    newpo = Mid(Findcell.Offset(0, -3).Address, 2, Len(Findcell.Offset(0, -3).Address) - InStr(2, Findcell.Offset(0, -3).Address, "$"))
    
    moh = Findcell.Offset(-1, 1).Value
    
    mohcol = Mid(Findcell.Offset(0, 1).Address, 2, Len(Findcell.Offset(0, 1).Address) - InStr(2, Findcell.Offset(0, 1).Address, "$"))
    
    
    
       For p = 4 To 30
    Cells(p, newpo).ClearContents
    If Cells(p, mohcol) < moh Then
    On Error Resume Next
    Cells(p, newpo).Value = Cells(p, "i").Value * moh - Cells(p, thiscol).Value
    Cells(p, newpo).Value = Round(Cells(p, newpo).Value, 0)
    End If
    Next p
    End Sub
    Happy with my advice? Click on the * reputation button below

  3. #3
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,944

    Re: Dynamic Range in For Loop

    Similar approach to Crooza's one above

    Sub GoalSeekMultipleCells()
        Dim StartDate As Date
        Dim Find As Range
        Dim ReqSheet As Worksheet
        Dim col As Long    
    
        Set ReqSheet = Worksheets("REQ SHEET")
        StartDate = ReqSheet.Range("D1").Value
        Set Find = ReqSheet.Range("A3:AH3").Find(What:=StartDate)
        col = ReqSheet.Range("3:3").Find(ReqSheet.Range("D1"), , xlValues, xlWhole).Column 
       
        For p = 4 To 30
            Cells(p, col).ClearContents
            If Cells(p, "x") < Range("X2").Value Then
                On Error Resume Next
                Cells(p, col).Value = Cells(p, "i").Value * Range("X2").Value - Cells(p, "w").Value
                Cells(p, col).Value = Round(Cells(p, col).Value, 0)
            End If
        Next p
    End Sub
    Note, it should really have error trapping because if the date in D1 does not appear in row 3 (as per your example file) then it will fall over.

    BSB

+ 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] Setting Dynamic Loop Range
    By radddogg in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-12-2017, 12:13 PM
  2. Filling Comboboxes with a loop from a dynamic range
    By dougman824 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-13-2017, 10:03 AM
  3. [SOLVED] Loop through a dynamic range.
    By Sluggoslabang in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 09-06-2016, 02:41 PM
  4. [SOLVED] Dynamic range in a loop
    By trailonu in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-30-2015, 07:22 PM
  5. [SOLVED] Merge Cells in Dynamic Range using For loop
    By crazymazy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-01-2013, 11:33 AM
  6. VBA Plot Multiple Charts Using Loop Through Dynamic Range
    By sanjeevpandey in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-30-2013, 07:38 AM
  7. Loop with dynamic range
    By mthomas in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-22-2011, 10:35 PM

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.6.0 RC 1