如果"标题值"不存在移动到下一个?

时间:2018-01-12 11:01:30

标签: vba

我需要快速建议(请参阅下面的代码)。每当"标题"时,我希望代码继续下一行。例如,("AW1")不存在。

所以,例如代码遇到这一行:

Col = Application.WorksheetFunction.Match(WS2.Range("AW1").Value, .Rows("1:1"), False)
.Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng16.Rows.Count).Value = Rng16.Value

如果("AW1")不存在,我希望此时不执行任何操作,而是转到("AX1")

Col = Application.WorksheetFunction.Match(WS2.Range("AX1").Value, .Rows("1:1"), False)
.Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng17.Rows.Count).Value = Rng17.Value

等等,每个人都有。

Sub TransData4Headers()

Dim WS1 As Worksheet
Dim WS2 As Worksheet

Dim Rng As Range, Rng1 As Range, Rng2 As Range, Rng3 As Range, Rng4 As Range, Rng5 As Range, Rng6 As Range, Rng7 As Range, Rng8 As Range, Rng9 As Range
Dim Rng10 As Range, Rng11 As Range, Rng12 As Range, Rng13 As Range, Rng14 As Range, Rng15 As Range, Rng16 As Range
Dim Rng17 As Range, Rng18 As Range, Rng19 As Range, Rng20 As Range, Rng21 As Range, Rng22 As Range

Dim Col As Long
On Error Resume Next

Set WS1 = Sheets("Main")
Set WS2 = Sheets("Bckg")

Set Rng = WS2.Range("AG2:AG2")
 Set Rng1 = WS2.Range("AH2:AH2")
  Set Rng2 = WS2.Range("AI2:AI2")
   Set Rng3 = WS2.Range("AJ2:AJ2")
    Set Rng4 = WS2.Range("AK2:AK2")
     Set Rng5 = WS2.Range("AL2:AL2")
      Set Rng6 = WS2.Range("AM2:AM2")
       Set Rng7 = WS2.Range("AN2:AN2")
        Set Rng8 = WS2.Range("AO2:AO2")
         Set Rng9 = WS2.Range("AP2:AP2")
          Set Rng10 = WS2.Range("AQ2:AQ2")
           Set Rng11 = WS2.Range("AR2:AR2")
            Set Rng12 = WS2.Range("AS2:AS2")
           Set Rng13 = WS2.Range("AT2:AT2")
          Set Rng14 = WS2.Range("AU2:AU2")
         Set Rng15 = WS2.Range("AV2:AV2")
        Set Rng16 = WS2.Range("AW2:AW2")
       Set Rng17 = WS2.Range("AX2:AX2")
      Set Rng18 = WS2.Range("AY2:AY2")
     Set Rng19 = WS2.Range("AZ2:AZ2")
    Set Rng20 = WS2.Range("BA2:BA2")
   Set Rng21 = WS2.Range("BB2:BB2")
  Set Rng22 = WS2.Range("BC2:BC2")

 With WS1

