+ Reply to Thread
Results 1 to 20 of 20

Simplifying Code Using For-Next Loop

Hybrid View

inwalkedbud Simplifying Code Using... 07-02-2008, 11:39 PM
mikerickson This question was also asked... 07-03-2008, 12:37 AM
rylo Hi Try this.... For i... 07-03-2008, 01:04 AM
inwalkedbud thanks for the response. ... 07-03-2008, 01:25 AM
rylo Hi Does the macro have the... 07-03-2008, 01:48 AM
inwalkedbud i tried to add this code to... 07-12-2008, 01:01 PM
mikerickson VB does not treat strings... 07-12-2008, 07:03 PM
inwalkedbud mike-- is there any way to... 07-12-2008, 07:06 PM
mikerickson You should look into arrays. ... 07-12-2008, 10:57 PM
  1. #1
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101

    Simplifying Code Using For-Next Loop

    Looking to simplify the following code possibly with For-Next Loop.

    Sub Simplify()
    
    With Workbooks("Book1")
         
         .Sheets("Sheet1").Activate
         Call MySub_1
    
         .Sheets("Sheet2").Activate
         Call MySub_2
    
         .Sheets("Sheet3").Activate
         Call MySub_3
    
         .Sheets("Sheet4").Activate
         Call MySub_4
    
    End With
    
    End Sub
    Where the My Subs are private sub procedures. Thanks.

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    This question was also asked at
    http://www.mrexcel.com/forum/showthread.php?t=328091
    There are a couple of options there, both use Application.Run
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Try this....

    For i = 1 To 4
        Sheets("Sheet" & i).Activate
        Application.Run ThisWorkbook.Name & "!sheet" & i & ".MySub_" & i
    Next i
    rylo

  4. #4
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101
    thanks for the response. What if the sheets were named (ex. "A", "B", "C" etc...) instead of numbered?

  5. #5
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Does the macro have the same suffix as the sheet (ie A,B...) or does it still have the number suffix?

    rylo

  6. #6
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101
    sorry, but i tried to give a generic example in the first code section. code looks more like this. thanks.

    Sub Simplify()
    
    With Workbooks("MyWorkbook")
         
         .Sheets("A").Activate
         Call MySub_A
    
         .Sheets("B").Activate
         Call MySub_B
    
         .Sheets("C").Activate
         Call MySub_C
    
         .Sheets("D").Activate
         Call MySub_D
    
    End With
    
    End Sub

  7. #7
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101
    i tried to add this code to the previous post to eliminate the compile error but, i get new compile error: syntax error. any thoughts. thanks.

    Dim r_ As String
    Dim f_ As String
    Dim form As String
    
    r_ = "r_"
    f_ = "f_"
    form = ".formula"
    
    Dim i As Integer
    For i = 1 To 3
        r_ & i & form = f_ & i
    Next i
    
    End Sub

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    VB does not treat strings resulting from concatenation as variables.
    VB does not treat strings resulting from concatenation as properties, methods, functions, collections, or objects.

    The approach you have been trying throughout this thread (refering to the argument a_1 as "a_" & "1") is fatally flawed.

    If you describe what you are trying to do, a successful approach (probably involving arrays) might be found.
    Last edited by mikerickson; 07-12-2008 at 07:07 PM.

  9. #9
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101
    mike--

    is there any way to reduce the redundancy then? thanks. bud.

  10. #10
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    You should look into arrays.
    The code above could be done like this:
    Dim i As Long
    Dim myRange(1 To 3) As Range
    
    For i = 1 To 3
        Set myRange(i) = Cells(1, i)
    Next i
    
    Dim formulaString(1 To 3) As String
    
    For i = 1 To 3
        formulaString(i) = "=" & CStr(2 * i - 1) & "+" & CStr(2 * i)
    Next i
    
    For i = 1 To 3
        myRange(i).FormulaR1C1 = formulaString(i)
    Next i
    This could be further simplified, to one loop
    Dim myRange(1 To 3) As Range
    Dim formulaString(1 To 3) As String
    Dim i As Long
    
    For i = 1 To 3
        Set myRange(i) = Cells(1, i)
        formulaString(i) = "=" & CStr(2 * i - 1) & "+" & CStr(2 * i)
        myRange(i).Formula = formulaString(i)
    Next i
    Starting with arrays and using For..Next loops makes it easier to see this version.
    Dim i As Long
    For i = 1 to 3
        Cells(1,i).FormulaR1C1 = "=" & CStr(2 * i - 1) & "+" & CStr(2*i)
    Next i
    Unless you need myRange or formulaString later in the routine, this would be the way to go.

+ 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