为个案 VBA 中一个文本框的多个工作表定义工作表和变量

Defining worksheets and variables for mulitple worksheets for one textbox in VBA for cases

下面的代码块有问题。我正在扫描将文本放置在 ActiveX 文本框中的条形码。看到ActiveX文本框里的文字,就跑了一系列的案例。然后它执行必要的动作。我的猜测是文本框在 Worksheet(1) 中?或者我不知道如何正确调暗工作表或设置变量!

我也知道我需要将 0 更改为实际可能的单元格范围值。

Private Sub TextBox1_Change()

Dim bws As Worksheet, v, a, b, c, d, e, f, g, h, i, j, k, l
Set bws = Worksheets("PACKAGING-BOXES")

Dim cws As Worksheet, v, a, b, c, d, e, f, g, h, i, j, k, l
Set cws = Worksheets("PACKAGING-CARTONS")

Dim rws As Worksheet, v, a, b, c, d, e, f, g, h, i, j, k, l
Set rws = Worksheets("Cut ROlls")

v = TextBox1.Value
a = 0
b = 0
c = 0
d = 0
e = 0
f = 0
g = 0
h = 0
i = 0
j = 0
k = 0
l = 0


'a = Packing Units in one case
'b = Clip strip, row reference, PACKAGING-CARTONS
'c = Clip strip hooks, row reference, PACKAGING-CARTONS
'd = Number of clip strips or hooks used, PACKAGING-CARTONS
'e =  row reference, PACKAGING-CARTONS
'f = Number of polybags used
'g = Carton Number, row reference, PACKAGING-CARTONS-WAYNE
'h = Box Number 1, row reference, PACKAGING-BOXES
'i = Box Number 2, row reference, PACKAGING-BOXES
'j = Box Number 3, row reference, PACKAGING-BOXES
'k = row ID for cut rolls
'l = number for case selection



'_____________________________________________________________________________________________
Select Case v

Case 1: l = 10


a = 72
b = 0
c = 0
d = 0
e = 0
f = 0
g = 97
h = 35
i = 36
j = 0
k = 1000





    'other cases here....
'_____________________________________________________________________________________________
End Select

'_____________________________________________________________________________________________


If l = 1 Then
'Packed Items (Packing Report)


    cws.Cells(b, 8) = cws.Cells(b, 8).Value - d
    cws.Cells(b, 8) = cws.Cells(b, 9).Value + d

    cws.Cells(c, 8) = cws.Cells(c, 8).Value - d
    cws.Cells(c, 9) = cws.Cells(c, 9).Value + d

    cws.Cells(e, 8) = cws.Cells(e, 8).Value - f
    cws.Cells(e, 9) = cws.Cells(e, 9).Value + f

    cws.Cells(g, 8) = cws.Cells(g, 8).Value - (a * cws.Cells(1, 1))
    cws.Cells(g, 9) = cws.Cells(g, 9).Value + (a * cws.Cells(1, 1))



    bws.Cells(h, 8) = bws.Cells(h, 8).Value - a
    bws.Cells(h, 9) = bws.Cells(h, 9).Value + a

    bws.Cells(i, 8) = bws.Cells(i, 8).Value - a
    bws.Cells(i, 9) = bws.Cells(i, 9).Value + a

    bws.Cells(j, 8) = bws.Cells(j, 8).Value - a
    bws.Cells(j, 9) = bws.Cells(j, 9).Value + a



    rws.Cells(k, 1) = cws.Cells(k, 1).Value + (a * cws.Cells(1, 1))




    TextBox1.Activate
    TextBox1.Value = ""


        End If


        End Sub

感谢您的帮助!

明白了,这需要作为代码的开头。需要将 Wb 定义为变量而不是每个 sheet :)

Private Sub TextBox1_Change()
Dim wb As Workbook, v, a, b, c, d, e, f, g, h, i, j, k, l
Set wb = Workbooks("Inventory Tracking.xlsm")


Dim bws As Worksheet
Set bws = wb.Worksheets("PACKAGING-BOXES")

Dim cws As Worksheet
Set cws = wb.Worksheets("PACKAGING-CARTONS")

Dim rws As Worksheet
Set rws = wb.Worksheets("Cut Rolls")

Dim fws As Worksheet
Set fws = wb.Worksheets("Sheet1")

v = TextBox1.Value
a = 0
b = 0
c = 0
d = 0
e = 0
f = 0
g = 0
h = 0
i = 0
j = 0
k = 0
l = 0