在matrix中find0的问题

我正在看excel中的电子表格

Name | Paperwork | Paperwork 1 | Paperwork 2 Joe | 1 | 1 | 1 Jane | 0 | 1 | 0 

我试图在电子表格中find0,并输出类似于在文书工作2中分配给Jane的文书工作中的错误

我有的VBA代码是:

 Private Sub CommandButton1_Click() Dim i As Integer, j As Integer, Staff As String, Consumer As String, Error As String, CurCell As String MsgBox "Starting the routine..." For i = 2 To 3 If Cells(i, 2).Value = 0 Then For j = 3 To 4 If Cells(i, j).Value = 1 Then CurCell = i & ", " & j Else CurCell = i & ", " & j MsgBox CurCell End If Next j End If Next i End Sub 

我试图扫描文书工作; 它是一个说明文书工作是否完成的专栏。 因为Joe完成了他的文书工作,algorithm经过了它。 简却错过了文书工作2.所以,当algorithm到达位置(简,文书工作),它开始在行(文书工作)

对于(Jane,文书工作1),algorithm看到1,并且移动以递增For(Jane,Paperwork 2)algorithm看到0,并且我想要做的是显示:“Jane is missing”+文书工作2。

我想在这一点上做一些事情,比如将Staff stringvariables设置为= Cell(row i,j).value,然后将'Staff'输出到电子表格中的某处,但是我不知道VBA语法能够这样做。

 Sub ZeroError() Dim rng As Range Dim rowREF As Integer 'row reference Dim colREF As Integer ' column reference Dim eName As String 'name holder for employee Dim wAssignment As String 'assignment holder eg Paperwork Dim colLOCATION As Integer ' this is the column you want to put your results in colLOCATION = 1 ' placing everying in column note that i add 6 in CELLS rowREF = 1 colREF = 1 eName = "" wAssignment = "" Set rng = ActiveSheet.UsedRange For Each cell In rng If cell.Value = 0 Then rowREF = cell.Row colREF = cell.Column eName = Cells(rowREF, 1) wAssignment = Cells(1, colREF) If (eName <> "" And wAssignment <> "") Then If Cells(rowREF, colLOCATION + 6) <> "" Then colLOCATION = colLOCATION + 1 Else colLOCATION = 1 End If Cells(rowREF, colLOCATION + 6) = eName & " " & "is missing" & " " & wAssignment End If End If If cell.Value <> 0 Then rowREF = cell.Row colREF = cell.Column eName = Cells(rowREF, 1) wAssignment = Cells(1, colREF) If (eName <> "" And wAssignment <> "") Then If Cells(rowREF, colLOCATION + 6) <> "" Then colLOCATION = colLOCATION + 1 Else colLOCATION = 1 End If Cells(rowREF, colLOCATION + 6) = eName & " " & "has completed" & " " & wAssignment End If End If Debug.Print colLOCATION Next End Sub 

在下面的答案调整到您的设置 – 抱歉,我不是最有效的编码器,但它应该为你工作。

使用.CurrentRegion作为起点,你应该能够抵消和循环每个编号的单元格。 我已经把结果放在右边一个未使用的列中。 这是我可以从你的叙述中find的最好的。

 Sub lost_Paperwork() Dim iStaffCol As Long, rng As Range With ActiveSheet 'define this worksheet peoperly! With .Cells(1, 1).CurrentRegion iStaffCol = .Columns.Count + 2 For Each rng In .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1) If rng.Value = 0 Then _ .Cells(Rows.Count, iStaffCol).End(xlUp).Offset(1, 0) = _ .Cells(rng.Row, 1).Value & ", missing " & .Cells(1, rng.Column).Value Next rng End With .Cells(1, iStaffCol) = "Staff" End With End Sub 

你的结果应该类似于以下内容。

在这里输入图像说明