VBA如何优化许多列的宏

时间:2018-06-25 16:03:12

标签: vba

我想写一个宏来有条件地对列中的行进行计数,我大约有30列,我想为所有可能的列都提供一个宏。我为一栏写了一个宏,但不知道如何优化它们。 我的第一列(110_1.1)宏如下。而且我需要为接下来的列“ 120_1.2”,“ 130_1.3”,...等等写一个宏,直到“ 810_8.1”,因此是Sheets(“ Element_errors”)。Cells(i,j)每列应采用值“ err120”,“ err130”,..“ err810”和j = 1..30。 问题还在于,所有列都不一一对应。

 Sub Element110_error()
    Dim zelle As Range
    Dim i As Integer
    Dim posMonitoring As Integer
    Dim j As Integer
    Dim intLastRow As Integer

    With Sheets("ICS Table")
     intLastRow = .UsedRange.Rows.Count
    Set zelle = .Cells.Find("110_1.1", lookat:=xlPart)

    If zelle Is Nothing Then
        For i = 2 To intLastRow
            Sheets("Element_errors").Cells(i, 1).Value = "no data"
        Next i
    Else
        posMonitoring = zelle.Column

        For i = 2 To intLastRow
            If .Cells(i, posMonitoring).Value < 1 Or .Cells(i, posMonitoring).Value > 10 Then
                Sheets("Element_errors").Cells(i, 1) = "err110"
            Else
                Sheets("Element_errors").Cells(i, 1) = "no"
            End If
        Next i


      End If
    End With
End Sub

1 个答案:

答案 0 :(得分:1)

这将迭代每个模式并在每个列的输出:

B <- function(frame1, frame2, column){
  bframe$freq.x[is.na(bframe$freq.x)] <- 0
  bframe <- merge(frame 1, frame2, by = column, all = TRUE)
  bframe$result <- bframe$freq.x - bframe$freq.y
  bframe$percentage <- (bframe$result/bframe$freq.y)*100
  return(bframe)
}

B(DT2_1, 2_1, "BurgS")