VBA连接/使用格式需要太长时间

问题描述 投票:0回答:2

我想遍历范围中的列并连接日期和数字以生成ID类型。问题是:包含日期的单元格还包括时间,现在我使用Format(datum.Cells(1, 1), "dd/mm/yyyy"),但这需要很长时间才能处理。有没有办法加快速度?

For Each datum In Range(rngDestination.Cells(1, 14), 
 rngDestination.Cells(lastRow, 14))
       If Not datum.Value = "" Then
         datum.Cells(1, 10).Value = Format(datum.Cells(1, 1), "dd/mm/yyyy")
       End If
Next datum
 For Each kette In Range(rngDestination.Cells(1, 1), 
 rngDestination.Cells(lastRow, 1))
  kette.Cells(1, 0).Value = kette.Cells(1, 23).text& & kette.Cells(1, 5).text
Next kette
excel vba loops format
2个回答
1
投票

使用数组和类型函数?没有测试过。

 Dim rng As Range, arr(), i As Long
 Set rng = Range(rngDestination.Cells(1, 14), rngDestination.Cells(lastRow, 14))

 arr = rng.Value
 For i = LBound(arr, 1) To UBound(arr, 1)
   If Not IsEmpty(arr(i, 1)) Then
       arr(i, 1) = Format$(arr(i, 1), "dd/mm/yyyy")
   End If
 Next

 rng = Application.WorksheetFunction.Transpose(Application.WorksheetFunction.Index(arr, 0, 1))

1
投票

尝试在阵列中立即将它们放在一起,然后将值转储回来。

dim arr as variant, tmp as variant, i as long

with rngDestination
    arr = .Range(rngDestination.Cells(1, 14),  rngDestination.Cells(lastRow, 14)).value2
    tmp = .Range(rngDestination.Cells(1, 5),  rngDestination.Cells(lastRow, 5)).value2
    for i =lbound(arr, 1) to ubound(arr, 1)
        if cbool(len(arr(i, 1))) then
            arr(i, 1) = int(arr(i, 1))
        else
            arr(i, 1) = vbnullstring
        end if
    next i
    .Cells(1, 23).resize(ubound(arr, 1), ubound(arr, 1)) = arr
    .Cells(1, 23).resize(ubound(arr, 1), ubound(arr, 1)).numberformat = "dd/mm/yyyy"
    for i =lbound(arr, 1) to ubound(arr, 1)
        arr(i, 1) = format(arr(i, 1), "dd/mm/yyyy") & tmp(i, 1)
    next i
    .Cells(1, 0).resize(ubound(arr, 1), ubound(arr, 1)) = arr
end with
© www.soinside.com 2019 - 2024. All rights reserved.