这是我的第一个宏,我需要一些帮助。我不断更改工作表1中的变量,并在工作表2中运行另一个宏来获取我的结果。所以这是一个敏感性测试,我正在编写以下宏来运行已经存在的marco。它生成的某些行似乎是正确的,但其中一些不是。我无法弄清楚出了什么问题。任何提示都表示赞赏。
Sub SensitivityTest()
For i = 8 To 11
Range("G" & i + 1).Select
Selection.Copy
Range("D10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D15").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Call AnotherMacro
Range("Q76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("H" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AD76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("I" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("Q20").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("J" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AD20").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("K" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("Q27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("L" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AD27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("M" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("Q28").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("N" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AD28").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("O" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("V76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("Q" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AI76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("R" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("V20").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("S" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AI20").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("T" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("V27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("U" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AI27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("V" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("V28").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("W" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AI28").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("X" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next i
End Sub
答案 0 :(得分:2)
跟进@ bruceWayne的评论:
当前的复制/粘贴操作:
Sheets("Sheet2").Select
Range("AD76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("I" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
不选择/激活:
Sheets("Sheet2").Range("AD76").Copy
Sheets("Sheet1").Range("I" & i + 1).PasteSpecial Paste:=xlPasteValues
答案 1 :(得分:1)
因为我在吃午饭时感到无聊,所以我决定重写代码以查看在删除所有.Select
,Selection.
位(以及其他一些位)后会减少多少整理)。我想出了这个:
Sub SensitivityTest()
With Sheets("Sheet1")
For i = 8 To 11
.Range("D10").Value = .Range("G" & i + 1).Value
.Range("D15").Value = .Range("G" & i + 1).Value
'This next line shouldn't be required if "AnotherMacro" was suitably changed
'to fully qualify all ranges, etc, being referred to
Sheets("Sheet2").Select
Call AnotherMacro
'Because the original code was pasting values, I have changed the
'code to just set the destination cell's Value equal to the
'source cell's Value. This avoids using the clipboard, which
'often leads to problems if the user is doing something else
'while a macro is running.
.Range("H" & i + 1).Value = Sheets("Sheet2").Range("Q76").Value
.Range("I" & i + 1).Value = Sheets("Sheet2").Range("AD76").Value
.Range("J" & i + 1).Value = Sheets("Sheet2").Range("Q20").Value
.Range("K" & i + 1).Value = Sheets("Sheet2").Range("AD20").Value
.Range("L" & i + 1).Value = Sheets("Sheet2").Range("Q27").Value
.Range("M" & i + 1).Value = Sheets("Sheet2").Range("AD27").Value
.Range("N" & i + 1).Value = Sheets("Sheet2").Range("Q28").Value
.Range("O" & i + 1).Value = Sheets("Sheet2").Range("AD28").Value
.Range("Q" & i + 1).Value = Sheets("Sheet2").Range("V76").Value
.Range("R" & i + 1).Value = Sheets("Sheet2").Range("AI76").Value
.Range("S" & i + 1).Value = Sheets("Sheet2").Range("V20").Value
.Range("T" & i + 1).Value = Sheets("Sheet2").Range("AI20").Value
.Range("U" & i + 1).Value = Sheets("Sheet2").Range("V27").Value
.Range("V" & i + 1).Value = Sheets("Sheet2").Range("AI27").Value
.Range("W" & i + 1).Value = Sheets("Sheet2").Range("V28").Value
.Range("X" & i + 1).Value = Sheets("Sheet2").Range("AI28").Value
Next i
'Include a final select of Sheet1, just to get around the effect of
'doing the Select of Sheet2 during the macro. This wouldn't be
'needed if AnotherMacro was similarly tidied up to not require
'Sheet2 to be Selected before running.
.Select
End With
End Sub
我发现很多更容易阅读,因此在必要时进行维护和调试会更容易。
P.S。如果循环从i + 1
更改为i
,则所有For i = 8 To 11
语句都可以更改为For i = 9 To 12
。
P.P.S。我猜你的代码有时候工作的原因有时并没有,当你调用宏时,你的代码依赖于Sheet1
作为活动工作表。如果Sheet2
处于活动状态,那么几乎肯定不会按照您的意愿行事。
答案 2 :(得分:0)
谢谢大家的帮助!当我在星期五运行以下代码时,它停留在最后几行,并且相同的结果不断重复。但是,当我让它在下班后运行而不是在计算机上做其他事情时,它就有效了!
Sub SensitivityTest()
With Sheets("Sheet1")
For i = 9 To 40
.Range("D10").value = .Range("G" & i).value
.Range("D15").value = .Range("G" & i).value
Call AnotherMacro
.Range("H" & i).value = Sheets("Sheet2").Range("Q76").value
.Range("I" & i).value = Sheets("Sheet2").Range("AD76").value
.Range("J" & i).value = Sheets("Sheet2").Range("Q20").value
.Range("K" & i).value = Sheets("Sheet2").Range("AD20").value
.Range("L" & i).value = Sheets("Sheet2").Range("Q23").value
.Range("M" & i).value = Sheets("Sheet2").Range("AD23").value
.Range("N" & i).value = Sheets("Sheet2").Range("Q28").value
.Range("O" & i).value = Sheets("Sheet2").Range("AD28").value
.Range("Q" & i).value = Sheets("Sheet2").Range("V76").value
.Range("R" & i).value = Sheets("Sheet2").Range("AI76").value
.Range("S" & i).value = Sheets("Sheet2").Range("V20").value
.Range("T" & i).value = Sheets("Sheet2").Range("AI20").value
.Range("U" & i).value = Sheets("Sheet2").Range("V23").value
.Range("V" & i).value = Sheets("Sheet2").Range("AI23").value
.Range("W" & i).value = Sheets("Sheet2").Range("V28").value
.Range("X" & i).value = Sheets("Sheet2").Range("AI28").value
Next i
End With
End Sub