连接单元格但之前检查值 - Excel VBA

朱拉努

所以我想从串联的值D column在的那些J column,到目前为止好,但我想检查值从column D已经在J column,我用InStr它工作,直到它符合的单元格的值已经是不需要连接

脚本:

    Sub Concatenate()
    Dim counter As Integer
    Dim myValue As Variant
    myValue = InputBox("For how many rows do you want to concatenate D w/ Js")
    d = 4 ' Concatenate from this column
    j = 10 'Concatenate to this column
    ' counter -> Counter to iterate through all the cells in both columns
    For counter = 2 To myValue
      If Cells(counter, d).Value <> "" Then
        If InStr(1, Cells(counter, j).Value, Cells(counter, d).Value) = 0 Then
          Cells(counter, j).Value = Cells(counter, j).Value & "," & Cells(counter, d).Value
        Else
          ' Had to leave it blank
        End If
      End If

    Next counter

End Sub

例子

How it looks                         How it should look
|    D    |   J     |                 |    D     |      J      |
|    a    |   1     |result of code =>|    a     |     1,a     |
|    b    |   2,b   |result of code =>|    b     |     2,b     | <= (nothing modified)
|    c    |   3     |result of code =>|    c     |     3,c     |
|   abc   |   42    |result of code =>|   abc    |     42,abc  |

当遇到具有值的行时,| b | 2,b |程序停止,其余部分保持不变

朱拉努

最终代码,工作:

Sub Concatenate()
Dim counter As Integer
Dim myValue As Variant
myValue = InputBox("For how many rows do you want to concatenate D w/ Js")
d = 4 ' Concatenate from this column
j = 10 'Concatenate to this column
' counter -> Counter to iterate through all the cells in both columns
For counter = 2 To myValue
  If Cells(counter, d).Value <> "" Then
    If InStr(1, Cells(counter, j).Value, Cells(counter, d).Value) = 0 Then
      Cells(counter, j).Value = Cells(counter, j).Value & "," & Cells(counter, d).Value
    Else
      ' Blank
    End If
  End If
Next counter

End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章