以增量方式复制/粘贴VBA

时间:2018-08-09 13:20:09

标签: excel vba excel-vba loops

我正在考虑为代码的以下部分创建一个循环:

InvestorMaterialDataCrypto001.Range(InvestorMaterialDataCrypto001.Cells(TableFirstRow, TableFirstColumn), InvestorMaterialDataCrypto001.Cells(TableLastRow_Crypto001, TableLastColumn)).Copy
    HistoricalSheet.Cells(InputReferenceRow_002, InputReferenceColumn + (CountColumns * 0)).PasteSpecial Paste:=xlPasteValues
InvestorMaterialDataCrypto002.Range(InvestorMaterialDataCrypto002.Cells(TableFirstRow, TableFirstColumn), InvestorMaterialDataCrypto002.Cells(TableLastRow_Crypto002, TableLastColumn)).Copy
    HistoricalSheet.Cells(InputReferenceRow_002, InputReferenceColumn + (CountColumns * 1)).PasteSpecial Paste:=xlPasteValues
InvestorMaterialDataCrypto003.Range(InvestorMaterialDataCrypto003.Cells(TableFirstRow, TableFirstColumn), InvestorMaterialDataCrypto003.Cells(TableLastRow_Crypto003, TableLastColumn)).Copy
    HistoricalSheet.Cells(InputReferenceRow_002, InputReferenceColumn + (CountColumns * 2)).PasteSpecial Paste:=xlPasteValues
InvestorMaterialDataCrypto004.Range(InvestorMaterialDataCrypto004.Cells(TableFirstRow, TableFirstColumn), InvestorMaterialDataCrypto004.Cells(TableLastRow_Crypto004, TableLastColumn)).Copy
    HistoricalSheet.Cells(InputReferenceRow_002, InputReferenceColumn + (CountColumns * 3)).PasteSpecial Paste:=xlPasteValues
InvestorMaterialDataCrypto005.Range(InvestorMaterialDataCrypto005.Cells(TableFirstRow, TableFirstColumn), InvestorMaterialDataCrypto005.Cells(TableLastRow_Crypto005, TableLastColumn)).Copy
    HistoricalSheet.Cells(InputReferenceRow_002, InputReferenceColumn + (CountColumns * 4)).PasteSpecial Paste:=xlPasteValues
InvestorMaterialDataCrypto006.Range(InvestorMaterialDataCrypto006.Cells(TableFirstRow, TableFirstColumn), InvestorMaterialDataCrypto006.Cells(TableLastRow_Crypto006, TableLastColumn)).Copy
    HistoricalSheet.Cells(InputReferenceRow_002, InputReferenceColumn + (CountColumns * 5)).PasteSpecial Paste:=xlPasteValues 

问题是我找不到增加“工作表”名称/变量和(CountColumns * 1)部分的解决方案。

有助于进一步优化此代码...

总代码为:

Dim FilePath As String: FilePath = InvestorMaterialDataFilePathParsed
Dim InvestorMaterialData As Workbook
Set InvestorMaterialData = Workbooks.Open(Filename:=FilePath)
Set InvestorMaterialDataDashboardSheet = InvestorMaterialData.Worksheets("Dashboard")
Set InvestorMaterialDataTop30Sheet = InvestorMaterialData.Worksheets("Top30")
Set InvestorMaterialDataCrypto001 = InvestorMaterialData.Worksheets("CryptoAsset001")
Set InvestorMaterialDataCrypto002 = InvestorMaterialData.Worksheets("CryptoAsset002")
Set InvestorMaterialDataCrypto003 = InvestorMaterialData.Worksheets("CryptoAsset003")
Set InvestorMaterialDataCrypto004 = InvestorMaterialData.Worksheets("CryptoAsset004")
...
TableFirstColumn = InvestorMaterialDataCrypto001.Range("A1").Column
TableLastColumn = InvestorMaterialDataCrypto001.Range("A1").End(xlToRight).Column
TableFirstRow = InvestorMaterialDataCrypto001.Range("A1").Row
TableLastRow_Crypto001 = InvestorMaterialDataCrypto001.Range("A1").End(xlDown).Row
TableLastRow_Crypto002 = InvestorMaterialDataCrypto002.Range("A1").End(xlDown).Row
TableLastRow_Crypto003 = InvestorMaterialDataCrypto003.Range("A1").End(xlDown).Row
TableLastRow_Crypto004 = InvestorMaterialDataCrypto004.Range("A1").End(xlDown).Row


and the top part of the loop...

1 个答案:

答案 0 :(得分:1)

没有完整阅读您的问题/代码。但是,由于三个小时后您没有得到任何回应,因此我敢于发布可能有用的提示(如果我误解了您的帖子,则完全错误)。复制到至少包含三张纸的新工作簿(“ Sheet1”,“ Sheet2”和“ Sheet3”),然后尝试一下。

Sub Trial()
    Dim i As Integer
    Dim TempName1 As String
    Dim TempName2 As String

    For i = 1 To 3
        'TempName1 = "InvestorMaterialDataCrypto00" & i
        TempName1 = "Sheet" & i
        TempName2 = "TableLastRow_Crypto00" & i

        'here do your stuff. Next line just as an example
        Worksheets(TempName1).Cells(i, 1) = TempName2
    Next

End Sub