【问题标题】:Copy new data into other worksheets将新数据复制到其他工作表中
【发布时间】:2013-02-18 06:01:02
【问题描述】:

我有一个名为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,那么如何将这些数据复制到其特定的产品工作表中?

【问题讨论】:

  • how can I recognize the new data belongs to which product's worksheet, 呃,你必须告诉我们(至少在逻辑上)如何区分。 Product type 是哪个字段,它与特定工作表有什么关系,即工作表名称是产品类型的指示符还是其他什么?或许你也可以举个例子?
  • @SiddharthRout 是的,工作表名称是产品类型的指示符。如果产品类型是 prod2 意味着我需要将其复制到工作表产品 2。
  • 那么 Col B 会有产品代码吗?
  • 是的。每个产品都有不同的产品代码。
  • 还有两个问题。 1 会不会只有 prod1-10? 2需要添加到mainData还是直接添加到相关的sheet中?

标签: excel vba


【解决方案1】:

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

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

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

【讨论】:

  • thanks.yes 它适用于产品工作表。实际上我想要做的是复制到另一个工作簿中,而不是复制到同一个工作簿中的工作表中。无论如何感谢您的帮助。我真的是 vba 的新手。仍然模糊了如何链接工作簿
  • 我刚刚又解决了你的问题。您在哪里提到必须将其复制到其他工作簿中的工作表中?
  • 我真的很抱歉。只是意识到我没有提到它。是的,我想将它复制到其他工作簿中的工作表中。我的错误
猜你喜欢
  • 1970-01-01
  • 2022-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-12
  • 1970-01-01
相关资源
最近更新 更多