VBA再次查找下一列启动循环

时间:2017-03-22 08:56:41

标签: excel vba excel-vba

我知道这将是一件轻松的事情,但是在经过几天的尝试和谷歌搜索之后我无法做到这一点所以我听起来并不像在这里问白痴。

我正在研究什么应该是一个简单的代码来检查名为numeric的列中的值实际上是数字,计算出现次数并打印cell.address。

在众多工作表中有许多列要搜索,并且代码适用于所有工作表中的第一列我只是无法获取findNext代码,因此它会循环遍历所有数字列,然后再转到下一个工作表。 (当我得到数字整理时,代码包括第二个循环的日期值检查开始)

对于任何帮助,我都会非常感激,所以我不再失去任何头发。提前道歉我对VBA很新。

代码:

Sub ErrorFormatCount()

Dim j As Long
Dim LastRow, LastCol, DateCol, AssetCol, NumericCol As Long
Dim ErrorCount, Counter As Integer
Dim Toolwb As Workbook
Dim ws As Worksheet

    Application.ScreenUpdating = False
    Set Toolwb = Workbooks("EDTDoctor")
    Toolwb.Sheets("Infrastructure").Activate
    For Each ws In Worksheets
        If ActiveSheet.Name = "EndSheetName" Then
            Exit For
        End If
        'On Error Resume Next
        NumericCol = ActiveSheet.Rows(7).Find("Numeric", Lookat:=xlWhole).Column
        DateCol = ActiveSheet.Rows(7).Find("Date", Lookat:=xlWhole).Column
        AssetCol = Rows(4).Find(What:="1035", Lookat:=xlWhole).Column
        LastRow = ActiveSheet.Cells(Rows.Count, AssetCol).End(xlUp).Row
        LastCol = ActiveSheet.Cells(AssetCol, Columns.Count).End(xlToLeft).Column
        For j = 8 To LastRow
            ErrorCount = 0
            Counter = Toolwb.Sheets("Cover").Cells(41, "G").Value
            NextPrintCell = Toolwb.Sheets("Cover").Cells(Rows.Count, "G").End(xlUp).Offset(1, 0).Row

            If Not IsNumeric(Toolwb.ActiveSheet.Cells(j, NumericCol)) Then
                ActiveSheet.Cells(j, NumericCol).Select
                Toolwb.Sheets("Cover").Cells(NextPrintCell, "G") _
                = ActiveCell.address(RowAbsolute:=False, ColumnAbsolute:=False, External:=True)
                ErrorCount = Application.WorksheetFunction.CountA(ActiveCell)
                Toolwb.Sheets("Cover").Cells(41, "G").Value = ErrorCount + Counter
            End If

         Next j

         Toolwb.ActiveSheet.Next.Activate
    Next ws
    Application.ScreenUpdating = True
    Toolwb.Sheets("Cover").Activate
    MsgBox ("Checked For Formatting Errors" & vbNewLine & vbNewLine _
    & "Format  Errors Found" & " - " & Counter), vbInformation = vbOKOnly
End Sub

'使用Excel 2010

1 个答案:

答案 0 :(得分:0)

好的,还有几个问题,我已在评论中注明了这些问题。让我知道你是如何进行的

Sub ErrorFormatCount()

Dim j As Long, k As Long
Dim LastRow As Long, LastCol As Long, DateCol As Long, AssetCol As Long, NumericCol As Long
Dim ErrorCount As Long, Counter As Long
Dim Toolwb As Workbook
Dim ws As Worksheet

Application.ScreenUpdating = False
Set Toolwb = Workbooks("EDTDoctor")

For Each ws In Worksheets
    If ws.Name = "EndSheetName" Then
        Exit For
    End If
    'On Error Resume Next
    NumericCol = ws.Rows(7).Find(What:="Numeric", Lookat:=xlWhole).Column
    DateCol = ws.Rows(7).Find(What:="Date").Column
    AssetCol = ws.Rows(4).Find(What:="1035").Column
    LastRow = ws.Cells(Rows.Count, AssetCol).End(xlUp).Row
    LastCol = ws.Cells(AssetCol, Columns.Count).End(xlToLeft).Column
    For j = 8 To LastRow
        For k = NumericCol To DateCol  'this is the loop for column but not sure if the start or end values are right
            Counter = Toolwb.Sheets("Cover").Cells(41, "G").Value
            NextPrintCell = Toolwb.Sheets("Cover").Cells(Rows.Count, "G").End(xlUp).Offset(1, 0).Row
            If Not IsNumeric(ws.Cells(j, k)) Then
                Toolwb.Sheets("Cover").Cells(NextPrintCell, "G") _
                            = ws.Cells(j, k).Address(RowAbsolute:=False, ColumnAbsolute:=False, External:=True)
                ErrorCount = Application.WorksheetFunction.CountA(ws.Cells(j, k))
                Toolwb.Sheets("Cover").Cells(41, "G").Value = ErrorCount + Counter 'not sure if this should vary for different columns
            End If
        Next k
    Next j
Next ws

Application.ScreenUpdating = True
Toolwb.Sheets("Cover").Activate
MsgBox ("Checked For Formatting Errors" & vbNewLine & vbNewLine _
& "Format  Errors Found" & " - " & Counter), vbInformation = vbOKOnly

End Sub