另一个工作簿中存在A列的VBA检查值
我正在尝试构建一个宏,该宏循环遍历colA中的一系列值,并检查它们是否与其他工作簿一起存在。在其中的一个例子中,我想将其标记为“已工作”/“未工作” 有关于从哪里开始的指导吗?示例 这是一个你正在寻找的例子。请记住,两个工作簿必须在同一个Excel实例中打开另一个工作簿中存在A列的VBA检查值,vba,excel,match,nested-loops,Vba,Excel,Match,Nested Loops,我正在尝试构建一个宏,该宏循环遍历colA中的一系列值,并检查它们是否与其他工作簿一起存在。在其中的一个例子中,我想将其标记为“已工作”/“未工作” 有关于从哪里开始的指导吗?示例 这是一个你正在寻找的例子。请记住,两个工作簿必须在同一个Excel实例中打开 Sub check() Dim i As Integer, k As Integer, j As Integer 'Define your counting variables Dim Report1 As Worksheet, bRe
Sub check()
Dim i As Integer, k As Integer, j As Integer 'Define your counting variables
Dim Report1 As Worksheet, bReport As Workbook, Report2 As Worksheet, bReport2 As Workbook 'Define your workbook/worksheet variables
Set Report1 = Excel.ActiveSheet 'Assign active worksheet to report1
Set bReport = Report1.Parent 'Assign the workbook of report 1 to breport
On Error GoTo wbNotOpen 'If an error occurs while accessing the named workbook, send to the "wbNotOpen" line.
Set bReport2 = Excel.Workbooks("otherworkbookname.xlsm") 'Assign the other workbook which you are cross-referencing to the bReport2 variable.
Set Report2 = bReport2.Worksheets("otherworksheetname") 'Do the same with the worksheet.
On Error GoTo 0 'Reset the error handler (to undo the wbNotOpen line.)
k = Report1.UsedRange.Rows.Count 'Get the last used row of the first worksheet.
j = Report2.UsedRange.Rows.Count 'Get the last used row of the second worksheet.
For i = 2 To k 'Loop through the used rows of the first worksheet. I started at "2" to omit the header.
'Next, I used the worksheet function "countIf" to quickly check if the value exists in the given range. This way we don't have to loop through the second worksheet each time.
If Application.WorksheetFunction.CountIf(Report2.Range(Report2.Cells(2, 1), Report2.Cells(j, 1)), Report1.Cells(i, 1).Value) > 0 Then
Report1.Cells(i, 5).Value = "Worked" 'If the value was found, enter "Worked" into column 5.
Else
Report1.Cells(i, 5).Value = "Not worked" 'If the value wasn't found, enter "Not worked" into column 5.
End If
Next i
Exit Sub
'This is triggered in the event of an error while access the "other workbook".
wbNotOpen:
MsgBox ("Workbook not open. Please open all workbooks then try again.")
Exit Sub
End Sub
此链接还包括告诉如何检查另一工作簿中是否存在单元格的步骤。这些评论很有用
{
Sub CheckValue()
Dim S1 As Worksheet
Dim S2 As Worksheet
Dim i As Integer
Dim k As Integer
Dim j As Integer
Set S1 = Worksheets("Sheet1")
Set S2 = Worksheets("Sheet2")
k = S1.UsedRange.Rows.Count
j = S2.UsedRange.Rows.Count
For i = 1 To k
If Application.WorksheetFunction.CountIf(S2.Range(S2.Cells(2, 1), S2.Cells(j, 1)), S1.Cells(i, 1).Value) > 0 Then
S1.Cells(i, 5).Value = "Worked" 'If the value was found, enter "Worked" into column 5.
Else
S1.Cells(i, 5).Value = "Not worked" 'If the value wasn't found, enter "Not worked" into column 5.
End If
Next i
End Sub
}您可以通过打开一个工作簿(名为book1)并将其值存储在字典中来完成此操作。然后从另一个工作簿(名称book2)逐个访问值,并检查字典中是否存在该单词。如果找到匹配项,则在第2册中标记该单词。希望它能帮助你。对于更多的查询,请让我知道。下面的代码我试图理解{If Application.WorksheetFunction.CountIf(Report2.Range(Report2.Cells(2,1),Report2.Cells(j,1)),Report1.Cells(I,1.Value)>0 Then}如果您能解释引用是如何工作的,我不太明白。我问上述问题是因为我已经做了必要的更改,并尝试使用两个文档中存在的一些测试数据运行宏,但是它显示的所有内容都是“未工作”@user1810449该行基本上是excel中CountIf函数的VBA版本。它的工作原理是一样的;它使用两个参数:一个用于范围,另一个用于搜索值。唯一的区别是您必须使用VBA
mySheet.range(cellone,celltow2)
指定范围。在上面的代码中,单元格一是Report2.单元格(2,1)
,单元格二是Report2.单元格(j,1)
。搜索值为Report1.Cells(i,1).value
。您希望.Value
在那里,因为我们引用的是单元格的内容,而不是单元格本身。我想知道是否有一个快速解决方法,可以标记空白单元格。每当我运行宏时,它会根据需要对其进行标记,但它也会标记空白单元格。如果在第一张工作表的使用范围内有空白单元格,countif函数将使用空字符串值作为搜索值。如果您想快速省略这些单元格,只需将If函数包装在另一个If中即可。如如果S1.Cells(i,1).Value“”,则。这基本上是说只有在搜索值不是空的情况下才执行以下操作。这是否意味着我必须编写一个嵌套的if语句?或者在.Nested下面添加另一个if-then行。把我写的行放在上面,把End If
行放在下面。我已经在代码中添加了If语句,无论它对所有单元格显示“Not Worked”{对于I=2到k,如果S1.cells(I,1).Value“”,那么If Application.WorksheetFunction.CountIf(S2.Range(S2.cells(2,1),S2.cells(j,1)),S1.cells(I,2).Value)>0然后S1.Cells(i,5).Value=“Worked”S1.Cells(i,5).Interior.ColorIndex=43否则S1.Cells(i,5).Value=“Not Worked”S1.Cells(i,5).Interior.ColorIndex=36'如果找不到值,则在第5列中输入“Not Worked”。如果下一个i}则结束