Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 在操作日期或时间时,格式会更改_Vba_Date_Excel - Fatal编程技术网

Vba 在操作日期或时间时,格式会更改

Vba 在操作日期或时间时,格式会更改,vba,date,excel,Vba,Date,Excel,对于编写Excel宏的普通VB程序员来说,这一定是一个小菜一碟 我试图将-HH:-MM:-SS格式中的负时间转换为HH:MM:SS,并交换前两列的值,只有在找到负日期时,前两列才是同一行中的时间戳 日期正在从负数转换为正数,但对于例如,从-01:-05:-14,它将更改为凌晨1:05:14。首先,缺少“0”,并在末尾添加“AM”,而不是01:05:14 此外,交换时间戳的格式从2014年1月4日04:31:31 AM更改为2014年1月4日4:31:31 AM,初始“0”丢失。 请帮我解决这个问

对于编写Excel宏的普通VB程序员来说,这一定是一个小菜一碟

我试图将-HH:-MM:-SS格式中的负时间转换为HH:MM:SS,并交换前两列的值,只有在找到负日期时,前两列才是同一行中的时间戳

日期正在从负数转换为正数,但对于例如,从-01:-05:-14,它将更改为凌晨1:05:14。首先,缺少“0”,并在末尾添加“AM”,而不是01:05:14

此外,交换时间戳的格式从2014年1月4日04:31:31 AM更改为2014年1月4日4:31:31 AM,初始“0”丢失。 请帮我解决这个问题?任何新的方法都是受欢迎的

我正在使用以下代码:

Dim row As Integer, col As Integer
Dim temp As Integer
Dim TimeText As String
Dim TestArray() As String 'Array to get the negative time, if any
temp = 0
Dim actualRow As Integer
Dim totalRows As Integer
Dim totalCols As Integer

Dim i As Integer
Dim tempSwap As String

totalRows = Application.CountA(Range("A:A"))
totalCols = Application.CountA(Range("1:1"))

For row = 2 To totalRows
actualRow = row - temp
For col = 2 To totalCols
TimeText = ActiveSheet.Cells(actualRow, col).Value

If Left(TimeText, 1) = "-" Then 'initial space and "-" sign

        TestArray() = Split(TimeText, "-")



        For i = 1 To UBound(TestArray)

          MsgBox (TestArray(i))

        Next

        ActiveSheet.Cells(actualRow, col).Value = TestArray(1) + TestArray(2) + TestArray(3) ' We know that there will be 3 array elements and TestArray(0)=""
    'SWAPPING THE COLUMN VALUES
    tempSwap = ActiveSheet.Cells(actualRow, col - 1).Value
    ActiveSheet.Cells(actualRow, col - 1).Value = ActiveSheet.Cells(actualRow, col - 2).Value
    ActiveSheet.Cells(actualRow, col - 2).Value = tempSwap

    temp = temp + 1

End If
Next col
Next row

如果您有如下单元格:

-01:-05:-14

选择它们并运行:

Sub FixTime()
    Dim r As Range
    For Each r In Selection
        With r
            v = Replace(Trim(.Text), "-", "")
            .Clear
            .NumberFormat = "hh:mm:ss"
            .Value = v
        End With
    Next r
End Sub

假设最终结果存储在名为ConvertedTime`的变量中,然后使用此格式ConvertedTime,HH:MM:SS,以确保excel工作表中的列也被格式化为时间HH:MM:SS