简体   繁体   English

如何让 VBA 复制一系列单元格,等待单元格计算并粘贴到另一个范围?

[英]How do I get VBA to Copy a range of cells, WAIT FOR THE CELLS TO CALCULATE and paste in another range?

This code will go into the sheet and switch out a cell to a certain function that is linked to the range I am going to copy.此代码将 go 放入工作表并将单元格切换到链接到我要复制的范围的某个 function。 Then it will pastevalues on another sheet in a specific cell.然后它会将值粘贴到特定单元格中的另一张纸上。 I change out ActiveCell (Line 6) with each copy and paste.每次复制和粘贴时,我都会更改 ActiveCell(第 6 行)。 This code isn't waiting for the cells that will be copied to calculate.此代码不等待将要复制的单元格进行计算。 Therefore I have the same cell values in my whole worksheet.因此,我的整个工作表中都有相同的单元格值。 Any help would be great:) I tried the "Application.Calculate" and that didn't work.任何帮助都会很棒 :) 我尝试了“Application.Calculate”但没有用。 This code goes on to copy and paste 100 different tickers for stocks I included five series of codes, but they keep going on to record each stock price.这段代码继续复制并粘贴 100 个不同的股票代码,我包括五个系列的代码,但它们继续记录每个股票价格。

Sheets("Investing").Select
    ActiveWindow.SmallScroll Down:=21
    Sheets("Homepage").Select
    Range("J2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=Investing!R[268]C[-9]"
    Range("J3").Select
    Sheets("Investing").Select
    ActiveWindow.SmallScroll Down:=-12
    Range("A249:B260").Select
    Selection.Copy
    Sheets("Daily Strategies").Select
    Range("E5").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Homepage").Select
    Range("J2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=Investing!R[269]C[-9]"
    Range("J3").Select
    Sheets("Investing").Select
    Range("A249:B260").Select
    Selection.Copy
    Range("D283").Select
    Sheets("Daily Strategies").Select
    Range("G5").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Homepage").Select
    Range("J2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=Investing!R[270]C[-9]"
    Range("J3").Select
    Sheets("Investing").Select
    Range("A249:B260").Select
    Selection.Copy
    Sheets("Daily Strategies").Select
    Range("I5").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Homepage").Select
    Range("J2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=Investing!R[271]C[-9]"
    Range("J3").Select
    Sheets("Investing").Select
    Range("A249:B260").Select
    Selection.Copy
    Sheets("Daily Strategies").Select
    Range("K5").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Homepage").Select
    Range("J2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=Investing!R[272]C[-9]"
    Range("J3").Select
    Sheets("Investing").Select
    Range("A249:B260").Select
    Selection.Copy
    Sheets("Daily Strategies").Select
    Range("M5").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

I

A Workbook Calculation工作簿计算

  • Copy the code into a standard module (eg Module1 ).将代码复制到标准模块(例如Module1 )。
  • Adjust the const ants including the workbook .调整包括工作簿在内的常量
  • If it doesn't work, experiment with the out-commented lines containing Calculate one by one.如果它不起作用,请逐一试验包含Calculate的注释掉的行。

The Code代码

Option Explicit

Sub insertVarious()

    'Application.CalculateFullRebuild

    Const hpgName As String = "Homepage"
    Const hpgCell As String = "J2"

    Const invName As String = "Investing"
    Const invAddr As String = "A249:B260"
    Const invAddr2 As String = "A270:A371"

    Const dstName As String = "Daily Strategies"
    Const dstFirst As String = "E5"

    Dim wb As Workbook: Set wb = ThisWorkbook

    Dim hpg As Range: Set hpg = wb.Worksheets(hpgName).Range(hpgCell)
    Dim inv As Range: Set inv = wb.Worksheets(invName).Range(invAddr)
    Dim inv2 As Range: Set inv2 = wb.Worksheets(invName).Range(invAddr2)
    Dim UB1 As Long: UB1 = inv.Rows.Count
    Dim UB2 As Long: UB2 = inv.Columns.Count
    Dim NoA As Long: NoA = inv2.Rows.Count

    Dim Daily As Variant: ReDim Daily(1 To UB1, 1 To NoA * UB2)
    Dim Curr As Variant, j As Long, k As Long, l As Long
    For j = 1 To NoA
        hpg.Value = inv2.Cells(j).Value
        'hpg.Parent.Calculate
        'inv.Parent.Calculate
        Curr = inv.Value
        GoSub writeDaily
    Next j

    wb.Worksheets(dstName).Range(dstFirst).Resize(UB1, NoA * UB2) = Daily

    MsgBox "Data transferred.", vbInformation, "Success"

    Exit Sub

writeDaily:
    For k = 1 To UB1
        For l = 1 To UB2
            Daily(k, (j - 1) * 2 + l) = Curr(k, l)
        Next l
    Next k
    Return

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM