将列分隔的单元格分成一列对应父列?

时间:2016-04-25 18:43:46

标签: excel vba excel-vba excel-2010

我已经获得了一张真正混乱的桌子,我需要帮助清理其中的一些数据。

字段按地区,区域和商店分组。每个商店编号都应该拥有自己的新行。 这就是现在的表格:

╔════════╦═══════════════╦════════════════════════╗
║ Region ║   District    ║         Store          ║
╠════════╬═══════════════╬════════════════════════╣
║ West   ║ N. California ║ 1, 2, 5, 8, 22, 23, 32 ║
║ West   ║ S. California ║ 6, 7, 9, 12, 15        ║
║ East   ║ E. New York   ║ 18, 26, 27, 54, 88     ║
╚════════╩═══════════════╩════════════════════════╝

这就是我需要的样子:

╔════════╦═══════════════╦═══════╗
║ Region ║   District    ║ Store ║
╠════════╬═══════════════╬═══════╣
║ West   ║ N. California ║     1 ║
║ West   ║ N. California ║     2 ║
║ West   ║ N. California ║     5 ║
║ West   ║ N. California ║     8 ║
║ West   ║ N. California ║    22 ║
║ West   ║ N. California ║    23 ║
║ West   ║ N. California ║    32 ║
║ West   ║ S. California ║     6 ║
║ West   ║ S. California ║     7 ║
╚════════╩═══════════════╩═══════╝

我有一个宏可以获取所有商店编号并将它们放入一列,但它没有考虑Region / District列。宏只是将所有数字放在一列中。 这是我使用的宏:

Sub Macro1()
    Dim fromCol As String
    Dim toCol As String
    Dim fromRow As String
    Dim toRow As String
    Dim inVal As String
    Dim outVal As String
    Dim commaPos As Integer

    ' Copy from column A to column B.'
    fromCol = "F"
    toCol = "H"
    fromRow = "1"
    toRow = "1"

    ' Go until no more entries in column A.'
    inVal = Range(fromCol + fromRow).Value
    While inVal <> ""

        ' Go until all sub-entries used up.'
        While inVal <> ""
            Range(fromCol + fromRow).Select

            ' Extract each subentry.'
            commaPos = InStr(1, inVal, ",")
            While commaPos <> 0

                ' and write to output column.'
                outVal = Left(inVal, commaPos - 1)
                Range(toCol + toRow).Select
                Range(toCol + toRow).Value = outVal
                toRow = Mid(Str(Val(toRow) + 1), 2)

                ' Remove that sub-entry.'
                inVal = Mid(inVal, commaPos + 1)
                While Left(inVal, 1) = " "
                    inVal = Mid(inVal, 2)
                Wend
                commaPos = InStr(1, inVal, ",")
            Wend

            ' Get last sub-entry (or full entry if no commas).'
            Range(toCol + toRow).Select
            Range(toCol + toRow).Value = inVal
            toRow = Mid(Str(Val(toRow) + 1), 2)
            inVal = ""
        Wend

        ' Advance to next source row.'
        fromRow = Mid(Str(Val(fromRow) + 1), 2)
        Range(fromCol + fromRow).Select
        inVal = Range(fromCol + fromRow).Value
    Wend
End Sub

有人可以协助我提供更有效的方法吗? 我真的很感激任何帮助!

编辑: 这是我的实际表格。以上只是一个例子。

