For each looping right but output wrong...

NewbieMan

New Member
Joined
Nov 25, 2017
Messages
33
I first of all thank you all for your efforts in helping ...I "think " I am slowly grasping things more and more everyday but with For Each statements, I still have some problems..looking at the code below

Code:
Dim MyPlayer As Range




For Each MyPlayer In Range("C3:C17")


If MyPlayer.Value = "FR" Then
    Range("S3:S17").Value = 1
ElseIf MyPlayer.Value = "SO" Then
    Range("S3:S17").Value = 2
ElseIf MyPlayer.Value = "JR" Then
    Range("S3:S17").Value = 3
ElseIf MyPlayer.Value = "SR" Then
    Range("S3:S17").Value = 4
ElseIf MyPlayer.Value = "" Then
    Range("S3").Value = 4
End If


Next MyPlayer

What I wish to happen is that For Each value in the range of C3:C17 the number should populate the corresponding S3:S17 cell For example, the first loop checks C3, and finds the string FR, it therefore populate S3 with the integer 1. Next it should check cell C4 and whichever string it finds, FR, SO, JR, or SR it should populate the corresponding S cell (or S4) , C5 answer would populate cell S5 with the integer. My loop seems to be able to check the C range correctly, finds the right integer to populate, but its populating the entire range of S3:S17 rather than a single cell. How would I change this so that C3 integer value populates S3, C4 integer value populates S4, C5 integer value goes to S5 etc?

Thanks for the help.
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try
Code:
For Each myplayer In Range("C3:C17")

   If myplayer.Value = "FR" Then
       myplayer.Offset(, 16).Value = 1
   ElseIf myplayer.Value = "SO" Then
       myplayer.Offset(, 16).Value = 2
   ElseIf myplayer.Value = "JR" Then
       myplayer.Offset(, 16).Value = 3
   ElseIf myplayer.Value = "SR" Then
       myplayer.Offset(, 16).Value = 4
   ElseIf myplayer.Value = "" Then
       myplayer.Offset(, 16).Value = 4
   End If


Next myplayer
Not sure if the last line is correct because you are using 4 for both SR & ""
 
Last edited:
Upvote 0
Try
Code:
For Each myplayer In Range("C3:C17")

   If myplayer.Value = "FR" Then
       myplayer.Offset(, 16).Value = 1
   ElseIf myplayer.Value = "SO" Then
       myplayer.Offset(, 16).Value = 2
   ElseIf myplayer.Value = "JR" Then
       myplayer.Offset(, 16).Value = 3
   ElseIf myplayer.Value = "SR" Then
       myplayer.Offset(, 16).Value = 4
   ElseIf myplayer.Value = "" Then
       myplayer.Offset(, 16).Value = 4
   End If


Next myplayer
Not sure if the last line is correct because you are using 4 for both SR & ""


Thank alot Fluff...worked great. I didnt even think to use an offset ..****! hopefully more experience will broaden the mind :) Thanks again PS - yes the last bit is meant to be set with a 4 as well :)

Cheers
 
Upvote 0
Glad to help & thanks for the feedback

FYI
you could also do it like this
Code:
Dim myplayer As Long
For myplayer = 3 To 17

   If Range("C" & myplayer).Value = "FR" Then
       Range("S" & myplayer).Value = 1
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,596
Members
452,657
Latest member
giadungthienduyen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top