Solved

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

Posted on 2014-01-12
3
276 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 26

Expert Comment

by:MacroShadow
Comment Utility
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
Comment Utility
Thanks MS, if I move my sheets within the workbook, would this skew my index order then?
0
 
LVL 26

Accepted Solution

by:
MacroShadow earned 500 total points
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now