refer to range in loop

mkw4949

Board Regular
Joined
Apr 28, 2006
Messages
113
I'm creating a simple loop that will go through a given range and every time the cell contains the work "UNIT" as the first 4 characters I want the macro to to organize the data. The problem is that I can't seem to get VBA to any actions to cells in the target range. here is the code I'm using:

Code:
Sub SortData()

Dim rDataTable As Range
Dim c As Range
Dim rTarget As Range
Dim x As Integer

Set rDataTable = Sheet1.Range("A21:A34")
Set rTarget = Sheet1.Range("H21")
x = 0

For Each c In rDataTable
Debug.Print Left(c.Value, 4)
Debug.Print c.Value
    If Left(c.Value, 4) = "UNIT" Then
        Sheet1.Cells(rTarget.Row + x, rTarget.Column) = c.Value
        x = x + 1
    Else: End If
Next c

End Sub

Any suggestions would be greatly appreciated. Thanks.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Thanks for the suggestion but that didn't work either. The problem is that my code does not seem to recognize the "c" variable. For example the two Debug.Prints that I am using return blanks as values which I don't understand. If I do a "Debug.Print c.Address" I get the addresses for "c". . .
 
Upvote 0
Ummmm. . . You're right. I was being an idiot. I wrote the macro in the wrong workbook so when ever I tested it it was looking at the wrong workbook and of course not doing anything to the intended workbook. . . Thanks for your time. I appreciate it. Take care.

Strange...
It worked perfect for me

M.
 
Upvote 0
Ummmm. . . You're right. I was being an idiot. I wrote the macro in the wrong workbook so when ever I tested it it was looking at the wrong workbook and of course not doing anything to the intended workbook. . . Thanks for your time. I appreciate it. Take care.

It can happen with anyone. Tks for your kind words.

All the best

M.
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,233
Members
452,898
Latest member
Capolavoro009

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