Solved

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

Posted on 2014-01-12
3
288 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 27

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 27

Accepted Solution

by:
MacroShadow earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
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.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

943 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

5 Experts available now in Live!

Get 1:1 Help Now