Solved

Excel VBA Syntax / usage : For each row in range

Posted on 2012-03-25
3
382 Views
Last Modified: 2012-03-25
Hi,

I have a named range which is 6 columns wide and (say) 100 rows long.

I've got the following code where I am trying to filter out the range for "TaskID" and then build a string using the values of all the columns or matching rows.


For Each Row In rngComments
    If Row.Value = TaskID Then
        For j = 1 To 6
            strComBuild = strComBuild & "- " & Row.Item(j)
        Next j
    strComBuild = strComBuild & Chr(13)
    End If
Next

Open in new window


I have two questions really :
1 - How do I refer to each of the column values in the rows collection ?
2 - Is this the best way to do this ?

Thanks in advance.

Craig
0
Comment
Question by:slobber72
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 360 total points
ID: 37762546
Craig

You can loop through the rows and columns like this.
For Each rw In rngComment.Rows

          For Each cl in rw.Cells.Columns

         Next cl

Next rw

Open in new window


Is it a specific column in rngComments do you want to check for the TaskID?

Would that column be included in the string you are building or would it be just the rest of the values in the row?
0
 

Author Comment

by:slobber72
ID: 37762604
I would like to know how to refer to a specific column.

In this case I will be using all the columns to form the string, but I would love to know how to refer to a specific column.

Thanks in advance,

Craig
0
 
LVL 33

Assisted Solution

by:Norie
Norie earned 360 total points
ID: 37762622
If we use the rw from the code I posted this will refer to the 1st column in the row where rw is the row.
rw.Columns(1).Value

Open in new window

This will refer to the 3rd column.
rw.Columns(3).Value

Open in new window


To use the same syntax for looping through all the columns in the row.
For I = 1 To rw.Columns.Count

      strString = strString & "-" & rw.Columns(I).Value

Next I

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now