Set Value from a cell moved 1 place to the right inside a For Loop

Tony mx

New Member
Joined
Oct 6, 2011
Messages
3
Hello Coders,
I have this loop, I get a range of specific cells and then I loop over this collection searching for a value, if this value exist I wanted to move one place to the right and then set a value on that cell, but for some reason when I put this offser(0,1) parameter, the loop became infinite, not sure why the code is doing this:
Code:
For Each c In Worksheets("Parameters").Range("A3:AR3").Cells
        
        adress_start = ""
        adress_letter = ""
        adress_end = ""
       
        If c.Value = "Row Order" Then
            
            adress_start = c.Address
            adress_letter = Mid(c.Address, 2, InStr(2, c.Address, "$") - 2)
            adress_end = adress_letter & "10"
           
            For Each cAux In Worksheets("Parameters").Range(adress_start & ":" & adress_end).Cells
               
                If cAux.Value <> "" And cAux.Value <> "Row Order" Then
                    
                    MsgBox "cAux adress" & cAux.Address
                    ' move one place to the right and set a value
                    cAux.Offset(0, 1).Value = "X value"
                    
                End If
            Next
        
        End If
 
    Next

Any help would be very appreciated,

Regards
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Not totally sure what you're doing but this might fit the bill.

Code:
Sub doittoit()
Dim X, Y As Long
Let X = 3
 
For Y = 1 To 44 'AR is column 44
    If Cells(X, Y).Value = "Row Order" Then
        'I can't figure out what you want to do but if it is populating the cell one place to the right just do this:
        'It looks like you're evaluating the next ten columns
        For Z = 1 To 10
            If Cells(X, Y + Z).Value <> "" And Cells(X, Y + Z).Value <> "Row Order" Then
                MsgBox ("xAux address is " & cells(x,Y+z).value)
                Cells(X, Y + Z).Value = "X Value"
            End If
        Next
    End If
Next
End Sub
 
Upvote 0
Your code works perfectly fine, but I think I didn't explain mysefl correctly take a look on this piece of code specially bolded lines:
Code:
For Each cAux In Worksheets("Parameters").Range(adress_start & ":" & adress_end).Cells
                
                If cAux.Value <> "" And cAux.Value <> "Row Order" Then
                    
                    MsgBox "cAux adress" & cAux.Address
                    [B]cAux.Select[/B]
                   [B] rightAdress = ActiveCell.Offset(0, 1).Address[/B]
[B]                    Worksheets("Parameters").Range(rightAdress).Value = "pls do it"[/B]
                 
                End If
                
            Next

The weird thing here is than after selecting cAux cell and moving one cell to the right from this cAux cell, I wanted to set a New Value for this rightAdress Cell (moved to right), for some reason is not setting any value and the loop returns to the cAux.Address again. If I comment the line where I wanted to set the new cell value the message boxes shows me the correct looping addresses .

Not sure how to fix this. Do you have any clue?
Regards
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,467
Members
448,965
Latest member
grijken

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