Solved

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

Posted on 2014-01-12
3
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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