Solved

Excel VBA Syntax / usage : For each row in range

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

856 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