需要回忆最近扫描的VBA坐标

时间:2016-08-22 22:30:30

标签: vba excel-vba if-statement excel

我就在这里。

我已经完成了一切,不包括一部分。我需要能够回忆起(b,c)的最后一个坐标,并且只有在扫描某个条形码时才从中减去一个。它还将获取单元格j并将其添加到(t,n)。

我要感谢Tim Williams帮助我学习并做到这一点! 以下是我正在处理的代码。有500多个案例,但为了简单起见,我附上了两种案例。我只需要从Case“2 in x 16 ft R -1”调用的最后一个坐标。

如果有人能帮助我,我会非常感激。

`Private Sub TextBox1_Change()

Dim ws As Worksheet, v, n, t, b, c, e, f, g, h
Set ws = Worksheets("Sheet1")

v = TextBox1.Value
n = 0
t = 0
b = 0
c = 0
e = 0
f = 0
g = 0
h = 0


Select Case v

Case "2 in x 16 ft R -1": n = 9
t = 1
b = 6
c = 1
e = 11
f = 6
g = "W2 in x 16 ft R -1"
h = 40
j = 0.296


Case "15 - FT - R": f = 5
e = 11
n = -1

End Select


If n > 0 Then
    ws.Cells(t, n) = g

    'puts name in cell for throw away value if needed later,
    'has to be a way for a scanned item to recall last scanned item
    'and subtract 1 from grid coordinates (b, c).
    'only subtracts one if a certain barcode is scanned
    'For example "CWaste-1", or "CWaste-2" would select last (b,c) and   subtract 1
    'then it would add a value based on which (b,c) it selected and based on it is was -1 or -2
' So if Case "2 in x 16 ft R -1" was selected and then Case "CWaste-1", I  would want the
'VBA to subtract 1 from ["2 in x 16 ft R -1" (b,c)] and then add j to cell (t,n)
'Basically, I would like to recall last coordinates and then add j to (t,n)

ws.Cells(1, 1) = j

ws.Cells(b, c) = ws.Cells(b, c) + h
' adding different number based on case

ws.Cells(f, e) = ws.Cells(f, e) - 1
' always subtracts 1 from certain range based on case

TextBox1.Activate
TextBox1.Value = ""

ElseIf n < 0 Then
ws.Cells(f, e) = ws.Cells(f, e) + 1
' always adds 1 from certain range based on case

TextBox1.Activate
TextBox1.Value = ""

End If
End Sub`

2 个答案:

答案 0 :(得分:2)

第一个:

Private Sub TextBox1_Change()

    Dim ws As Worksheet, v, n
    Set ws = Worksheets("Sheet1")

    v = TextBox1.Value
    n = 0

    Select Case v
        Case "15 - FT - R": n = 5
        Case "16 - FT - R": n = 6
        'other cases here....
    End Select

    If n > 0 Then
        ws.Cells(n, 11) = ws.Cells(n, 11) + 1
        TextBox1.Activate
        TextBox1.Value = ""
    End If

End Sub

第二个可以遵循类似的模式。

如果您有很多这样的类似测试,可能更容易将所有值和相应的行号等放在工作表的表中,然后使用Match()测试输入的值对表和读取返回参数值。

另外:当您可以使用If来处理

时,您正在嵌套ElseIf

编辑:对于您的第二个,这是使用工作表来保存各种参数的示例

“配置”表:

enter image description here

代码:

Private Sub TextBox1_Change()

    Dim ws As Worksheet, f As Range, rw As Range

    'is there a match for the textbox value on the CXonfig sheet?
    Set f = Worksheets("Config").Columns(1).Find(Trim(TextBox1.Value), _
                                       LookIn:=xlValues, lookat:=xlWhole)

    If Not f Is Nothing Then
        'Got a match...
        Set ws = Worksheets("Sheet1")
        Set rw = f.EntireRow
        'use the values from the Config sheet
        ws.Cells(rw.Cells(3).Value, 9) = rw.Cells(2).Value
        ws.Cells(rw.Cells(4).Value, 1) = ws.Cells(rw.Cells(4).Value, 1) + 13
        ws.Cells(rw.Cells(5).Value, 11) = ws.Cells(rw.Cells(5).Value, 11) - 1
        Application.EnableEvents = False
        TextBox1.Activate
        TextBox1.Value = ""
    End If

End Sub

答案 1 :(得分:0)

我很确定我已经想出如何做到这一切!我只是将两个单元格中的旧坐标作为变量引用。哇噢!