简体   繁体   English

Excel VBA宏:在一个工作表中查找/匹配单元格到另一个工作表中的列

[英]Excel VBA Macros: Find/Match cells from one sheet to a column in another

Ok, so I have a workbook with multiple sheets. 好的,我有一本包含多张纸的工作簿。 The Worksheets are named: 工作表的名称为:

Inputs Outputs Hardware 输入输出硬件

Input and output are serial IDs matched to actualy IP Addresses. 输入和输出是与实际IP地址匹配的序列ID。
Input 1 : 192.168.0.1 input 2 : 192.168.0.2 ... etc 输入1:192.168.0.1输入2:192.168.0.2 ...

Hardware has 3 columns. 硬件有3列。 The first has Devices, 2nd column which has the Input Serial IDs and the 3rd of Output Serial IDs. 第一列具有设备,第二列具有输入序列号和第三列输出序列号。

Toaster : Input 1 : Output 3 Blender : Input 2 : Output 2 ...etc 烤面包机:输入1:输出3搅拌机:输入2:输出2 ...等

Now, normally, I'd be using Vlookup(A1,Inputs!A:B,2) and Vlookup(A1,Outputs!A:B,2), but I have to incorporate this into the VBA macro we have and I have no idea how. 现在,通常情况下,我将使用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

I'd also like to account for Errors like 2 devices on the same Input/Output or a Blank cell, but I think I can figure those out myself. 我也想考虑类似同一输入/输出或空白单元格上的2个设备的错误,但我想我可以自己找出这些错误。 This Find function is what's really giving me a lot of trouble. 该查找功能确实给我带来了很多麻烦。

First, there seems to be a problem if you are not able to call on the Application.Match function. 首先,如果您无法调用Application.Match函数,则似乎存在问题。 I am not sure why that would be missing, but I know there are some "limited" versions of Office/Excel which do not have full VBA functionality. 我不确定为什么会丢失它,但是我知道有些Office / Excel“有限”版本没有完整的VBA功能。 I am not sure if that is the case with your installation. 我不确定安装的情况是否如此。

Now, on to your problem though... 现在,就您的问题...

To use the Application.Match function: 要使用Application.Match函数:

The Match function takes a single row or single column range input. Match功能采用单行或单列范围输入。 You are attempting to pass in the range "A:B" , which will always raise an error. 您试图传递范围"A:B" ,这将始终引发错误。 Change to a single row/column range instead. 改为改为单个行/列范围。

Further, 2 is not an option for the third argument, which can be either -1 (less than), 0 or False (exact), or 1 (greater than). 此外,第2个参数不是2的选项,它可以是-1(小于),0或False(精确)或1(大于)。 I'm not sure this alone will raise an error, but you should fix it anyways. 我不确定仅此一项会引发错误,但是无论如何您都应该修复它。

inputRow = Application.Match(HWID, Worksheets("Inputs").Range("A:A"), False)

If an exact match cannot be found, it will raise an error, which you can trap like so: 如果找不到完全匹配的内容,则会引发错误,您可以这样捕获错误:

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

NOTE If you actually need to check both columns, then you can either double up on the Match function, or use the range .Find method instead. 注意如果您实际上需要检查列,则可以在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

Handling errors with WorksheetFunction.Match 使用WorksheetFunction.Match处理错误

Error trapping for Application.WorksheetFunction.Match should be something like: 捕获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.

相关问题 Excel VBA用于隐藏一个工作表中的单元格(如果它们匹配另一个工作表中的单元格) - Excel VBA for hiding cells in one sheet if they match cells in another sheet Excel-将单元格+宏+ VBA从一张纸复制到另一张纸? - Excel - copy cells+macro+VBA from one sheet to another? 如何使用 VBA 宏从一张工作表中复制“特定”行并粘贴到另一个工作表中 - How to copy "specific" rows from one sheet and paste in to another in an excel using VBA Macros Excel VBA-IF / AND问题-如果两列中的值在单独的工作表上匹配,则将一个工作表中的值复制到另一列中的值 - Excel VBA - IF/AND Issue - If values from two columns match on separate sheets, copy a value from one sheet to another in another column 如果列中的行范围与另一工作表中的列的范围相匹配,则 VBA 会清除行中单元格的内容 - VBA clear contents from cells in row if range of rows in column match range from column in another sheet VBA代码 - 根据匹配条件将单元格中的单元格复制到Excel中的另一个工作表 - VBA Code - Copying cells from one sheet to another sheet in excel depending on matching condition Macro Excel可根据单元格匹配将范围单元格从一张纸复制到另一张纸,如果不匹配,则跳过单元格 - Macro Excel to copy range cells from one sheet to another based on cell match and skip cell if no match Excel VBA - 根据 3 个条件(复杂的 IF AND 相关 VBA 与通配符)使用另一张工作表中的值填充一张工作表上的列 - Excel VBA - Populate a column on one sheet with values from another sheet based on 3 criteria (complicated IF AND related VBA with wildcards) 将一个Excel工作表中的两列与另一工作表中的两列进行比较,如果匹配,则从另一列中复制数据 - Comparing two columns in one Excel sheet, to two columns in another sheet, and if they match, copy data from another column VBA从一个Excel工作表复制到另一个Excel工作表? - VBA copying from one excel sheet to another excel sheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM