Excel宏用于基于列转置数据

时间:2014-06-20 18:09:36

标签: excel excel-vba vba

我是excel vb脚本的新手。我试图从excel表的多行复制值,具体取决于另一个coloumn中的值(prpId)。有多个' prpId'对于其中的每一个,在另一列中有多个值,我也需要检查,如果第二个条件也匹配,则将同一行的相邻列中存在的任何值复制到另一个单元格。

我写的代码是:

Sub TransData()
Dim prpId As long
prpId = 100011;
Dim prpIdRng As long
Dim i As long
i = 2
    While Cells(ActiveCell.Count, "a").Row <> Cells(Rows.Count, "a").End(xlUp).Row 
        While (Cells(ActiveCell.Row, "a").Value = prpId and Cells(ActiveCell.Row, "a").Value = "")
            If (Cells(ActiveCell.Row, "f").Value = "Co") then
                Set Cells(i, "k").Value = Cells(ActiveCell.Row, "G").Value  
                        ElseIf (Cells(i, "f").Value = "He")) then
                Set Cells(i, "l").Value = Cells(ActiveCell.Row, "G").Value 
                        ElseIf (Cells(ActiveCell.Row, "f").Value = "A") then
                Set Cells(i, "m").Value = Cells(ActiveCell.Row, "G").Value 
                        ElseIf (Cells(ActiveCell.Row, "f").Value = "B") then
                Set Cells(i, "n").Value = Cells(ActiveCell.Row, "G").Value 
                        ElseIf (Cells(ActiveCell.Row, "f").Value = "C") then
                Set Cells(i, "o").Value = Cells(ActiveCell.Row, "G").Value 
                        ElseIf (Cells((ActiveCell.Row, "f").Value = "D") then
                Set Cells(i, "p").Value = Cells(ActiveCell.Row, "G").Value 
                        ElseIf (Cells(ActiveCell.Row, "f").Value = "E") then
                Set Cells(i, "q").Value = Cells(ActiveCell.Row, "G").Value 
            End If
            ActiveCell.Row = ActiveCell.Row+1
        Wend
        prpId = Cells(ActiveCell.Row, "a").Value     
        i = i+1
    Wend        

当我运行此操作时,我遇到运行时错误。有人可以帮忙。

1 个答案:

答案 0 :(得分:0)

在尝试运行此代码时,我遇到了一些语法错误和运行时错误,

我调整了一些括号并修复了@Acantud提到的行问题:

Sub TransData()
Dim prpId As Long
prpId = (100011)
Dim prpIdRng As Long
Dim i As Long
i = 2
    While Cells(ActiveCell.Count, "a").Row <> Cells(Rows.Count, "a").End(xlUp).Row
        While (Cells(ActiveCell.Row, "a").Value = prpId And Cells(ActiveCell.Row, "a").Value = "")
            If (Cells(ActiveCell.Row, "f").Value = "Co") Then
                Set Cells(i, "k").Value = Cells(ActiveCell.Row, "G").Value
                        ElseIf (Cells(i, "f").Value = "He") Then
                Set Cells(i, "l").Value = Cells(ActiveCell.Row, "G").Value
                        ElseIf (Cells(ActiveCell.Row, "f").Value = "A") Then
                Set Cells(i, "m").Value = Cells(ActiveCell.Row, "G").Value
                        ElseIf (Cells(ActiveCell.Row, "f").Value = "B") Then
                Set Cells(i, "n").Value = Cells(ActiveCell.Row, "G").Value
                        ElseIf (Cells(ActiveCell.Row, "f").Value = "C") Then
                Set Cells(i, "o").Value = Cells(ActiveCell.Row, "G").Value
                        ElseIf (Cells(ActiveCell.Row, "f").Value = "D") Then
                Set Cells(i, "p").Value = Cells(ActiveCell.Row, "G").Value
                        ElseIf (Cells(ActiveCell.Row, "f").Value = "E") Then
                Set Cells(i, "q").Value = Cells(ActiveCell.Row, "G").Value
            End If
            ActiveCell.Offset(1, 0).Activate
        Wend
        prpId = Cells(ActiveCell.Row, "a").Value
        i = i + 1
    Wend
End Sub