Home > database >  VBA how to get property out of row for "Each Row"
VBA how to get property out of row for "Each Row"

Time:10-19

I'm going insane trying to figure this relatively simple thing out. How do I extract the property in a "For Each" row loop?

Here's a version of my code

Dim wb as Workbook
Dim ws as Worksheet
Dim curr_region as range

set wb = ThisWoorkbook
set ws = wb.Worksheets(1)
set curr_region = ws.Range("A1").CurrentRegion 'this range is a 9 columns and 24 rows

For each curr_row in curr_region.Rows
      curr_row(1,2).Value2 ''''error occurs here

Next

The loop works up until I try to get the value out of curr_row and I don't undestand what's wrong. In the locals window, I can see the "Value2" property (is that the right term?) which expands into "Value2(1)" which further expands into Value2(1,1)....Value2(1,9)

How do I access Value2(1,2) as it appears in the locals window? I've tried the following:

curr_row(1,2).Value
curr_Row(1,2).Value2
curr_row.Value2(1,2)
curr_row.Value(1,2)
curr_row(1,2)
curr_row(1)

Nedless to say I'm going insane because none of these work, what am I doing wrong here? Beyond this specific example, what is the general syntax to getting to a property of an object? I thought once I can see it locals, it would be a straightforward mapping, but I'm so confused. Just driving me insane here.

CodePudding user response:

It seems straightfoward to use Cells here:

curr_row.Cells(1, 2).Value2

For example:

Dim foo As Variant
foo = curr_row.Cells(1, 2).Value2
Debug.Print curr_row.Cells(1, 2).Value2
curr_row.Cells(1, 2).Value2 = "bar"

Best practice is to declare all your variables:

Dim curr_row As Range
  • Related