根据两个标准水平循环

时间:2016-03-10 05:54:54

标签: excel-vba vba excel

我有一个表格,我想在其中创建一个VBA代码,以水平方式按升序排列日期,但是当我激活代码时,它会给我一个带有最后一个条件的日期。我的代码如下:

 Private Sub Worksheet_Activate()
 Dim a As Range, ab As Range, b As Range, bc As Range
 Set ab = Range("B3:E3")
 Set bc = Range("A2:D2")
 For Each a In ab
 For Each b In bc
 If Cells(3, 1) <> "" Then
     Cells(3, a.Column) = Range("A3") + Cells(2, b.Column)
 Else
     Cells(3, a.Column) = ""
 End If
 Next
 Next
 End Sub

enter image description here

1 个答案:

答案 0 :(得分:0)

是的,我更改了Ranges识别名称,但最重要的部分是解决方案。

Private Sub Worksheet_Activate()
      On Error Resume Next
      Application.ScreenUpdating = False
      Application.Calculation = xlCalculationManual
      Dim cng As Range, rng As Range
      Set rng = Range("B3:E3")
      For Each cng In rng
      If Cells(3, 1) <> "" Then
            cng.Offset(0, 0).Value = cng.Offset(0, -1) + 1
      ElseIf Cells(3, 1) = "" Then
            cell.Offset(0, 5).Value = ""
      End If
      Next
      Application.Calculation = xlCalculationAutomatic
      Application.ScreenUpdating = True
End Sub