Solved

Excel VBA Syntax / usage : For each row in range

Posted on 2012-03-25
3
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

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 34

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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

695 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