Vba在一个单元格中分隔多个日期

时间:2017-12-07 19:32:40

标签: excel vba excel-vba

我正在尝试将多个日期从一个单元格分成多个单元格,这些单元格在转置区域中包含一个日期,然后将它们作为单独的条目粘贴回原始区域。

示例单元格的日期可能存储为10/1110/1110/13或10/310/310/410/5。 第二种情况是造成错误的原因,例如10/3等单位数天没有前导零。

理想情况下,代码会将日期分成单独的单元格,如:10 / 11,10 / 11,10 / 13和10 / 3,10 / 4,10 / 5。但是,当存在单个数字天时,它会完全混乱并且不准确。

不可否认,我得到了另一位同事正在度假的同事的帮助,这就是我理解这个问题的原因。有没有什么我可以改变以占一位数的日子或我应该采用不同的方式处理这个过程?

谢谢!

 
'separate column J by "/" and store in transpose area

  dim h as variant
  dim i as variant
  dim j as variant
  dim counter as variant
  dim stringcheck as variant
  dim strInput as variant
  dim strCurrent as variant



 strInput = Cells(j, 10)
 h = 0

For counter = 1 To Len(strInput) - 2
    stringcheck = InStr(strInput, "/")
    Debug.Print j & stringcheck
    If stringcheck <> 0 Then


        If Mid(strInput, counter, 1) = "/" Then
            Cells(17, i + h) = strCurrent & Mid(strInput, counter, 3)
            counter = counter + 2
            h = h + 1
            strCurrent = vbNullString
        Else
            Cells(17, i + h) = Cells(j, 10)
            strCurrent = strCurrent & Mid(strInput, counter, 1)

        End If

    'else just paste the value
    Else
        Cells(17, i) = strInput
    End If

Next counter
 

1 个答案:

答案 0 :(得分:0)

如果一个单元格的混合日期中的所有月份都可以合理地假设为相同,则可以将其用作分隔混搭的分隔符并重新组合。

Function splitMashUp(str As String, _
                     Optional splitchr As String = "/", _
                     Optional delim As String = ", ")
    Dim i As Long, tmp As Variant

    tmp = Split(str, Left(str, InStr(1, str, splitchr)))
    For i = LBound(tmp) + 1 To UBound(tmp)
        tmp(i) = Left(str, InStr(1, str, splitchr)) & tmp(i)
    Next i
    splitMashUp = Mid(Join(tmp, delim), Len(delim) + 1)
End Function

enter image description here