Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA搜索行已更改,代码需要更新_Excel_Vba - Fatal编程技术网

Excel VBA搜索行已更改,代码需要更新

Excel VBA搜索行已更改,代码需要更新,excel,vba,Excel,Vba,下面是一段代码,我现在使用它自动将数字插入到列a上有今天日期且该列第一行有正确名称的单元格中 但是,如果名称位于除1以外的任何其他行中,我似乎无法使其工作 如果我想让它搜索第2行或多行上的匹配项,我需要做哪些更改 Sub SyöttöEriVälilehti() Application.ScreenUpdating = False On Error GoTo M Dim i As Long Dim Lastrow As Long Dim col As Long col = 0 Dim Last

下面是一段代码,我现在使用它自动将数字插入到列a上有今天日期且该列第一行有正确名称的单元格中

但是,如果名称位于除1以外的任何其他行中,我似乎无法使其工作

如果我想让它搜索第2行或多行上的匹配项,我需要做哪些更改

Sub SyöttöEriVälilehti()

Application.ScreenUpdating = False
On Error GoTo M
Dim i As Long
Dim Lastrow As Long
Dim col As Long
col = 0
Dim LastColumn As Long
Dim DateLastrow As Long
Dim ans As String
Dim LString As String
Dim LArray() As String
Dim anss As String
Dim ansss As String

With Sheets("Malli2Data") ' Sheet name
    DateLastrow = .Cells(Rows.Count, "A").End(xlUp).Row
    Set SearchRange = .Range("A1:A" & DateLastrow).Find(Date)
    If SearchRange Is Nothing Then MsgBox Date & "  No matches", , "Oops!": Exit Sub
    Lastrow = SearchRange.Row
    LastColumn = .Cells(1, Columns.Count).End(xlToLeft).Column
    ans = InputBox("Input name and number like so:  Tom,5")
    LString = ans
    LArray = Split(LString, ",")
    anss = LArray(0)
    ansss = LArray(1)

    For i = 2 To LastColumn
        If .Cells(1, i).Value = anss Then col = Cells(1, i).Column
    Next

    If col = 0 Then MsgBox anss & " No matches": Exit Sub

    .Cells(Lastrow, col).Value = ansss
End With
Application.ScreenUpdating = True
Exit Sub
M:
MsgBox "Error" & vbNewLine _
& "Check input" & _
vbNewLine & "You typedt: " & ans & vbNewLine & "Correct input type: " & vbNewLine & "Name" & ",Number" & _
vbNewLine & vbNewLine & "Try again"

End Sub
片段:

For i = 2 To LastColumn
    If .Cells(1, i).Value = CDec(anss) Then col = Cells(1, i).Column
Next
正在第1行搜索您的姓名

如果你想改变它,让它成为一个变量,比如

For i = 2 To LastColumn
    If .Cells(xRow, i).Value = CDec(anss) Then col = Cells(1, i).Column 
Next
xRow是您定义的要搜索的行

同时,您可以细分出循环中的最后一位并使用

For i = 2 To LastColumn
    If .Cells(xRow, i).Value = CDec(anss) Then col = i
Next
因为它们是一样的东西


编辑20201-04-23A:使用CDec(anss)将字符串(从“ans”中收集)转换为十进制数-然后可以将其与从单元格中取出的.Value进行比较。

您好,谢谢!我是VBA新手,仍然有一些问题,无法找到输入搜索行的正确方法。尝试了2和2:2,但仍然出现错误。您是否尝试了:xRow=2Hi,我尝试了,但它返回“无匹配项”,这就是我现在使用的。xRow=2表示i=2到最后一列If.Cells(xRow,i).Value=anss然后col=i'xRow märittelee hakurivin NextOK意识到我们现在错过了什么。将字符串(anss)与值进行比较。将编辑原始答案以进行更正。