如何使用用户表单将数据发送到不同的标签?

时间:2017-08-17 10:16:08

标签: database excel vba excel-vba

我创建了一个电子表格,用于通过用户表单记录大量数据。在此用户表单中,初始数据将始终转到电子表格中的masterdata选项卡。然而,我的问题是我有另外3个工作表,ws2,ws3和ws4。

现在基于用户表单中的4个字段,有16种可能的不同结果,这些结果将决定是否还需要将此数据包含在ws2& ws3,或者ws3,或者ws 2,或者ws2和ws 4,或者只是ws 4等....

规则如下:

enter image description here

有人可以让我知道如何在必要的表格中获取这些信息。以下是我的userform代码

Dim iRow As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim ws4 As Worksheet
Dim Nextnum As Long
Dim Xnum As Long

Set ws1 = Worksheets("MasterData")
Set ws2 = Worksheets("X")
Set ws3 = Worksheets("A")
Set ws4 = Worksheets("C")

Nextnum = Sheets("MasterData").Range("A2").End(xlDown).Value + 1
Xnum = Sheets("X").Range("A2").End(xlDown).Value + 1
ANum = Sheets("A").Range("A2").End(xlDown).Value + 1
CNum = Sheets("C").Range("A2").End(xlDown).Value + 1

'find first empty row in database
mrow = ws1.Cells.Find(what:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

'copy the data to the database
    ws1.Cells(mrow, 1).Value = Nextnum
    ws1.Cells(mrow, 2).Value = Format(Date, "DD/MM/YYYY")
    ws1.Cells(mrow, 3).Value = Format(Time, "HH:MM:SS")
    ws1.Cells(mrow, 4).Value = CInt(Format(Date, "WW"))
    ws1.Cells(mrow, 5).Value = Format(Date, "MMM-YY")
    ws1.Cells(mrow, 6).Value = CInt(Format(Date, "YYYY"))
    ws1.Cells(mrow, 7).Value = 1
    ws1.Cells(mrow, 8).Value = TxtWeight.Value * (1300 / 1000)
    ws1.Cells(mrow, 9).Value = Application.WorksheetFunction.VLookup(ComboBrd.Value, Sheets("Lookup Vals").Range("G:H"), 2, False)
    ws1.Cells(mrow, 10).Value = Application.UserName
                If ComboBrd.Value = "Mn" Then ws1.Cells(mrow, 11).Value = Application.WorksheetFunction.VLookup(ComboCom.Value, Sheets("Lookup Vals").Range("L:N"), 2, False) Else
                If ComboBrd.Value = "Pr" Then ws1.Cells(mrow, 11).Value = Application.WorksheetFunction.VLookup(ComboCom.Value, Sheets("Lookup Vals").Range("P:R"), 2, False) Else
                    If ComboBrd.Value = "Vot" Then ws1.Cells(mrow, 11).Value = Application.WorksheetFunction.VLookup(ComboCom.Value, Sheets("Lookup Vals").Range("P:R"), 2, False)
    ws1.Cells(mrow, 12).Value = TxtRecDate.Value
    ws1.Cells(mrow, 13).Value = ComboPD.Value
    ws1.Cells(mrow, 14).Value = ComboNP.Value
    ws1.Cells(mrow, 15).Value = ComboBrd.Value
    ws1.Cells(mrow, 16).Value = ComboCom.Value
    ws1.Cells(mrow, 17).Value = TxtAdditional.Value
    ws1.Cells(mrow, 18).Value = TxtDOD.Value
    ws1.Cells(mrow, 19).Value = TxtBn.Value
    ws1.Cells(mrow, 20).Value = TxtFS.Value
    ws1.Cells(mrow, 21).Value = ComboPrdG.Value
    ws1.Cells(mrow, 22).Value = ComboIss.Value
    ws1.Cells(mrow, 23).Value = TxtUni.Value
    ws1.Cells(mrow, 24).Value = TxtWet.Value
    ws1.Cells(mrow, 25).Value = TxtInc.Value
    ws1.Cells(mrow, 26).Value = TxtDet.Value
    ws1.Cells(mrow, 27).Value = TxtShr.Value

如果有人可以提供帮助,我们将不胜感激。

谢谢,

1 个答案:

答案 0 :(得分:0)

我认为选择案例会有助于创建一系列应填写的工作表。只需循环并添加数据:

Dim targetWorksheets As Variant

Select Case True
Case c1 And c2 And c3 >= 50 And c4 <= 1: targetWorksheets = Array(ws1, ws2, ws3)
Case c1 And c2 And c3 >= 50 And c4 > 1: targetWorksheets = Array(ws1, ws2, ws3)
'etc ....
Case Else: targetWorksheets = Array(ws1)
End Select 

For Each ws In targetWorksheets
    mrow = ws.Cells.Find(what:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
    ws.Cells(mrow, 1).Value = Nextnum
    ws.Cells(mrow, 2).Value = Format(Date, "DD/MM/YYYY")
    ws.Cells(mrow, 3).Value = Format(Time, "HH:MM:SS")
    ws.Cells(mrow, 4).Value = CInt(Format(Date, "WW"))
    'etc
Next ws

的Gr