【问题标题】:VBA, dividing an xlsx into multiple sheetsVBA,将一个xlsx分成多张
【发布时间】:2017-10-19 22:49:09
【问题描述】:

我不经常使用 VBA,所以我需要一些帮助。 .xlsx,第 1 行有列标题。要处理的第一个工作表名为“已提交” 第一栏是“年金”,我正在寻找什么。在此列下将是名称。名称和行数不断变化。 (问题:某些单元格可能有多个名称用“,”分隔,并且名称将在多个单元格中列出)exp:

I2 = "John Doe"


I3 是“John Doe”


I4 是“John Doe, Jane Doe”(如果发生这种情况,我们只需要第一个完整的名字,停在“,”


I5 是“Jane Doe”等等。

需要发生的事情是每行都需要剪切并粘贴到一个新工作表上,标题为“I_Submitted 列”中的名称,具有相同的列标题并将所有 John Doe 和 Jane Doe 组合在一张表上在单独的纸上。

一旦这张表被清除,下一张表是“付费”,做与“提交”相同的事情 最后是我们要读取的“D”列上的工作表“YTD”,其过程与其他两张工作表完全相同。

感谢您的帮助。

【问题讨论】:

  • 是的,打开编辑器并尝试成功创建一个循环来读取名称,但是从我的帖子中可以看出,我几乎不处理 VBA。我什至回到了我过去的问题,因为我认为我可以从那里提出一些我需要的东西,但是对于像我这样获得帮助的人来说,令人沮丧的部分是大多数时候帮助的“聪明”人没有有时间告诉你他们为什么要做他们所做的事情,所以几乎不可能学习,因为我们中的一些人一年只使用 1 或 2 次 VBA。感谢您表达您的不满。

标签: excel vba


【解决方案1】:

毫无疑问,这对于 VBA 新手来说是一项艰巨的任务。这里有很多小块很难弄清楚,比如遍历每一行,创建一个新的工作表,弄清楚你要复制到的工作表中的哪一行是最后一行,弄清楚如何获得名字来自逗号分隔的列表等。

下面的代码应该完全符合您的需要。我在每一行之前做了很多 cmets 来解释它在做什么。它应该给你一个坚实的起点。

Sub splitUpRows()

    'set up a variable to hold the worksheet from which we will be reading each row
    Dim ws_read As Worksheet
    Set ws_read = ThisWorkbook.Sheets("Submitted")

    'set up a variable to hold the worksheet to which we will write the data
    Dim ws_write As Worksheet

    'A variable to hold whether the ws_write sheet already exists
    Dim ws_write_exists As Boolean

    'A variable to hold which row we are reading, and one to hold which row we are writing
    Dim readRow As Integer, writeRow As Integer

    'A variable to hold the name of the person we are processing
    Dim str_name As String

    'Now loop from row 2 to the last row in the ws_read worksheet
    For readRow = 2 To ws_read.Range("A50000").End(xlUp).Row

        'get the name of the person (or if there are two, get the first one)
        'There are few ways to get the firstname, but here I am "Splitting"
        '  the value in Column I for this row to an array. I am then requesting
        '  the 1st item "(0)" from that array.
        'This is a nice way of doing it because you could quickly 
        '  change this to a for loop for each item in the array
        '  in case you wanted a row for each person from the same line.
        str_name = Split(ws_read.Cells(readRow, 9).Value, ",")(0)

        'Check to see if this sheet already exists by looping through all
        'of the worksheets in this workbook.
        'If it does, then set the ws_write_exists to true and exit the loop
        ws_write_exists = False
        For Each ws_write In ThisWorkbook.Worksheets
            If ws_write.Name = str_name Then ws_write_exists = True: Exit For
        Next ws_write

        'If it doesn't exist, then make it.
        'If it does exist, then it's already set to the right worksheet so
        'no worries just move along.
        If Not ws_write_exists Then

            'Add a new worksheet
            Set ws_write = ThisWorkbook.Worksheets.Add

            'name it after the person
            ws_write.Name = str_name

            'And copy the header row from ws_read to the first row of ws_write
            ws_write.Rows(1).Value2 = ws_read.Rows(1).Value2
        End If

        'So now we definitely have a worksheet for this person
        'And we definitely have ws_write set to that worksheet
        'Lets figure out what row we are writing to
        writeRow = ws_write.Range("A50000").End(xlUp).Row + 1

        'And now lets copy from ws_read to ws_write
        ws_write.Rows(writeRow).Value2 = ws_read.Rows(readRow).Value2
    Next readRow


End Sub

要使用“付费”表执行此操作,只需将 ws_read 更改为指向“付费”即可。对于您的“YTD”表,您必须更改 ws_read 并将 str_name 更改为指向第 4 列而不是第 9 列。

【讨论】:

  • JNevill,我不知道这是否有效,但这太棒了.. 我只为 cmets 给你投票。我喜欢能够阅读代码并了解它的意图!太棒了!
  • 是的!没有什么比在没有任何 cmets 来减轻打击的情况下被一堆代码击中更糟糕的了。我刚刚用一个fakey 标头对它进行了3 或4 行的测试,它在我眨眼之前就把它炸飞了,所以我想你会好运的。您会看到它复制了整行,因此如果您有其他数据挂在右侧的列中,那么您也会选择这些数据。只是一个警告。
  • 赞成。我现在将尝试以这种格式回答问题。谢谢@JNevill先生。
  • 太棒了,谢谢你们。非常感激。效果很好,我正在尝试用它做一些其他的事情,我将通过 cmets 和一些研究工作.. 再次感谢!
  • 我很高兴听到它在做这项工作。如果您遇到任何问题或后续类型的问题,请继续在 SO 上创建一个全新的问题。这样它就会出现在更多的眼球面前。这里有一些知识渊博的人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-14
  • 2015-12-08
  • 2013-06-11
  • 2016-09-26
相关资源
最近更新 更多