我正在查看excel中的电子表格
Name | Paperwork | Paperwork 1 | Paperwork 2
Joe | 1 | 1 | 1
Jane | 0 | 1 | 0
我试图在电子表格中找到0,并输出类似的内容 分配给Jane for Paperwork 2的文书工作中存在错误
我的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
我试图扫描文书工作;它是一个专栏,说明文书工作是否完成。因为乔完成了他的文书工作,所以算法会超越它。然而Jane缺少文书工作2.因此,当算法到达位置(Jane,Paperwork)时,它开始排成行(文书工作)
对于(Jane,Paperwork 1),算法看到1,然后移动到递增 对于(Jane,Paperwork 2),算法看到0,我想要做的是显示: "简失踪" +文书工作2。
我想在这一点上做点什么,比如将Staff字符串变量设置为= Cell(第i行,第j行).value,然后输出' Staff'到电子表格的某个地方,但我不知道VBA语法能够这样做。
答案 0 :(得分:1)
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 e.g. 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
根据以下答案调整您的设置 - 抱歉,我不是最有效的编码器,但它应该适合您。
答案 1 :(得分:0)
使用.CurrentRegion
作为起点,您应该能够偏移并遍历每个编号的单元格。我已将结果放入右侧未使用的列中。这是我能从你的叙述中找到的最好的。
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
您的结果应该类似于以下内容。