0

我将参考下图: 在此处输入图像描述

我试图将 FirstValue 列拆分为它右侧的两列;但是,我想根据 Parameter 列拆分列。当 Parameter 值为奇数时,我只想将值复制到 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
4

3 回答 3

3

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

将这些复制到数据的末尾

假设格式相同 (Data,Col1,Col2,Parameter),但使用相对寻址
第 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将调整当前单元格的公式

于 2012-07-06T18:05:09.267 回答
1

对于单元格 D2:

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

说明:如果 Range E2 不能被 2 整除,则显示 B2 的值,否则不显示。

您可以通过在单元格 C2 的 MOD 周围插入“NOT”来扭转这种情况:

    =IF(NOT(MOD(E2,2)),B2,"")
于 2012-07-06T18:05:26.723 回答
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
于 2012-10-05T10:29:32.893 回答