Excel VBA缩短代码以将值从一个范围复制到另一个范围

时间:2019-03-30 22:35:52

标签: excel vba

我已经获得了包含以下代码的Excel工作簿,并且想知道如何缩短它以使其易于管理。该代码重复地将值从一个命名范围内的某些单元格复制到另一命名范围内的某些单元格,并复制字体和填充颜色。代码比这更长(遵循相同的模式,命名范围增加1),所以我确定有更好的编写方法。

Private Sub Copy_Jobs()

Worksheets("Sales").Range("Week1")(1).Cells.Value = Worksheets("Tasks").Range("Job1")(1).Cells.Value
Worksheets("Sales").Range("Week1")(2).Cells.Value = Worksheets("Tasks").Range("Job1")(2).Cells.Value
Worksheets("Sales").Range("Week1")(3).Cells.Value = Worksheets("Tasks").Range("Job1")(3).Cells.Value
Worksheets("Sales").Range("Week1")(4).Cells.Value = Worksheets("Tasks").Range("Job1")(5).Cells.Value
Worksheets("Sales").Range("Week1").Font.Color = Worksheets("Tasks").Range("Job1").Font.Color
Worksheets("Sales").Range("Week1").Interior.Color = Worksheets("Tasks").Range("Job1").Interior.Color

Worksheets("Sales").Range("Week2")(1).Cells.Value = Worksheets("Tasks").Range("Job2")(1).Cells.Value
Worksheets("Sales").Range("Week2")(2).Cells.Value = Worksheets("Tasks").Range("Job2")(2).Cells.Value
Worksheets("Sales").Range("Week2")(3).Cells.Value = Worksheets("Tasks").Range("Job2")(3).Cells.Value
Worksheets("Sales").Range("Week2")(4).Cells.Value = Worksheets("Tasks").Range("Job2")(5).Cells.Value
Worksheets("Sales").Range("Week2").Font.Color = Worksheets("Tasks").Range("Job2").Font.Color
Worksheets("Sales").Range("Week2").Interior.Color = Worksheets("Tasks").Range("Job2").Interior.Color

End Sub

1 个答案:

答案 0 :(得分:0)

将通用代码拆分为单独的子...

export class Circle {
  C: number;
  r: number;
}

请注意,只有在范围设置一致的情况下,才能真正复制字体和内部颜色。