将多个值插入多个不同的单元格

时间:2018-10-23 11:36:50

标签: excel vba excel-vba performance cell

我有以下代码,用相关值更新相关单元格。即使只有11个,我也想知道是否还有更好的方法,以帮助加快代码的速度,以及万一需要更新000个单元格的情况。

Range("E1").Value = "Export Date"
Range("G1").Value = "Amended Start Date"
Range("H1").Value = "Ticket Age (Working Days)"
Range("J1").Value = "Overdue (1=Yes, 0=No)"
Range("U1").Value = "TicketEntity1"
Range("V1").Value = "TicketEntity2"
Range("W1").Value = "TicketEntity3"
Range("X1").Value = "TicketEntity4"
Range("Y1").Value = "TicketEntity5"
Range("Z1").Value = "TicketEntity6"

2 个答案:

答案 0 :(得分:1)

您可以使用数组将其收紧以覆盖相邻的单元格。

Range("E1").Value = "Export Date"
Range("G1:H1").Value = array("Amended Start Date","Ticket Age (Working Days)")
Range("J1").Value = "Overdue (1=Yes, 0=No)"
Range("U1:Z1").Value = array("TicketEntity1","TicketEntity2","TicketEntity3", _
                             "TicketEntity4","TicketEntity5","TicketEntity6")

TicketEntryx 系列也可以使用增量作为后缀标识符进行循环。

答案 1 :(得分:1)

Sub Test()

Dim CurrentColumn As Long
Dim i As Long

CurrentColumn = 20

With wsTest
    For i = 1 To 5 '<= Use the correct number according your requirements
        .Cells(1, CurrentColumn + 1).Value = "TicketEntity" & i
        CurrentColumn = CurrentColumn + 1
    Next i
End With

End Sub