Solved

Excel VBA Syntax / usage : For each row in range

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

734 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