VBA:在可变范围内复制单元格值

时间:2017-04-26 19:43:18

标签: vba excel-vba excel

在下面的代码中,我最难确定可变范围内的特定单元格" rngCell"。在" If"声明,我想复制rngCell(活动单元所在的)列或行中的特定单元格,而不是rngCell的值。我尝试过使用偏移,但一直都失败了。示例:如果rngCell位于e42,我可能需要e2或a42的值。

谢谢。

Dim rngCell As Range
Dim lngLstRow As Long
Dim ws As Worksheet, resultsWS As Worksheet

lngLstRow = ws.UsedRange.Rows.Count
Worksheets("FileShares").Select

j = 4
p = 1
q = 4
g = 6

Dim k&                      
For k = 9 To 50
With ws
    For Each rngCell In .Range(.Cells(8, k), .Cells(lngLstRow, k))

        For i = LBound(maxKeywords) To UBound(maxKeywords)
            If rngCell.Value = maxKeywords(i) And rngCell.Interior.ColorIndex = 3 Then
                resultsWS.Cells(g, 2).Offset(j + p, 0) = rngCell.Value

            g = g + 1

            j = q + p - 5 'Used to start at row 8 and every row after
            End If
        Next i
    Next rngCell

End With
Next k

1 个答案:

答案 0 :(得分:1)

如果rngCell是E42,那么:

rngCell.EntireRow.Cells(1)    '>>A42
rngCell.EntireColumn.Cells(2) '>>E2

ws.Cells(rngCell.Row, 1)      '>>A42
ws.Cells(2, rngCell.Column)   '>>E2