简体   繁体   中英

Loop through Rows in Table and assign cell value to named variable

First question so here goes.

I am trying to create a loop that will work it's way through rows in a table and assign the cell values for respective columns to declared variables. The plan is to use these variables in logic statements and calculations with named range input cells, then transfer the output to a worksheet where it will form the basis of a pivot table.

The legacy workbook template I am using had a heap of vlookups, if statements, etc with no named ranges; it is messy and someone has inserted columns/rows which have broken some of the formulas.

I was doing something similar with MSAccess queries and vba some years ago but have not done much vba and none since or in excel.

So far, it addition to the MSDN Reference I have found the following helpful https://stackoverflow.com/a/21490552 and https://stackoverflow.com/a/12497229/6598860

'Declare variables
Dim Var1 As String    'To store Value from Table Column "Col1"
Dim Var2 As String    'To store Value from Table Column "Col2"
Dim Var3 As Variant    'To Store Value from Table Column "Date"
Dim SD As String    'To Store Value from Named Input Cell "StartDate"
Dim ED As String    'To Store Value from Named Input Cell "EndDate"
Dim counter As Integer    'Counter for pasting to row in worksheet "Dump"

'Set Date Range
SD = Range("StartDate").Value
ED = Range("EndDate").Value

'Set Counter
counter = 1

'Loop through rows in table "tblMyData"on Worksheet "Data" and
'assign values to variables (this bit is breaking down)
Dim lo As Excel.ListObject
Dim lr As Excel.ListRow
Set lo = Worksheets("Data").ListObjects("tblMyData")
For Each lr In lo.ListRows
    Var1 = Range(lr.Range, lo.ListColumns("Col1").Range).Value
    Var2 = Range(lr.Range, lo.ListColumns("Col2").Range).Value
    Var3 = Range(lr.Range, lo.ListColumns("Date").Range).Value

    'Logic Statement e.g.
    If SD > Var3 > ED Then
        Worksheets("Dump").Activate
        Cells(counter, 1).Value = Var1 * Var2
        Cells(counter, 2).Value = Var3
    Else

    End If
Next

I will be replacing the Variable Names and Column names with something more descriptive but hopefully this illustrates what I am trying to achieve. Any help or advice would be much appreciated. Thanks

Change

Range(lr.Range, lo.ListColumns("Col1").Range).Value

to

Intersect(lr.Range, lo.ListColumns("Col1").Range).Value .

That's how I'd reference the single cells at the intersection of a column and a row.

I am not sure if this is the only error, but I would be replacing the bottom IF with the piece of code below:

'Logic Statement e.g.
If SD > Var3 And Var3 > ED Then
    With Worksheets("Dump")
        .Cells(counter, 1).Value = Var1 * Var2
        .Cells(counter, 2).Value = Var3
    End With
Else
    'if you are not doing anything with the Else you can just remove it
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM