代码检查/帮助

时间:2018-05-15 15:16:10

标签: excel vba excel-vba

有人可以查看我的代码,然后告诉我错误在哪里,因为我收到类型不匹配错误消息?使用此代码,我想删除相应单元格中包含“0”的所有行。

我收到了代表行的错误消息:sn = Application.Index(sn, Application.Transpose(Split(Mid(c00, 2), "|")), [transpose(row(1:8))]) 此外,我必须声明变量“c00”,我选择“c00 As Variant”。我不知道它是否正确。我很感激有人帮助我解决问题。

Dim sn As Variant, c00 As Variant

   sn = Sheets(1).UsedRange

   For j = 1 To UBound(sn)
     If sn(j, 4) & sn(j, 5) & sn(j, 6) & sn(j, 7) & sn(j, 8) & sn(j, 9) = "000000" Then c00 = c00 & "|" & j
   Next

     If c00 <> "" Then
        sn = Application.Index(sn, Application.Transpose(Split(Mid(c00, 2), "|")), [transpose(row(1:8))])
            Sheets(1).UsedRange.ClearContents
            Sheets(1).Cells(1).Resize(UBound(sn), UBound(sn, 2)) = sn
     End If

原始代码

Dim LR%


LR = Cells(Rows.Count, 3).End(xlUp).Row 

Set Myrange = Range("D2:AO" & LR).SpecialCells(xlCellTypeBlanks) 'nur Leerzellen
Myrange.Formula = "0"


ActiveSheet.UsedRange
Set r = ActiveSheet.UsedRange

lastrow3 = r.Rows.Count + r.Row - 1

    For j = lastrow3 To 1 Step -1
        If (Cells(j, 4) = 0 And Cells(j, 5) = 0 And Cells(j, 6) = 0 And Cells(j, 7) = 0 And Cells(j, 8) = 0 And Cells(j, 9) = 0) Then
            Rows(j).Delete
        End If
    Next j

图片错误

enter image description here

1 个答案:

答案 0 :(得分:0)

编辑:错误来自于在大于函数大小限制的数组上尝试使用Application.Index。重定向至here以获取Q&amp; A和Application.Index的替代选项。

我将细分我对您的代码的分析:

  

Application.Index(Array,Row_Number,Column_Number)

您目前拥有的代码:

sn = Application.Index(sn, Application.Transpose(Split(Mid(c00, 2), "|")), [transpose(row(1:8))])

表示参数是:

  • 数组:sn
  • Row_Number:Application.Transpose(Split(Mid(c00, 2), "|"))
  • Column_Number:[transpose(row(1:8))]

Array部分对我来说很好看。我认为(?)的行号将是您在j中收集的c00的值(尽管Application.Transpose可能没有必要更正:它在此场景。)。我不知道你的Column_Number参数是怎么回事......

<强>的问题:

Application.Index 保留所选的列/行。但是,if语句会选择行j所在的0的值,因此不会丢失它们,而是保留 那些行。

如果您打算保留所有列,只需将0输入Column_Number参数即可。 更正:仅在选择要保留的单行时才有效。如果选择多行,则还必须列出所有列。

更正后的代码:

由于此代码确实删除了数据,因此您应该在运行此代码之前保存数据的副本。

注意:c00可以是变体;字符串也有效。您还需要复制fillA功能。

Dim sn As Variant, c00 As String

   sn = Sheets(1).UsedRange

   ' Changed condition based on your post with previous code. (And negated)
   For j = 1 To UBound(sn)
     If Not ((Cells(j, 4) = 0 And Cells(j, 5) = 0 And Cells(j, 6) = 0 And Cells(j, 7) = 0 And Cells(j, 8) = 0 And Cells(j, 9) = 0)) Then c00 = c00 & "|" & j
   Next

     If c00 <> "" Then
        ' Corrected inputs for Application.Index, Added helper function "fillA".
        sn = Application.Index(sn, Application.Transpose(Split(Mid(c00, 2), "|")), fillA(1, UBound(sn, 2) - LBound(sn, 2) + 1))
            Sheets(1).UsedRange.ClearContents
            Sheets(1).Cells(1).Resize(UBound(sn), UBound(sn, 2)) = sn
     End If
Function fillA(min As Long, max As Long) As Variant
    Dim var() As Variant, i As Long
    ReDim var(1 To max - min + 1)

    For i = min To max
        var(i) = i + min - 1
    Next i
    fillA = var
End Function

修改

意识到这并未解决您的问题。我怀疑该错误来自您为[transpose(row(1:8))]参数插入的Column_Number。 也许其他人有一种更简单的方式来做我对fillA函数所做的事情(我相信你在尝试)。