Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

running for loop for different sheets based on value in other sheet

Posted on 2014-01-12
3
Medium Priority
?
323 Views
Last Modified: 2014-01-12
Folks

Below is my code and this calls up values in sheet 8

  For i = 2 To Sheet8.UsedRange.Rows.Count
      val1 = Range("A" & i).Value
      val2 = Range("B" & i).Value

How do I change the specifc loop to select different sheets based on another sheet value, i.e. like so:


 If Sheet1.Range("F1") = "5" Then
   For i = 2 To Sheet8.UsedRange.Rows.Count
 ElseIf Sheet1.Range("F1") = "9" Then
    For i = 2 To Sheet9.UsedRange.Rows.Count
 ElseIf Sheet1.Range("F1") = "O" Then
      For i = 2 To Sheet10.UsedRange.Rows.Count
End If

all help will do
0
Comment
Question by:rutgermons
  • 2
3 Comments
 
LVL 28

Expert Comment

by:MacroShadow
ID: 39774735
Try this:

Sub Demo()

    Dim intSheetIndex As Integer

    Select Case Sheet1.Range("F1")
        Case Is = "5"
            intSheetIndex = 8
        Case Is = "9"
            intSheetIndex = 9
        Case Is = "O"
            intSheetIndex = 10
    End Select

    For i = 2 To Sheets(intSheetIndex).UsedRange.Rows.Count
        val1 = Range("A" & i).Value
        val2 = Range("B" & i).Value
    Next

End Sub

Open in new window

0
 

Author Comment

by:rutgermons
ID: 39774817
Thanks MS, if I move my sheets within the workbook, would this skew my index order then?
0
 
LVL 28

Accepted Solution

by:
MacroShadow earned 2000 total points
ID: 39774830
Yes it will, if you want you can adjust it to use the worksheet names.

Sub Demo()

    Dim strSheetName As String

    Select Case Sheet1.Range("F1")
        Case Is = "5"
            strSheetName = "Sheet8"
        Case Is = "9"
            strSheetName = "Sheet9"
        Case Is = "O"
            strSheetName = "Sheet10"
    End Select

    For i = 2 To Sheets(strSheetName).UsedRange.Rows.Count
        val1 = Range("A" & i).Value
        val2 = Range("B" & i).Value
    Next

End Sub

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question