[英]Excel VBA for hiding cells in one sheet if they match cells in another sheet
[英]Excel VBA Macros: Find/Match cells from one sheet to a column in another
好的,我有一本包含多张纸的工作簿。 工作表的名称为:
输入输出硬件
输入和输出是与实际IP地址匹配的序列ID。
输入1:192.168.0.1输入2:192.168.0.2 ...
硬件有3列。 第一列具有设备,第二列具有输入序列号和第三列输出序列号。
烤面包机:输入1:输出3搅拌机:输入2:输出2 ...等
现在,通常情况下,我将使用Vlookup(A1,Inputs!A:B,2)和Vlookup(A1,Outputs!A:B,2),但是我必须将其合并到我们拥有的VBA宏中不知道如何。
Sub TrackHardware()
'~~~~~~~~~~~~~~~~~~~~~
'Activating Device
'~~~~~~~~~~~~~~~~~~~~~
currentOutputRow = 2
Dim test As String
For currentRow = 2 To 32768 'The last row of your data
'For Loop to go through contents of Hardware individually
If Not (IsEmpty(Worksheets("Hardware").Range("A" & currentRow).Value)) Then
'To Skip the empty cells
HWID=Worksheets("Hardware").Range("a" & currentvalue).Value
'HWID is the search term coming from Sheet:'Hardware'
Desc=Worksheets("Hardware").Range("D" & currentvalue).Value
'Desc is the Plain Text description coming from Sheet:'Hardware'
inputrow={Match pseudocode that didn't work(HWID, "Inputs", Range:= "A:B", 2) }
outputrow={Match pseudocode that didn't work(HWID, "Outputs", Range:= "A:B", 2) }
'trying to find the row # of search term in Sheets 'Input' and 'Output'
Worksheets("Inputs").Range("C" & inputrow).Value = Desc
Worksheets("Outputs").Range("C" & outputrow).Value = Desc
'Pastes The Device Description to Input and Output Sheets
End If
Next currentRow
'And on to the next line in 'Hardware'
End Sub
我也想考虑类似同一输入/输出或空白单元格上的2个设备的错误,但我想我可以自己找出这些错误。 该查找功能确实给我带来了很多麻烦。
首先,如果您无法调用Application.Match
函数,则似乎存在问题。 我不确定为什么会丢失它,但是我知道有些Office / Excel“有限”版本没有完整的VBA功能。 我不确定安装的情况是否如此。
现在,就您的问题...
要使用Application.Match
函数:
Match
功能采用单行或单列范围输入。 您试图传递范围"A:B"
,这将始终引发错误。 改为改为单个行/列范围。
此外,第2
个参数不是2
的选项,它可以是-1(小于),0或False(精确)或1(大于)。 我不确定仅此一项会引发错误,但是无论如何您都应该修复它。
inputRow = Application.Match(HWID, Worksheets("Inputs").Range("A:A"), False)
如果找不到完全匹配的内容,则会引发错误,您可以这样捕获错误:
inputRow = Application.Match(HWID, Worksheets("Inputs").Range("A:A"), False)
If IsError(inputRow) Then
'Do something like:
MsgBox HWID & " not found!", vbInformation
Exit Sub
End If
注意如果您实际上需要检查两列,则可以在Match
函数上加倍使用,也可以使用range .Find
方法。
Dim foundRange as Range
Set foundRange = Range("A:B").Find(HWID)
If Not foundRange Is Nothing Then
inputRow = foundRange.Row
Else
MsgBox HWID & " not found!", vbInformation
End If
使用WorksheetFunction.Match
处理错误
捕获Application.WorksheetFunction.Match
错误应类似于:
inputRow = Empty
On Error Resume Next
inputRow = Application.WorksheetFunction.Match(HWID, Worksheets("Inputs").Range("A:A"), False)
If Err.Number <> 0 Then
MsgBox "Match not found", vbInformation
Exit Sub
End If
On Error GoTo 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.