0

我有一张名为 的工作表mainData,其中包含十种产品的所有数据。

当我在 中输入新数据时mainData,我想自动将新数据复制到另一个产品工作表的最后一行。当我输入新数据时mainData,如何识别新数据属于哪个产品的工作表,从而将新数据复制到产品工作表中?

我坚持将其复制到另一个工作表,因为我需要根据产品类型将其复制到另外十个工作表。

这是我对mainData:

With Sheets("mainData")
    LastRow = .Range("B" & .Rows.Count).End(xlUp).Row + 1

    .Range("B" & LastRow) = ComboBox1.Text
    .Range("C" & LastRow) = TextBox1.Text
    .Range("D" & LastRow) = TextBox2.Text
    .Range("E" & LastRow) = TextBox3.Text
    .Range("F" & LastRow) = TextBox4.Text
    .Range("G" & LastRow) = TextBox5.Text
    .Range("H" & LastRow) = ComboBox2.Text
    .Range("I" & LastRow) = TextBox6.Text
    .Range("J" & LastRow) = TextBox7.Text
    .Range("K" & LastRow) = TextBox8.Text


    Range("B32:B320").Select
    ActiveWorkbook.Worksheets("mainData").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("mainData").Sort.SortFields.Add Key:=Range("B32:B305") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
    "prod1, prod2, prod3, prod4, prod5, prod6, prod7, prod8, prod9, prod10" _
    , DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("mainData").Sort
    .SetRange Range("B32:W305")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

这就是我的意思。当我在 mainData 工作表中输入新的 prod1 数据时,我想自动将其复制到产品 1 工作表的最后一行。我可以在 mainData 中输入多种类型的产品,即 prod2、prod4,那么如何将这些数据复制到其特定的产品工作表中?

4

1 回答 1

0

这是你正在尝试的吗?(未经测试

我也没有做任何错误处理。我相信你会照顾好它:)

Dim prd As String
Dim ws As Worksheet
Dim LastRow As Long

'~~> Extract the number from the combobox
prd = Trim(Replace(ComboBox1.Text, "prod", ""))

'~~> Decide which sheet the data needs to be written to
'~~> Please ensure that sheets have names like "Product 1", "Product 2" etc
Set ws = ThisWorkbook.Sheets("Product " & prd)

'~~> Update it to the relevant sheet
With ws
    LastRow = .Range("B" & .Rows.Count).End(xlUp).Row + 1

    .Range("B" & LastRow) = ComboBox1.Value
    .Range("C" & LastRow) = TextBox1.Text
    .Range("D" & LastRow) = TextBox2.Text
    .Range("E" & LastRow) = TextBox3.Text
    .Range("F" & LastRow) = TextBox4.Text
    .Range("G" & LastRow) = TextBox5.Text
    .Range("H" & LastRow) = ComboBox2.Value
    .Range("I" & LastRow) = TextBox6.Text
    .Range("J" & LastRow) = TextBox7.Text
    .Range("K" & LastRow) = TextBox8.Text

    '~~> Sort the data
    With .Range("B2:W" & LastRow)
        .Sort Key1:=ws.Range("B2"), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    End With
End With

'~~> Update it in mainData
With Sheets("mainData")
    LastRow = .Range("B" & .Rows.Count).End(xlUp).Row + 1

    .Range("B" & LastRow) = ComboBox1.Value
    .Range("C" & LastRow) = TextBox1.Text
    .Range("D" & LastRow) = TextBox2.Text
    .Range("E" & LastRow) = TextBox3.Text
    .Range("F" & LastRow) = TextBox4.Text
    .Range("G" & LastRow) = TextBox5.Text
    .Range("H" & LastRow) = ComboBox2.Value
    .Range("I" & LastRow) = TextBox6.Text
    .Range("J" & LastRow) = TextBox7.Text
    .Range("K" & LastRow) = TextBox8.Text

    '~~> Sort the data
    With .Range("B2:W" & LastRow)
        .Sort Key1:=Sheets("mainData").Range("B2"), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    End With
End With
于 2013-02-18T08:14:29.197 回答