Excel 如何使用电池满足条件?

Excel 如何使用电池满足条件?,excel,vba,Excel,Vba,我使用userform向工作表添加新内容。为了添加内容,我得到了一个ID号(例如1到4,但它将用于较大的数字),并且我尝试在满足条件时发送数据(条件是,如果我在userform上的组合框中输入的数字与工作表上的数字相同,则userform上写入的所有数据将填充ID号下方的行) 我已经被困在这几个小时了,只是想不出其他的方法来做它 代码 欢迎提供任何帮助,如果需要,我可以提供更多详细信息。也许您可以从这里开始: Dim numero As Long numero = CLng(ComboBox_n

我使用userform向工作表添加新内容。为了添加内容,我得到了一个ID号(例如1到4,但它将用于较大的数字),并且我尝试在满足条件时发送数据(条件是,如果我在userform上的组合框中输入的数字与工作表上的数字相同,则userform上写入的所有数据将填充ID号下方的行)

我已经被困在这几个小时了,只是想不出其他的方法来做它

代码


欢迎提供任何帮助,如果需要,我可以提供更多详细信息。

也许您可以从这里开始:

Dim numero As Long
numero = CLng(ComboBox_numero.Value) 'value of the number ID filled in the combobox

Dim numeroPosition As Variant
numeroPosition = Application.Match(numero, Sheets(2).Range("A5", Sheets(2).Cells(Rows.Count, "A").End(xlUp)), 0) ' use Application.Match to find the index of the searched number inside the the array

If Not IsError(numeroPosition) Then ' if match found
    Sheets(5).Cells(1, numeroPosition - 2).Value = numero 'copy 'numero' in Sheets(5) wanted cell
        'here will be the data sent below the ID number
End If

不清楚您为什么要将值从sheet2复制到sheet5?您可以直接在sheet2上使用Application.Match(),而不需要循环。我不知道是什么应用程序。Match()它是否拾取数据?我对VBA相当陌生,对于可能被视为巨大错误的内容感到抱歉。我正在从Sheet2复制值,因为我制作了一个完全新的工作表,只需要这部分数据(如果这是问题,则需要在其他工作表上)。我不完全理解它。我知道numeroPosition是numero在数组中的位置(我没有在Application.Match的末尾得到0)。我也没有得到numeroPosition-2。我的意思是我得到的是列,但我的列从“B2”开始因此,它可能会得到一个0的值。请参阅
Match
文档。您是否尝试了该代码?它是否达到预期效果?如果没有,它没有达到预期效果?我确实尝试了该代码,正如之前一样,表5中没有写入任何内容,没有显示错误,但是在
表(2)中没有找到与
numero
完全匹配的内容.范围A5:A..
。可能是因为不同的类型吗?第2页上的范围A5:A8(暂时停到8,只是想弄清楚一些事情)是唯一的数字(从1到4)。
Dim numero As Long
numero = CLng(ComboBox_numero.Value) 'value of the number ID filled in the combobox

Dim numeroPosition As Variant
numeroPosition = Application.Match(numero, Sheets(2).Range("A5", Sheets(2).Cells(Rows.Count, "A").End(xlUp)), 0) ' use Application.Match to find the index of the searched number inside the the array

If Not IsError(numeroPosition) Then ' if match found
    Sheets(5).Cells(1, numeroPosition - 2).Value = numero 'copy 'numero' in Sheets(5) wanted cell
        'here will be the data sent below the ID number
End If