╔═══════════╦══════════════════════╦════════════════════════════════════════════════════════════════════════╗
║ Region    ║ District             ║ Stores                                                                 ║
╠═══════════╬══════════════════════╬════════════════════════════════════════════════════════════════════════╣
║ SOUTHWEST ║ Arizona North        ║ 13, 168, 179, 309, 379, 391, 440, 455, 528, 608, 663, 675              ║
║ SOUTHWEST ║ Arizona South        ║ 35, 36, 37, 218, 252, 302, 447, 510, 535, 545                          ║
║ WEST      ║ Bay Area             ║ 17, 19, 38, 205, 268, 284, 349, 361, 362, 386, 476, 494, 591, 601, 638 ║
║ SOUTHEAST ║ Central Florida      ║ 108, 133, 189, 228, 354, 366, 454, 485, 492, 565, 667, 676             ║
║ SOUTHWEST ║ Central Texas        ║ 69, 94, 183, 195, 248, 285, 318, 385, 491, 512, 622                    ║
║ WEST      ║ Central Valley       ║ 4, 7, 9, 16, 241, 450, 490, 516, 600                                   ║
║ EAST      ║ Chicago              ║ 126, 178, 187, 295, 418, 427, 497, 543, 669, 693                       ║
║ NORTHEAST ║ Connecticut          ║ 260, 273, 306, 307, 312, 321, 341, 367, 396, 398, 473, 585, 661        ║
║ SOUTHEAST ║ Crystal Coast        ║ 82, 87, 91, 191, 327, 352, 589, 593, 609, 627, 633                     ║
║ NORTHEAST ║ Detroit              ║ 117, 143, 145, 153, 170, 210, 235, 320, 407, 458, 522, 588, 660        ║
║ EAST      ║ East Pennsylvania    ║ 139, 140, 151, 159, 162, 164, 261, 328, 329, 344, 513                  ║
║ SOUTHEAST ║ Florida East         ║ 128, 150, 163, 436, 466, 536, 538, 648, 655                            ║
║ SOUTHEAST ║ Georgia/Alabama      ║ 95, 106,110, 148, 350, 353, 364, 486, 546, 570, 582, 616, 689          ║
║ MIDWEST   ║ Heartland            ║ 74, 75, 76, 186, 239, 258, 388, 574, 619, 624                          ║
║ MIDWEST   ║ Houston East         ║ 83, 193, 197, 247, 324, 378, 493, 617                                  ║
║ MIDWEST   ║ Houston West         ║ 71, 79, 172, 246, 254, 323, 372, 431, 460, 483, 547, 680               ║
║ WEST      ║ Inland Empire        ║ 26, 28, 31, 33, 380, 384, 404, 464, 523, 525, 580, 592, 636, 654       ║
║ EAST      ║ Long Island          ║ 337, 347, 368, 375, 376, 390, 403, 430, 521                            ║
║ MIDWEST   ║ Louisiana            ║ 80, 81, 90, 100, 272, 275, 336, 508, 511, 576, 631                     ║
║ NORTHEAST ║ Michigan             ║ 152, 157, 188, 265, 308, 428, 437, 446, 479, 583, 628, 630, 664        ║
║ EAST      ║ Mid America          ║ 114, 134, 135, 142, 280, 358, 400, 424, 471, 475, 481, 503, 670        ║
║ EAST      ║ Mid Atlantic         ║ 161, 166, 175, 177, 181, 206, 315, 413, 448, 451, 489, 568, 595        ║
║ MIDWEST   ║ Minnesota/Wisconsin  ║ 88, 132, 137, 156, 212, 219, 253, 271, 356, 359, 482, 488, 612         ║
║ SOUTHEAST ║ N Carolina East      ║ 101, 104, 120, 220, 240, 264, 269, 276, 335, 369, 563, 632, 682        ║
║ SOUTHEAST ║ N Carolina West      ║ 99, 192, 208, 299, 434, 505, 529, 610, 618, 671, 677, 685              ║
║ NORTHEAST ║ New England          ║ 298, 338, 342, 345, 351, 405, 409, 439, 442, 465, 467, 478             ║
║ EAST      ║ New Jersey           ║ 176, 182, 199, 201, 215, 281, 326, 393, 397, 432, 537                  ║
║ SOUTHWEST ║ New Mexico           ║ 55, 56, 65, 67, 70, 72, 236, 402, 417, 443, 586, 691                   ║
║ NORTHEAST ║ New York East        ║ 154, 173, 196, 334, 355, 517, 639, 646, 662                            ║
║ NORTHEAST ║ New York West        ║ 147, 149, 158, 233, 245, 487, 641, 643, 647                            ║
║ SOUTHEAST ║ North Florida        ║ 107, 115, 138, 213, 238, 294, 325, 435, 449, 468, 566, 668             ║
║ MIDWEST   ║ North Indiana        ║ 111, 113, 116, 222, 224, 229, 234, 438, 441, 573                       ║
║ EAST      ║ North Jersey         ║ 237, 293, 340, 343, 365, 381, 389, 395, 420, 421, 524                  ║
║ SOUTHWEST ║ North Texas          ║ 53, 61, 73, 167, 185, 311, 317, 422, 502, 575, 635, 657                ║
║ EAST      ║ Ohio North           ║ 121, 123, 130, 160, 202, 221, 249, 461, 484, 584, 594                  ║
║ EAST      ║ Ohio South           ║ 112, 119, 180, 231, 232, 423, 549, 550                                 ║
║ MIDWEST   ║ Ohio Valley          ║ 1, 25, 122, 125, 184, 211, 262, 274, 305, 410, 518, 604                ║
║ SOUTHWEST ║ Oklahoma             ║ 48, 63, 64, 66, 251, 267, 357, 360, 462, 472, 590                      ║
║ WEST      ║ Orange County        ║ 12, 20, 27, 29, 34, 373, 401, 414, 425, 433, 571, 526, 605             ║
║ WEST      ║ Portland             ║ 30, 39, 40, 45, 46, 169, 243, 255, 278, 429, 452, 477, 581             ║
║ SOUTHWEST ║ Rocky Mountain North ║ 58, 59, 68, 77, 136, 230, 263, 266, 603, 673                           ║
║ SOUTHWEST ║ Rocky Mountain South ║ 62, 171, 174, 203, 314, 394, 419, 498, 500, 620, 696                   ║
║ WEST      ║ Sacramento/Nevada    ║ 11, 21, 22, 23, 24, 204, 310, 383, 540, 614, 656, 659                  ║
║ WEST      ║ San Diego            ║ 8, 14, 15, 18, 42, 226, 348, 411, 412, 444, 445, 506, 515              ║
║ WEST      ║ San Fernando         ║ 2, 3, 5, 6, 10, 32, 200, 217, 374, 459, 572, 602, 681                  ║
║ SOUTHEAST ║ South Carolina       ║ 96, 103, 129, 209, 223, 319, 331, 532, 606, 634, 645, 651, 672         ║
║ SOUTHEAST ║ South Florida        ║ 124, 131, 146, 282, 316, 346, 501, 504, 519, 644, 649                  ║
║ SOUTHWEST ║ South Texas          ║ 57, 60, 198, 225, 257, 259, 297, 371, 426, 463, 613                    ║
║ MIDWEST   ║ St. Louis            ║ 78, 92, 93, 127, 155, 194, 242, 290, 304, 370, 415, 534, 679           ║
║ MIDWEST   ║ Tennessee East       ║ 84, 86, 89, 118, 214, 270, 301, 303, 495, 587, 611, 623, 653           ║
║ MIDWEST   ║ Tennessee West       ║ 85, 286, 287, 289, 406, 640, 642, 652                                  ║
║ SOUTHWEST ║ Utah                 ║ 41, 43, 44, 54, 105, 292, 332, 333, 363, 480, 520, 569                 ║
║ EAST      ║ Virginia East        ║ 97, 102, 109, 244, 300, 322, 416, 626                                  ║
║ EAST      ║ Virginia West        ║ 98, 227, 313, 514, 527, 625                                            ║
║ WEST      ║ Washington North     ║ 52, 207, 279, 330, 339, 377, 530, 621, 629, 690                        ║
║ WEST      ║ Washington South     ║ 47, 49, 50, 51, 190, 296, 392, 453, 457, 533, 596, 674                 ║
║ SOUTHEAST ║ West Florida         ║ 141, 144, 165, 256, 283, 399, 456, 469, 470, 474, 509, 665             ║
║ EAST      ║ West Pennsylvania    ║ 216, 250, 277, 288, 291, 408, 496, 531, 541, 564, 567, 615             ║
╚═══════════╩══════════════════════╩════════════════════════════════════════════════════════════════════════╝

1 个答案:

答案 0 :(得分:1)

试试这个

Option Explicit

Sub Macro1()
    Dim myArr As Variant
    Dim iRegion As Long, iRow As Long

    With ThisWorkbook.Worksheets("Stores") '<== change it as per your needs
        With .Range("A2:A" & .Cells(.rows.Count, 1).End(xlUp).Row).Resize(, 3) 'I'm assuming that "Region" is column "A", "District" is column "B" and "Store" is column "C"
            myArr = .Value
            .ClearContents
        End With
        For iRegion = LBound(myArr, 1) To UBound(myArr, 1)
            With .Cells(.rows.Count, 1).End(xlUp).Offset(1).Resize(UBound(Split(myArr(iRegion, 3), ",")) + 1)
                .Offset(, 0) = myArr(iRegion, 1)
                .Offset(, 1) = myArr(iRegion, 2)
                .Offset(, 2) = Application.Transpose(Split(myArr(iRegion, 3), ","))
            End With
        Next iRegion
    End With
End Sub