Problem to loop columns with array

elmnas

Board Regular
Joined
Feb 20, 2015
Messages
206
Hello people,

I am trying to create an array that each string in my array presents a column,

I want to loop each used cell in in the column then go to next string in the array(next column)



here is my code so far

Code:
Sub Test()


Dim i As Long
Dim ArrCol() As Variant
ArrCol = Array("T", "U", "V")


For i = LBound(ArrCol) To UBound(ArrCol)
Dim mystring As Variant
mystring = ArrCol(i)


'I need to declare the column and cell here but I dont sucess...


MsgBox mystring


Next


End Sub

Could someone help me?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi - here is one possible option, for example.

Code:
Sub Test()
Dim i As Long, c As Range, ArrCol() As Variant
ArrCol = Array("T", "U", "V")
For i = LBound(ArrCol) To UBound(ArrCol)
    For Each c In Range(ArrCol(i) & "1:" & ArrCol(i) & Range(ArrCol(i) & Rows.Count).End(xlUp).Row)
        Debug.Print c.Value 'for example
    Next c
Next i
End Sub
 
Upvote 0
Hi - here is one possible option, for example.

Code:
Sub Test()
Dim i As Long, c As Range, ArrCol() As Variant
ArrCol = Array("T", "U", "V")
For i = LBound(ArrCol) To UBound(ArrCol)
    For Each c In Range(ArrCol(i) & "1:" & ArrCol(i) & Range(ArrCol(i) & Rows.Count).End(xlUp).Row)
        Debug.Print c.Value 'for example
    Next c
Next i
End Sub

Thank you work as a charm I will look on it in more detail so I get more understanding for it

Cheers!
 
Upvote 0
How do I select a cell that is offset same row but in Column D? when I got C.value?

I know I can use

Code:
    c.Select
    
     ActiveCell.Offset(0, -3).Select
    MsgBox ActiveCell.Offset(0, -3).Value

But that doesn't help me because the value dont gonna be the same when its looping in column "U", "V" etc.
then I have to increase the value by -1 cause its one more step away


Thank you in advance
 
Last edited:
Upvote 0
How do I select a cell that is offset same row but in Column D? when I got C.value?

Something like this maybe..

Code:
Debug.Print Range("D" & c.Row).Value 'for example
 
Upvote 0
Something like this maybe..

Code:
Debug.Print Range("D" & c.Row).Value 'for example

work as a charm thank you!
Another question the code looping from row 1 to last used cell
but how do I make the code loop from row 2 to last used cell instead?
 
Upvote 0
how do I make the code loop from row 2 to last used cell instead?

You could try:

Rich (BB code):
    For Each c In Range(ArrCol(i) & "2:" & ArrCol(i) & Range(ArrCol(i) & Rows.Count).End(xlUp).Row)
 
Upvote 0
I still facing a problem,

here is my code at the moment,

I need to color the cell (C.value) if the value is lower or equal than the value in column "D" same row then color it green.
if the value in (C.value) its more than the value in column D same row color it red.

but it doesnt work why?

Have I missed anything?

see my code

Code:
Sub MyCelCounter()




Dim i As Long, c As Range, ArrCol() As Variant
ArrCol = Array("K")


    For i = LBound(ArrCol) To UBound(ArrCol)
        For Each c In Range(ArrCol(i) & "2:" & ArrCol(i) & Range(ArrCol(i) & Rows.Count).End(xlUp).Row)
         
       
       
        myColDV = Range("D" & c.Row).Value
       MyColv = Len(c.Value)
                  
                  
             ' MsgBox "This is value in column D:" & myColDV & "This is value in active Loop column:" & MyColv
              
              

              If myColDV > MyColv Then
              c.Interior.ColorIndex = 4
              End If
              
                 
     
                
        Next c
    Next i
    
End Sub

here is another code I made here do the loops work fine but I have hardcoded to column "K"

Code:
Sub MyColCounter()

Dim i As Long
Dim columnD As String
    For i = 2 To ActiveSheet.Range("D" & Rows.Count).End(xlUp).Row
    Dim CharCount As Integer

    CharCount = Len(Cells.Item(i, "K").Value)
    'MsgBox DCount & " " & CharCount
        If (Cells.Item(i, "D").Value > CharCount) Then
        Cells.Item(i, "K").Interior.ColorIndex = 4
        'MsgBox Cells.Item(i, "D").Value
            Else
            Cells.Item(i, "K").Interior.ColorIndex = 3
            End If
            If (Cells(i, 4).Value) <> "" Then
                If Cells(i, 4).Value = "NONE" Then
                'MsgBox Cells(i, 4).Value
                Cells(i, "K").Interior.ColorIndex = 4
            End If
        End If
    Next i
    
End Sub

I dont understand this works here but not the other one.
 
Last edited:
Upvote 0
I need to color the cell (C.value) if the value is lower or equal than the value in column "D" same row then color it green.
if the value in (C.value) its more than the value in column D same row color it red.

Is there a reason why you're not using conditional formatting?
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

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