根据第一列上的索引值将多个列值转换为行

时间:2014-09-29 10:31:13

标签: excel-vba vbscript offset vba excel

我有一个带有输入的工作表,如下例所示:

5 | abc | def | aabc | 1234 | 2222
3 | 123 | 456 | 777
4 | 990 | 1w1 | wsd | asde
..
.. [lets say there is a fixed 500 rows]

第一列是索引,后面跟着行中相应的值数。

我需要在新工作表中输出如下所示:

5 | abc
5 | def
5 | aabc
5 | 1234
5 | 2222
3 | 123
3 | 456
3 | 777
4 | 990
4 | 1w1
4 | wsd
4 | asde

===== 其他信息:

订单并不重要。我尝试了以下内容。

我从vbscript中稍微修改了这个post并获得了以下内容:

Col A | Col B

5 | ABC
5 |高清
5 | AABC
5 | 1234
5 | 2222
3 | 123个
3 | 456个
3 | 777个
3 |
3 |
4 | 990个
4 | 1W1
4 | WSD
4 | ASDE
4 |

现在我只需要删除第二列为空的行。有什么想法吗?

以下是修改后的vbscript:

Sub multi()
Dim iCounter As Integer
    Dim iInner As Integer
    Dim k As Long
    Dim ws As Worksheet
    Dim wsNew As Worksheet
    Dim last_col As Long
    Set rCell = Range("A1:A" & Range("A1").End(xlDown).Row)
    Set wsNew = Sheets.Add()
'    last_col = ActiveCell.End(xlToRight).Column
    SetActive
    last_col = ActiveCell.Value
    For Each ws In Worksheets
        iCounter = 1
        For Each cCell In rCell
          For iInner = 1 To last_col
              With wsNew
                  .Cells(iCounter, 1).Value = cCell.Value
                  .Cells(iCounter, 2).Value = cCell.Offset(0, iInner).Value
              End With
              iCounter = iCounter + 1
              MoveActive
          Next iInner
        Next cCell
    Next ws
End Sub

Sub SetActive()
    Worksheets("Sheet1").Activate
    Worksheets("Sheet1").Range("A1").Activate
'    ActiveCell.Font.Bold = True
End Sub

Sub MoveActive()
    Worksheets("Sheet1").Activate
    Range("A1:A2500").Select
'    ActiveCell.Value = "Monthly Totals"
    ActiveCell.Offset(0, 1).Activate
End Sub

0 个答案:

没有答案