循环的VBA查询
我是新来的VBA /自我教学,我已经做了下面的代码,但你可以看到它是可怕的和漫长的,我已经尝试了没有成为一个循环改变“K1”的值和“M7 “下面的代码(它的长和手册)基本上只是在”Lu.k1“中改变和设置因子,并将修改后的值在教师.s30中复制到regional.m7重复将LU.k1更改为2个副本并粘贴教练员。进入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
尝试一个循环来减less多余的代码。
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