Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing Multiple For Each Statements Together with Next - VBA

Status
Not open for further replies.

blahbla1

Technical User
Jul 30, 2012
2
US
Hello,

My question is how to make 2 variables change at the same time with a "for each" statement in VBA. For example, I would like to make the C and D variable change together instead of only one changing. My problem is that every time my code is doing a loop, only the C variable is changing. This is being done on 2 different columns where I want it to go to the next row each time such as
1 2
2 4
5 6
________________
Sub stuff()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
ActiveSheet.Range(Cells(2, 2), Cells(65536, 2).End(xlUp)).Select
Selection.Copy
Range("C2").Select
ActiveSheet.Paste
Set ws = ActiveWorkbook.Sheets("CMRData")
Dim c As Range
Dim d As Range
For Each c In ActiveSheet.Range(Cells(2, 3), Cells(65536, 3).End(xlUp))
For Each d In ActiveSheet.Range(Cells(2, 6), Cells(65536, 6).End(xlUp))
If Not IsNumeric(d.Value) Then
If Len(c.Value) = 6 Then
c.Value = Left(c.Value, 5)
End If
ElseIf Not IsNumeric(d.Value) Then
If Len(c.Value) = 5 Then
c.Value = Left(c.Value, 5)
End If
Else
c.Value = ""
End If
Exit For
Next d
Next c
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
---------------------------

Thanks!
 
Use a single For looping the row number.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks. What would be an example of that? I am not quite sure what it would look like.
 
You can use one For Each...Next loop.
Within the first loop the "c" is a local variable that refers to processed cell, [tt]c.Offset(0,3)[/tt] is a cell that is three columns to the right. You can compare and change both.

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top