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

我将参考下面的图片: 在这里输入图像说明

我试图把FirstValue列分成两列, 不过,我想分割基于参数列的列。 当参数值是奇数时,我想将值只复制到OtherValue1列。 当参数值是偶数时,我想只将值复制到OtherValue2列。 在阅读论坛并尝试excel的“文本到列”function后,我无法find解决scheme。

有没有办法实现这个使用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 

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

将这些复制到您的数据的末尾

假定采用相同的格式(Data,Col1,Col2,Parameter),但使用相对寻址
第2列=IF(MOD(OFFSET(C2,0,2),2)=1,OFFSET(C2,0,-1),"") =IF(MOD(OFFSET(D2,0,1),2)=0,OFFSET(D2,0,-2),"") 用当前单元replaceD2

再次,复制和粘贴 – 一旦你有第一个正确的,Excel会调整当前单元格的公式

对于单元格D2:

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

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

您可以通过在单元格C2的MOD周围插入“NOT”来取消该操作:

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

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