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

时间:2016-09-01 20:06:33

标签: excel vba excel-vba activex

我遇到下面的代码块问题。我正在扫描条形码,将文本放在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

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

想出来,这需要是代码的开始。需要将Wb定义为变量而不是每个工作表:)

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