VBA excel:如何迭代列并根据条件更改偏移列

时间:2015-12-02 17:12:59

标签: excel vba excel-vba

我正在尝试更改迭代右侧的6列单元格。下面的vba似乎没有出现任何错误,但是当我运行Sub时,没有任何更新。有什么想法吗?

Sub RemoveDate()

Dim state
Dim dat
Dim cell As Range
Dim LastRow As Long


Sheets("RawData").Select

LastRow = Range("A" & Rows.Count).End(xlUp).Row

'If cell in column J is = state then change the cell 6 columns to right

For Each cell In Range("J1" & LastRow).Cells
    state = cell.Value
    dat = cell.Offset(0, 6).Value

    If state = "Pending" Then
        dat = "TBD"
    ElseIf state = "Under Review" Then
        dat = "TBD"
    ElseIf state = "BRD Refinement" Then
        dat = "TBD"
    ElseIf state = "On Hold" Then
        dat = "TBD"
    End If
Next


End Sub

2 个答案:

答案 0 :(得分:2)

仅仅因为有多个项目错误,这就结合了我的和FindWindow的评论:

Sub RemoveDate()

Dim state
Dim dat As Range
Dim cell As Range
Dim LastRow As Long


Sheets("RawData").Select

LastRow = Range("A" & Rows.Count).End(xlUp).Row

'If cell in column J is = state then change the cell 6 columns to right

For Each cell In Range("J1", Range("J" & LastRow)).Cells
    state = cell.Value
    Set dat = cell.Offset(0, 6)

    If state = "Pending" Then
        dat = "TBD"
    ElseIf state = "Under Review" Then
        dat = "TBD"
    ElseIf state = "BRD Refinement" Then
        dat = "TBD"
    ElseIf state = "On Hold" Then
        dat = "TBD"
    End If
Next


End Sub

使用dat = cell.Offset(0, 6).Value,您将变量dat设置为单元格的内容。然后使用if语句替换变量中的值而不是单元格。

通过使变量成为一个范围,并使用Set关键字,它引用单元格本身,并用if语句中设置的值替换单元格中的内容。

只需查看Select Case,就可以更轻松地使用多个elseif。用这个替换整个循环:

For Each cell In Range("J1", Range("J" & LastRow)).Cells
   Select Case cell.Value
        'because all the values result in the same outcome
        Case "Pending", "Under Review", "BRD Refinement", "On Hold"
            cell.Offset(0, 6) = "TBD"
        Case Else
    End Select        
Next

答案 1 :(得分:2)

为什么不这样:

Sub RemoveDate()
  Dim state As String, cell As Variant, LastRow As Long
  LastRow = Sheets("RawData").Cells(Rows.Count, 1).End(xlUp).Row
  For Each cell In Sheets("RawData").Range("J1:J" & LastRow).Cells
    state = cell.Value
    If state = "Pending" Or state = "Under Review" Or state = "BRD Refinement" Or state = "On Hold" Then
      cell.Offset(0, 6).Value = "TBD"
    End If
  Next
End Sub

在为很多细胞做的时候,它可能很慢,所以我更喜欢这样做:

Sub RemoveDate()
  Dim getRng As Variant, putRng As Variant, i As Long
  i = Sheets("RawData").Cells(Rows.Count, 1).End(xlUp).Row
  Set getRng = Sheets("RawData").Range("J1:J" & i).Value
  Set putRng = Sheets("RawData").Range("P1:P" & i).Value 

  While i > 1
    If getRng(i, 1) = "Pending" Or getRng(i, 1) = "Under Review" Or getRng(i, 1) = "BRD Refinement" Or getRng(i, 1) = "On Hold" Then
      putRng(i, 1) = "TBD"
    End If
    i = i - 1
  Wend
  Sheets("RawData").Range("P1:P" & Ubound(putRng)).Value = putRng
End Sub

如果P中还有公式,那么最好用.Value替换.Formula 第二个代码只是将范围的所有值都检查并更改为变量。然后使用i作为地址来获取变量中的值以比较/更改它(从最后一项到第一项)。这比使用真实细胞要快得多。但是,您的For Each ...在第一种方式中也是如此,但.Offset有点需要再次杀死它。作为最后一个操作,它将输出值的整个范围放回到工作表中。在一个步骤中为大量细胞执行此操作,也比仅对一些细胞一个接一个地执行此操作要快得多。这样它也会覆盖那些没有用旧值改变的细胞(这是一个有趣的事实:这仍然比离开它们更快)