Col = Application.WorksheetFunction.Match(WS2.Range("AG1").Value, .Rows("1"), False)
.Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng.Rows.Count).Value = Rng.Value

 Col = Application.WorksheetFunction.Match(WS2.Range("AH1").Value, .Rows("1"), False)
 .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng1.Rows.Count).Value = Rng1.Value

  Col = Application.WorksheetFunction.Match(WS2.Range("AI1").Value, .Rows("1"), False)
  .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng2.Rows.Count).Value = Rng2.Value

   Col = Application.WorksheetFunction.Match(WS2.Range("AJ1").Value, .Rows("1:1"), False)
   .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng3.Rows.Count).Value = Rng3.Value

    Col = Application.WorksheetFunction.Match(WS2.Range("AK1").Value, .Rows("1:1"), False)
    .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng4.Rows.Count).Value = Rng4.Value

     Col = Application.WorksheetFunction.Match(WS2.Range("AL1").Value, .Rows("1:1"), False)
     .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng5.Rows.Count).Value = Rng5.Value

      Col = Application.WorksheetFunction.Match(WS2.Range("AM1").Value, .Rows("1:1"), False)
      .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng6.Rows.Count).Value = Rng6.Value

       Col = Application.WorksheetFunction.Match(WS2.Range("AN1").Value, .Rows("1:1"), False)
       .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng7.Rows.Count).Value = Rng7.Value

        Col = Application.WorksheetFunction.Match(WS2.Range("AO1").Value, .Rows("1:1"), False)
        .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng8.Rows.Count).Value = Rng8.Value

         Col = Application.WorksheetFunction.Match(WS2.Range("AP1").Value, .Rows("1:1"), False)
         .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng9.Rows.Count).Value = Rng9.Value

          Col = Application.WorksheetFunction.Match(WS2.Range("AQ1").Value, .Rows("1:1"), False)
          .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng10.Rows.Count).Value = Rng10.Value

           Col = Application.WorksheetFunction.Match(WS2.Range("AR1").Value, .Rows("1:1"), False)
           .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng11.Rows.Count).Value = Rng11.Value

            Col = Application.WorksheetFunction.Match(WS2.Range("AS1").Value, .Rows("1:1"), False)
            .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng12.Rows.Count).Value = Rng12.Value

           Col = Application.WorksheetFunction.Match(WS2.Range("AT1").Value, .Rows("1:1"), False)
           .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng13.Rows.Count).Value = Rng13.Value

          Col = Application.WorksheetFunction.Match(WS2.Range("AU1").Value, .Rows("1:1"), False)
          .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng14.Rows.Count).Value = Rng14.Value

         Col = Application.WorksheetFunction.Match(WS2.Range("AV1").Value, .Rows("1:1"), False)
         .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng15.Rows.Count).Value = Rng15.Value

        Col = Application.WorksheetFunction.Match(WS2.Range("AW1").Value, .Rows("1:1"), False)
        .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng16.Rows.Count).Value = Rng16.Value

       Col = Application.WorksheetFunction.Match(WS2.Range("AX1").Value, .Rows("1:1"), False)
       .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng17.Rows.Count).Value = Rng17.Value

      Col = Application.WorksheetFunction.Match(WS2.Range("AY1").Value, .Rows("1:1"), False)
      .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng18.Rows.Count).Value = Rng18.Value

     Col = Application.WorksheetFunction.Match(WS2.Range("AZ1").Value, .Rows("1:1"), False)
     .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng19.Rows.Count).Value = Rng19.Value

    Col = Application.WorksheetFunction.Match(WS2.Range("BA1").Value, .Rows("1:1"), False)
    .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng20.Rows.Count).Value = Rng20.Value

   Col = Application.WorksheetFunction.Match(WS2.Range("BB1").Value, .Rows("1:1"), False)
   .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng21.Rows.Count).Value = Rng21.Value

  Col = Application.WorksheetFunction.Match(WS2.Range("BC1").Value, .Rows("1:1"), False)
  .Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng22.Rows.Count).Value = Rng22.Value

End With

End Sub

2 个答案:

答案 0 :(得分:0)

您可以使用if statement来有条件地执行代码。

我已使用for...next statement替换了代码中的重复内容。这有几个好处:

  • 它提高了代码的可读性
  • 降低了更新代码的复杂性
  • 减少了所需变量的数量

更新功能:

Sub TransData4Headers()
    Dim WS1 As Worksheet
    Dim WS2 As Worksheet
    Dim currentRange As Range
    Dim currentCol As Long
    Dim Col As Long

    Set WS1 = Sheets("Main")
    Set WS2 = Sheets("Bckg")

    ' Column 33 = AG.
    ' Column 55 = BC.
    For currentCol = 33 To 55

        Set currentRange = WS2.Cells(2, currentCol)
        If currentRange.Value = "" Then

            Col = Application.WorksheetFunction.Match(currentRange.Offset(-1, 0).Value, WS1.Rows(1), False)
            WS1.Cells(WS1.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(currentRange.Rows.Count).Value = currentRange.Value
        End If
    Next
End Sub

按F8将一次执行一行代码。这是检查函数内部工作的好方法。

答案 1 :(得分:0)

我找到了一个有效的替代品。我添加了这段代码,它基本上删除了堆积数据的行(由于我的初始代码无法忽略空标题而导致)并填充聚合表的最后一行中的空单元格:

.Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(.UsedRange.Rows.Count, 1).EntireRow.Delete
Sheets("Main").Select
LR = Range("A" & Rows.Count).End(xlUp).Row
Set Rg = Range("A" & LR & ":v" & LR)
For Each cl In Rg
If cl = "" Then
cl = "OK"
End If
Next cl
End With