VBA Offset.Value returns empty for a full cell

时间:2017-08-04 12:27:24

标签: excel vba ms-access

I am trying to go through a range of fields in an Excel document and if they are not empty add them and the values of cells C and D to a collection.
xlWB is passed in from the Sub that calls this one.
It recognizes the value I am testing for fine and i.Value returns X, which is what I expect.
But i.Offset(0, -9).Value and i.Offset(0, -8).Value are both returned empty in the debug window.

Dim KogrRng As range
Dim i As range
Dim nField As String
Dim nValue As String
Dim fldCol As Collection
Dim valCol As Collection

Set KogrRng = xlWB.Sheets("KoGr").range("L22:L500")

For Each i In KogrRng
        If (i.Value = "") Then
            'skip
        Else
            nField = xlWB.Sheets("KoGr").i.Offset(0, -9).Value & " " & xlWB.Sheets("KoGr").i.Offset(0, -8).Value
            nValue = i.Value
            Debug.Print (nField & ", " & nValue)
            fldCol.ADD nField
            valCol.ADD nValue
        End If
    Next i

Thank you for any help!

2 个答案:

答案 0 :(得分:0)

this should do it

Dim KogrRng As Range
Dim i As Range
Dim nField As String
Dim nValue As String
Dim fldCol As Collection
Dim valCol As Collection

Set KogrRng = xlwb.Sheets("KoGr").Range("L22:L500")

For Each i In KogrRng
        If (i.Value = "") Then
            'skip
        Else
            nField = i.Offset(0, -9).Value & " " & i.Offset(0, -8).Value
            nValue = i.Value
            Debug.Print (nField & ", " & nValue)
            fldCol.Add nField
            valCol.Add nValue
        End If
    Next i

offset -9 and -8 from column L correspond to column C and D. qualifying i with xlwb.sheets("KoGr") gives an error, it is already qualified in the definition of KoGrRng.

答案 1 :(得分:0)

请尝试以下代码!。

Dim KogrRng As Range
Dim i As Range
Dim nField As String
Dim nValue As String
Dim fldCol As New Collection
Dim valCol As New Collection

Set KogrRng = Sheets("KoGr").Range("L22:L500")

For Each i In KogrRng
        If (i.Value = "") Then
            'skip
        Else
            nField = i.Offset(0, -9).Value & " " & i.Offset(0, -8).Value
            nValue = i.Value
            Debug.Print (nField & ", " & nValue)
            fldCol.Add nField
            valCol.Add nValue
        End If
    Next i