Solved

Excel VBA Syntax / usage : For each row in range

Posted on 2012-03-25
3
383 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

911 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