VBA excel - if-else在循环中不起作用,因为我需要

问题描述 投票:-4回答:1

我正在设置一个包含大约15个字段的简单表单,最后将这些字段复制并保存在我的数据库中(相同的excel文件但是单独的电子表格)。然后我需要设计一个程序,使我能够修改现有的记录。我创建了一个将数据库中的所有数据调回到表单中,以便可以修改然后保存。它工作正常但我真的不知道何时将ELSE用于表格中不存在搜索记录的情况。

Public Sub amend()
Dim spec_number As Long
Dim licznik As Long
Dim x As Long
Dim specyfikacje As Worksheet
Dim formularz As Worksheet

Set specyfikacje = Sheets("specifications")
Set formularz = Sheets("form")

spec_number = formularz.Range("b4").Value

If spec_number = "" Then
    MsgBox "Type the specification number in the designated field: B4"
Else
    licznik = 2
    x = 2

    Do Until (specyfikacje.Range("A" & licznik).Value = "") = True
        licznik = licznik + 1
    Loop

    Do Until x > licznik
        If spec_number = specyfikacje.Range("a" & x).Value Then
            formularz.Range("b6") = specyfikacje.Range("b" & x).Value
            formularz.Range("b7") = specyfikacje.Range("c" & x).Value
            formularz.Range("b8") = specyfikacje.Range("d" & x).Value
            formularz.Range("b9") = specyfikacje.Range("e" & x).Value
            formularz.Range("b10") = specyfikacje.Range("f" & x).Value
            formularz.Range("b11") = specyfikacje.Range("g" & x).Value
            formularz.Range("b12") = specyfikacje.Range("h" & x).Value
            formularz.Range("b13") = specyfikacje.Range("i" & x).Value
            formularz.Range("b14") = specyfikacje.Range("j" & x).Value
            formularz.Range("b15") = specyfikacje.Range("k" & x).Value
            formularz.Range("b16") = specyfikacje.Range("l" & x).Value
            formularz.Range("b17") = specyfikacje.Range("m" & x).Value
            formularz.Range("b18") = specyfikacje.Range("n" & x).Value
            formularz.Range("b19") = specyfikacje.Range("o" & x).Value
            formularz.Range("b20") = specyfikacje.Range("p" & x).Value
            formularz.Range("b21") = specyfikacje.Range("q" & x).Value
            formularz.Range("b22") = specyfikacje.Range("r" & x).Value
            formularz.Range("b23") = specyfikacje.Range("s" & x).Value
        Else
            MsgBox "The product you typed in doesn't exist"
        End If
    x = x + 1
    Loop
End If
End Sub
excel excel-vba vba
1个回答
0
投票

我想如果你用以下代码替换你的代码,它应该按预期工作:

Dim licznik As Long
Dim x As Long
Dim specyfikacje As Worksheet
Dim formularz As Worksheet

Set specyfikacje = Sheets("specifications")
Set formularz = Sheets("form")

spec_number = formularz.Range("b4").Value

If spec_number = "" Then
    MsgBox "Type the specification number in the designated field: B4"
Else

    licznik = specyfikacje.Cells(specyfikacje.Rows.Count, "A").End(xlUp).Row 'find the last row on Column A on Sheet specifications

    With specyfikacje.Range("A:A")
        Set Rng = .Find(What:=spec_number) 'search column A of specifications for the spec_number
            If Not Rng Is Nothing Then 'if found
                For x = 1 To 18
                    formularz.Range("b" & i + 5) = Rng.Offset(0, i).Value
                Next x
            Else
                MsgBox "The product you typed in doesn't exist"
            End If
    End With
End If
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.