我是VBA /自我教学的新手,我已经制作了下面的代码,但你可以看到它很可怕而且很长,我已经尝试过没有成功进入一个循环来改变“K1”中的值和值在“M7”中,下面的代码(它的长和手动)基本上只是改变并设置因子在“Lu.k1”中并将trainers.s30中的结果更改值复制到regional.m7重复将LU.k1更改为2份并粘贴trainers.s30进入regional.m8等直到lu.k1 = 8
任何帮助都会很棒
Application.ScreenUpdating = False
Sheets("LU").Select
Range("E1").Select
ActiveCell.FormulaR1C1 = "4"
Range("I1").Select
ActiveCell.FormulaR1C1 = "2"
Range("J1").Select
ActiveCell.FormulaR1C1 = "3"
Range("K1").Select
ActiveCell.FormulaR1C1 = "1"
Sheets("Trainers").Select
Range("S30").Select
Selection.Copy
Sheets("Regional").Select
Range("M7").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
'next change
Sheets("LU").Select
Range("K1").Select
ActiveCell.FormulaR1C1 = "2"
Sheets("Trainers").Select
Range("S30").Select
Selection.Copy
Sheets("Regional").Select
Range("M8").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
Sheets("LU").Select
Range("K1").Select
ActiveCell.FormulaR1C1 = "3"
Sheets("Trainers").Select
Range("S30").Select
Selection.Copy
Sheets("Regional").Select
Range("M9").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
Sheets("LU").Select
Range("K1").Select
ActiveCell.FormulaR1C1 = "4"
Sheets("Trainers").Select
Range("S30").Select
Selection.Copy
Sheets("Regional").Select
Range("M10").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
Sheets("LU").Select
Range("K1").Select
ActiveCell.FormulaR1C1 = "5"
Sheets("Trainers").Select
Range("S30").Select
Selection.Copy
Sheets("Regional").Select
Range("M11").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
Sheets("LU").Select
Range("K1").Select
ActiveCell.FormulaR1C1 = "6"
Sheets("Trainers").Select
Range("S30").Select
Selection.Copy
Sheets("Regional").Select
Range("M12").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
答案 0 :(得分:1)
尝试使用循环来减少冗余代码。
Sub erqwtrw()
Dim wsLU As Worksheet, wsTRN As Worksheet, i As Long, mx As Long
Set wsLU = ActiveWorkbook.Worksheets("lu")
Set wsTRN = ActiveWorkbook.Worksheets("trainers")
mx = 8
wsLU.Range("E1") = 4
With Worksheets("regional")
.Range("M7").Resize(mx, 1).NumberFormat = wsTRN.Range("S30").NumberFormat
For i = 1 To mx
wsLU.Range("I1").Resize(1, 3) = Array(2, 3, i)
.Range("M6").Offset(i, 0) = wsTRN.Range("S30").Value
Next i
End With
End Sub