首页 > 解决方案 > 使用时间格式和一般格式连接/组合列

问题描述

我被一个问题困住了,找不到解决方案。我想用两种不同的格式连接单元格。一个单元格的格式为“tt:mm”,另一个单元格的格式为“general”我将如何连接这些单元格?我在网上看到这可以在 Exel 中用类似的东西完成

=A1 & TEXT(B1,"tt:mm")

或 =CONCATENATE(TEXT(A1;"tt:mm"); " ";B1)

但是这是如何通过循环在 VBA 中完成的呢?

我试图将单元格添加在一起,但我没有以我想要的格式返回日期。

Sub  Merge()
Dim i As Long
Dim time As String
LastRow = Range("A" & StartRow).End(xlDown).Row

For i = StartRow To LastRow
Range("B" & i).Value = Range("B" & i).Value & " " & Range("A" & i).Value
Range("B" & i).NumberFormat = "tt:mm General" 
End Sub    

因此,如果我为 xyz 和 02:30 运行此代码,那么我得到 0,15625 xyz 但我想得到 02:30 xyz。

非常感谢您的帮助。

标签: excelvba

解决方案


制作这一行: Range("B" & i).Value = Range("B" & i).Value & " " & Range("A" & i).Value

进入: Range("B" & i).Value = format(Range("B" & i).Value, "hh:mm") & " " & Range("A" & i).Value

此链接具有不同的格式字符串:https ://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/format-function-visual-basic-for-applications


推荐阅读