简体   繁体   English

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

[英]Vba separate multiple dates in one cell

I am trying to separate multiple dates from one cell into multiple cells containing one date in a transposed area and then paste them back over the original area as separate entries. 我正在尝试将一个单元格中的多个日期分隔为在转置区域中包含一个日期的多个单元格,然后将它们作为单独的条目粘贴回原始区域。

An example cell might have dates stored like 10/1110/1110/13 or 10/310/310/410/5. 示例单元格的日期可能存储为10/1110/1110/13或10/310/310/410/5。 The second scenario is what is causing the error as there is no leading zero for single digit days like 10/3, for example. 第二种情况是导致错误的原因,例如在10/3之类的单个数字天中没有前导零。

Ideally, the code would separate the dates into separate cells like: 10/11,10/11,10/13 and 10/3,10/4,10/5. 理想情况下,代码会将日期分成单独的单元格,例如:10 / 11,10 / 11,10 / 13和10 / 3,10 / 4,10 / 5。 When single digits days are present ,however, it comes out completely jumbled up and inaccurate. 但是,当存在一位数字的日子时,它就会完全混乱并且不准确。

Admittedly, I had help from another coworker with this code who is on vacation currently, which is why I am having such trouble understanding this. 诚然,我从另一个正在与此代码休假的同事那里获得帮助,这就是为什么我很难理解这一点的原因。 Is there something I could change to account for single digit days or should I approach this process differently? 我是否可以更改一些单位天的收入,或者应该以不同的方式处理此过程?

Thanks! 谢谢!

'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

If all of the months within one cell's mashed up dates can be reasonably assumed to be the same then that could be used as a delimiter to split the mash-up and reassemble it. 如果可以合理地假设一个单元格中的所有月份的混搭日期都相同,则可以将其用作分隔符,以分解并重新组合。

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM