使用相对参考

时间:2018-11-16 09:34:25

标签: excel vba performance loops simplify

作为VBA的新手,我希望对此代码进行一些输入,以提高其速度...目前感觉不那么“ VBA” -ish;但是代码的“结果”是正确的...

Sub Rigtig()

Set Marketshare = Sheets("Output").Range("p40:p50")
'Select.
Sheets("Output").Select
    Cells(38, 17).Copy
Sheets("Input").Select
   Cells(33, 28).Select
    Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
    Cells(23, 28).Select
    Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
    Cells(40, 17).Copy
    Cells(40, 17).Select
    Selection.PasteSpecial Paste:=xlPasteValues
Marketshare.Cells(2, 1).Copy
Sheets("Input").Select
    Cells(23, 28).Select
    Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
    Cells(41, 17).Copy
    Cells(41, 17).Select
    Selection.PasteSpecial Paste:=xlPasteValues
    Marketshare.Cells(3, 1).Copy
Sheets("Input").Select
    Cells(23, 28).Select
    Selection.PasteSpecial Paste:=xlPasteValues


Sheets("Output").Select
    Cells(38, 18).Copy
Sheets("Input").Select
   Cells(33, 28).Select
    Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
    Cells(23, 28).Select
    Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
    Cells(40, 18).Copy
    Cells(40, 18).Select
    Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
    Cells(38, 19).Copy
Sheets("Input").Select
   Cells(33, 28).Select
    Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Output").Select
Marketshare.Cells(1, 1).Copy
Sheets("Input").Select
    Cells(23, 28).Select
    Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Output").Select
    Cells(40, 19).Copy
    Cells(40, 19).Select
    Selection.PasteSpecial Paste:=xlPasteValues
Marketshare.Cells(2, 1).Copy
Sheets("Input").Select
    Cells(23, 28).Select
    Selection.PasteSpecial Paste:=xlPasteValues

我想在行中进行大约10次相同的“复制粘贴”,然后更改列。

预先感谢

最佳

Valdemar

2 个答案:

答案 0 :(得分:1)

就像@Glitch_Doctor所说的-如果只是您想要的值,则可以执行“此单元格=该单元格”而不是复制/粘贴。

要缩短代码并使之更加“ VBA”,可以将单元格引用对放入数组中并逐步遍历该数组:

Sub Test()

    Dim vAddresses As Variant
    Dim vRef As Variant

    vAddresses = Array( _
        Array("Q38", "AB33"), _
        Array("A1", "AB23"))

    For Each vRef In vAddresses
        Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
    Next vRef

End Sub  

您还可以使用With...End With块,这样就不必每次都使用工作表名称:

Sub Test1()

    With Worksheets("Output")
        Worksheets("Input").Cells(33, 28) = .Cells(38, 17)
    End With

End Sub 

如果要复制所有内容(公式,格式),请在一行中使用“复制并粘贴”:

Sub Test2()

    With Worksheets("Output")
        Worksheets("Input").Cells(33, 28).Copy Destination:=.Cells(38, 17)
    End With

End Sub

答案 1 :(得分:0)

所以我希望它是这样的:

    Sub Test()

    Dim vAddresses As Variant
    Dim vRef As Variant

    vAddresses = Array( _
        Array("Q38", "AB33"), _
        Array("P40", "AB23"))

For Each vRef In vAddresses
        Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
    Next vRef

Sheets("Output").Cells(40, 17).Value = Sheets("Output").Cells(40, 17).Value

 vAddresses = Array( _
        Array("Q38", "AB33"), _
        Array("P41", "AB23"))
For Each vRef In vAddresses
        Worksheets("Input").Range(vRef(1)) = Worksheets("Output").Range(vRef(0))
    Next vRef

Sheets("Output").Cells(41, 17).Value = Sheets("Output").Cells(41, 17).Value

这有意义吗?

因此将其向下重复10倍,然后向左移动1列并再次执行。

希望你明白我的意思,谢谢:)

/ Valdemar