Dim acs, cos, as_col, as_row As Integer
cos = Sheets.Count
acs = ActiveSheet.Index
as_LRow = Sheets(acs).Cells(Sheets(acs).Rows.Count, "A").End(xlUp).Row
For as_row = 3 To as_LRow
std_number = Sheets(acs).Cells(as_row, 1).Value
earliersheets = acs - 1
For s = 1 To earliersheets
ilast_row = Sheets(s).Cells(Sheets(s).Rows.Count, "A").End(xlUp).Row
For r = 3 To ilast_row
std_number_new = Sheets(s).Cells(r, 1).Value
If std_number = std_number_new Then
a = a & Sheets(s).Cells(r, 6).Value
'Sheets(acs).Cells(as_row, 8).Value = Sheets(s).Cells(r, 8).Value
Sheets(acs).Cells(as_row, 8).Value = Sheets(acs).Cells(as_row, 6).Value & " + " & a
End If
a = ""
Next r
Next s
Next as_row
我想要的是: 手动添加我要按下按钮的新工作表后。此按钮激活上面的代码。我想要的是检查“A”列中特定单元格中的值。如果此列中的值与上一个工作表中的值匹配,则显示新添加的工作表中第六列中的值。上面的代码可以做到这一点,但它只适用于两张纸。如果我制作了两张以上的纸张,那么它就不会显示更多的值。
更新
我在链接中添加了我的文件:Check.xlsm
打开此文件后,清除第三个(可能是第二个工作表)中的H列,然后在其中运行宏。你会明白我的意思
我想要的是让前一张纸中的所有先前值在列-H中移位。例如,我用值标记了两个单元格,这些值在运行宏后显示在H列中。
答案 0 :(得分:1)
您的问题是,您始终在H列中存储通过从当前工作表的F列获取值并在循环内附加正在处理的工作表的F列创建的值。当您移动到循环中的下一个工作表时,您将使用新值替换先前的值。
因此,当处理表w3-6时,首先查看表w1-4并生成值“13 + 34”并将其存储在表w3-6的单元格H3中。然后你看一下表w2-5并生成一个“13 + 18”的值,并将当前表格w3-6的单元格H3中的“13 + 34”的值替换为“13 + 18”。
请尝试使用此代码:
Dim acs As Long, cos As Long, as_col As Long, as_row As Long
Dim s As Long
Dim as_LRow As Long
Dim ilast_row As Long
Dim r As Long
Dim a As String
cos = Sheets.Count
acs = ActiveSheet.Index
as_LRow = Sheets(acs).Cells(Sheets(acs).Rows.Count, "A").End(xlUp).Row
For as_row = 3 To as_LRow
std_number = Sheets(acs).Cells(as_row, 1).Value
'Initialise variable containing result to go into column H
a = Sheets(acs).Cells(as_row, 6).Value
'Process earlier sheets in reverse order
'(so that values will be shown in reverse order)
For s = acs - 1 To 1 Step -1
ilast_row = Sheets(s).Cells(Sheets(s).Rows.Count, "A").End(xlUp).Row
For r = 3 To ilast_row
std_number_new = Sheets(s).Cells(r, 1).Value
If std_number = std_number_new Then
'Append value to result string
a = a & " + " & Cstr(Sheets(s).Cells(r, 6).Value)
Exit For
End If
Next r
Next s
'Store result
Sheets(acs).Cells(as_row, 8).Value = a
Next as_row