2017-08-04 97 views
0

我正在嘗試通過Excel文檔中的一系列字段,如果它們不是空的,請將它們以及單元格C和D的值添加到集合中。
xlWB從調用此函數的Sub中傳入。
它確認我正在測試的罰款和i.Value返回X,這是我所期望的。
i.Offset(0, -9).Valuei.Offset(0, -8).Value都在調試窗口中返回爲空。VBA Offset.Value對於完整單元格返回空值

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 

謝謝你的幫助!

+0

什麼是'KogrRng'? – pnuts

回答

0

這應該這樣做

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 

偏移-9和-8從L列對應於列C和D. 排位i相xlwb.sheets( 「KoGr」)給出了一個錯誤,這已經是符合KoGrRng的定義。

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