Excel:如何根据第三个奇数/偶数参数列将列拆分为两列?

时间:2012-07-06 17:55:16

标签: excel vba excel-vba excel-2007

我将参考下面的图片: enter image description here

我试图将FirstValue列拆分为它右边的两列;但是,我想根据参数列拆分列。当Parameter值为odd时,我想将值仅复制到OtherValue1列。当参数值为偶数时,我想将值仅复制到OtherValue2列。阅读论坛并尝试excel的“文本到列”功能后,我无法找到解决方案。

有没有办法使用VBA实现这个?

*注意:工作表实际上大约有10,000行,所以速度也很有帮助。

修改: 这是我到目前为止的代码。我在这行代码中遇到了对象错误:.Cells(2, MF1Col).Formula = "=IF(MOD(paraformula,2)=1,WTRfor,"")"

    Dim rw As Worksheet
Dim secondCell, MF1Cell, MF2Cell, paraCell, MF1formula, MF2formula, paraformula, WTRfor As Range
Dim secondCol As Long, MF1Col As Long, MF2Col As Long, paraCol As Long
 Set rw = ActiveSheet

With rw

    Set secondCell = .Rows(1).Find("FirstValue”)

    ' Check if the column with “FirstValue” is found

    'Insert Two Columns after FirstValue
    If Not secondCell Is Nothing Then
        secondCol = secondCell.Column
        .Columns(secondCol + 1).EntireColumn.Insert
        .Columns(secondCol + 2).EntireColumn.Insert
        .Cells(1, secondCol + 1).Value = "OtherValue1"
        .Cells(1, secondCol + 2).Value = "OtherValue2"
        .Activate

    Set MF1Cell = .Rows(1).Find("OtherValue1")
    MF1Col = MF1Cell.Column
    Set MF2Cell = .Rows(1).Find("OtherValue2")
    MF2Col = MF2Cell.Column
    Set paraCell = .Rows(1).Find("Parameter")
    paraCol = paraCell.Column

    Set paraformula = Range(.Cells(2, paraCol).Address(RowAbsolute:=False, ColumnAbsolute:=False))
    Set MF1formula = Range(.Cells(2, MF1Col).Address(RowAbsolute:=False, ColumnAbsolute:=False))
    Set WTRfor = Range(.Cells(2, secondCol).Address(RowAbsolute:=False, ColumnAbsolute:=False))
    .Cells(2, MF1Col).Formula = "=IF(MOD(" & paraformula & ",2)=1," & WTRfor & ","""")"
    Range(.Cells(2, MF1Col).Address).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    ActiveSheet.Paste

    Set MF2formula = Range(.Cells(2, MF2Col).Address(RowAbsolute:=False, ColumnAbsolute:=False))
    .Cells(2, MF2Col).Formula = "=IF(MOD(" & paraformula & ",2)=0," & WTRfor & ","""")"
    Range(.Cells(2, MF2Col).Address).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    ActiveSheet.Paste   

End If
End With

3 个答案:

答案 0 :(得分:3)

C2中的

=IF(MOD(E2,2)=1,B2,"")
在D2中,=IF(MOD(E2,2)=0,B2,"")

将这些复制到数据末尾

假设格式相同(数据,Col1,Col2,参数),但使用相对寻址
第1列:=IF(MOD(OFFSET(C2,0,2),2)=1,OFFSET(C2,0,-1),"") 将C2替换为当前单元格 第2列:=IF(MOD(OFFSET(D2,0,1),2)=0,OFFSET(D2,0,-2),"") 将D2替换为当前单元格

再次复制并粘贴 - 一旦你有第一个正确,excel将调整当前单元格的公式

答案 1 :(得分:1)

对于Cell D2:

    =IF(MOD(E2,2),B2,"")

说明: 如果范围E2不能被2整除,则显示值为B2,否则不显示任何内容。

你可以通过插入一个' NOT'来解决这个问题。在细胞C2的MOD附近:

    =IF(NOT(MOD(E2,2)),B2,"")

答案 2 :(得分:0)

VBA:

Sub odd_even()

a = 1                                                           ' start row
b = 10                                                          ' end row
c = 1                                                           ' column with values inputs

For d = a To b                                                  ' FOR loop from start row to end row

    If ActiveSheet.Cells(d, c) Mod 2 Then                       'mod becomes high when value is odd
        ActiveSheet.Cells(d, c + 2) = ActiveSheet.Cells(d, c)   'odd value gets copied to the odd-column   ( two to the right of the values)
        ActiveSheet.Cells(d, c + 3) = ""                        'same row on even-column gets cleared
    Else:
        ActiveSheet.Cells(d, c + 3) = ActiveSheet.Cells(d, c)   'even value gets copied to the even-column ( three to the right of the values)
        ActiveSheet.Cells(d, c + 2) = ""                        'same row on odd-column gets cleared
    End If

Next d                                                          ' go to next row

End Sub