2017-03-22 83 views
0

我知道這將是一個容易的,但我只是無法在嘗試和谷歌搜索的日子後得到它,所以我聽起來不像一個白癡在這裏問。VBA查找下一個列開始循環再次

我正在研究什麼應該是一個簡單的代碼來檢查名爲數字的列中的值實際上是數字,計數發生並打印cell.address。

許多工作表中有許多列用於搜索,並且所有工作表中的第一列的代碼都能正常工作我只是無法獲得findNext代碼,因此它在所有數字列中循環移動到下一個工作表之前。 (代碼包括日期值開始檢查第二個循環,當我得到數字排序出來)

我會sooooo偉大的任何assitance,所以我不會失去更多的頭髮。事先道歉我對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

+0

想想我們需要更多的細節來看結果應該去哪裏。 – SJR

+0

不用擔心。代碼遍歷選定列中的所有行(大多數工作表中除了第一個表「Cover」和最後一個「EndSheetName」外,每個ws中的列都帶有行(7)中的標題「numeric」。if if not not numeric counts the returns returns覆蓋!G41的累計結果並將從Cover!G43開始的單元格地址複製到下一行,以查找發現的每個數字錯誤。不幸的是,我無法更改工作表工作簿結構以使事情更輕鬆,因爲此輸出由客戶端數據設置如果您需要更多的信息或者需要上傳樣本,請告知我們。 – AmetureHour

回答

0

OK,還是有一些問題,我已經標誌着意見。讓我知道你是如何得到的

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 
+0

@Mark Fitzgerald非常感謝您的幫助,我做了一些調整,但是很有魅力! – AmetureHour

+0

不確定Mark Fitzgerald是誰,但很高興。也許你可以把它標記爲你的問題的答案? – SJR

+0

大聲笑。對不起,錯誤的用戶。謝謝SJR。 – AmetureHour