我如何缩短这段代码?

时间:2017-07-05 17:15:36

标签: excel-vba vba excel

我有一个启动值,开头有字母,最后有数字,我需要添加82次数字。如果你能告诉我缩短这个的可能方法,我将不胜感激。

'With Sheets("License Plate Number")
.Range("A3") = mp_start
.Range("A4") = Left(mp_start, 2) & Right(mp_start, 12) + 1
.Range("A5") = Left(mp_start, 2) & Right(mp_start, 12) + 2
.Range("A6") = Left(mp_start, 2) & Right(mp_start, 12) + 3
.Range("A7") = Left(mp_start, 2) & Right(mp_start, 12) + 4
.Range("A8") = Left(mp_start, 2) & Right(mp_start, 12) + 5
.Range("A9") = Left(mp_start, 2) & Right(mp_start, 12) + 6
.Range("A10") = Left(mp_start, 2) & Right(mp_start, 12) + 7
.Range("A11") = Left(mp_start, 2) & Right(mp_start, 12) + 8
.Range("A12") = Left(mp_start, 2) & Right(mp_start, 12) + 9
.Range("A13") = Left(mp_start, 2) & Right(mp_start, 12) + 10
.Range("A14") = Left(mp_start, 2) & Right(mp_start, 12) + 11
.Range("A15") = Left(mp_start, 2) & Right(mp_start, 12) + 12
.Range("A16") = Left(mp_start, 2) & Right(mp_start, 12) + 13
.Range("A17") = Left(mp_start, 2) & Right(mp_start, 12) + 14
.Range("A18") = Left(mp_start, 2) & Right(mp_start, 12) + 15
.Range("A19") = Left(mp_start, 2) & Right(mp_start, 12) + 16
.Range("A20") = Left(mp_start, 2) & Right(mp_start, 12) + 17
.Range("A21") = Left(mp_start, 2) & Right(mp_start, 12) + 18
.Range("A22") = Left(mp_start, 2) & Right(mp_start, 12) + 19
.Range("A23") = Left(mp_start, 2) & Right(mp_start, 12) + 20
.Range("A24") = Left(mp_start, 2) & Right(mp_start, 12) + 21
.Range("A25") = Left(mp_start, 2) & Right(mp_start, 12) + 22
.Range("A26") = Left(mp_start, 2) & Right(mp_start, 12) + 23
.Range("A27") = Left(mp_start, 2) & Right(mp_start, 12) + 24
.Range("A28") = Left(mp_start, 2) & Right(mp_start, 12) + 25
.Range("A29") = Left(mp_start, 2) & Right(mp_start, 12) + 26
.Range("A30") = Left(mp_start, 2) & Right(mp_start, 12) + 27
.Range("A31") = Left(mp_start, 2) & Right(mp_start, 12) + 28
.Range("A32") = Left(mp_start, 2) & Right(mp_start, 12) + 29
.Range("A33") = Left(mp_start, 2) & Right(mp_start, 12) + 30
.Range("A34") = Left(mp_start, 2) & Right(mp_start, 12) + 31
.Range("A35") = Left(mp_start, 2) & Right(mp_start, 12) + 32
.Range("A36") = Left(mp_start, 2) & Right(mp_start, 12) + 33
.Range("A37") = Left(mp_start, 2) & Right(mp_start, 12) + 34
.Range("A38") = Left(mp_start, 2) & Right(mp_start, 12) + 35
.Range("A39") = Left(mp_start, 2) & Right(mp_start, 12) + 36
.Range("A40") = Left(mp_start, 2) & Right(mp_start, 12) + 37
.Range("A41") = Left(mp_start, 2) & Right(mp_start, 12) + 38
.Range("A42") = Left(mp_start, 2) & Right(mp_start, 12) + 39
.Range("A43") = Left(mp_start, 2) & Right(mp_start, 12) + 40
.Range("A44") = Left(mp_start, 2) & Right(mp_start, 12) + 41
.Range("A45") = Left(mp_start, 2) & Right(mp_start, 12) + 42
.Range("A46") = Left(mp_start, 2) & Right(mp_start, 12) + 43
.Range("A47") = Left(mp_start, 2) & Right(mp_start, 12) + 44
.Range("A48") = Left(mp_start, 2) & Right(mp_start, 12) + 45
.Range("A49") = Left(mp_start, 2) & Right(mp_start, 12) + 46
.Range("A50") = Left(mp_start, 2) & Right(mp_start, 12) + 47
.Range("A51") = Left(mp_start, 2) & Right(mp_start, 12) + 48
.Range("A52") = Left(mp_start, 2) & Right(mp_start, 12) + 49
.Range("A53") = Left(mp_start, 2) & Right(mp_start, 12) + 50
.Range("A54") = Left(mp_start, 2) & Right(mp_start, 12) + 51
.Range("A55") = Left(mp_start, 2) & Right(mp_start, 12) + 52
.Range("A56") = Left(mp_start, 2) & Right(mp_start, 12) + 53
.Range("A57") = Left(mp_start, 2) & Right(mp_start, 12) + 54
.Range("A58") = Left(mp_start, 2) & Right(mp_start, 12) + 55
.Range("A59") = Left(mp_start, 2) & Right(mp_start, 12) + 56
.Range("A60") = Left(mp_start, 2) & Right(mp_start, 12) + 57
.Range("A61") = Left(mp_start, 2) & Right(mp_start, 12) + 58
.Range("A62") = Left(mp_start, 2) & Right(mp_start, 12) + 59
.Range("A63") = Left(mp_start, 2) & Right(mp_start, 12) + 60
.Range("A64") = Left(mp_start, 2) & Right(mp_start, 12) + 61
.Range("A65") = Left(mp_start, 2) & Right(mp_start, 12) + 62
.Range("A66") = Left(mp_start, 2) & Right(mp_start, 12) + 63
.Range("A67") = Left(mp_start, 2) & Right(mp_start, 12) + 64
.Range("A68") = Left(mp_start, 2) & Right(mp_start, 12) + 65
.Range("A69") = Left(mp_start, 2) & Right(mp_start, 12) + 66
.Range("A70") = Left(mp_start, 2) & Right(mp_start, 12) + 67
.Range("A71") = Left(mp_start, 2) & Right(mp_start, 12) + 68
.Range("A72") = Left(mp_start, 2) & Right(mp_start, 12) + 69
.Range("A73") = Left(mp_start, 2) & Right(mp_start, 12) + 70
.Range("A74") = Left(mp_start, 2) & Right(mp_start, 12) + 71
.Range("A75") = Left(mp_start, 2) & Right(mp_start, 12) + 72
.Range("A76") = Left(mp_start, 2) & Right(mp_start, 12) + 73
.Range("A77") = Left(mp_start, 2) & Right(mp_start, 12) + 74
.Range("A78") = Left(mp_start, 2) & Right(mp_start, 12) + 75
.Range("A79") = Left(mp_start, 2) & Right(mp_start, 12) + 76
.Range("A80") = Left(mp_start, 2) & Right(mp_start, 12) + 77
.Range("A81") = Left(mp_start, 2) & Right(mp_start, 12) + 78
.Range("A82") = Left(mp_start, 2) & Right(mp_start, 12) + 79
.Range("A83") = Left(mp_start, 2) & Right(mp_start, 12) + 80
.Range("A84") = Left(mp_start, 2) & Right(mp_start, 12) + 81      
End With'

如果你能告诉我缩短这个的可能方法,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

使用For循环:

.Range("A3") = mp_start
For i = 4 To 84
    .Range("A" & i) = Left(mp_start, 2) & Right(mp_start, 12) + i - 3
Next i