简体   繁体   English

Excel VBA - 将一行单元格值传递给数组,然后将该数组粘贴到单元格的相对引用

[英]Excel VBA - Pass a Row of Cell Values to an Array and then Paste that Array to a Relative Reference of Cells

Using Excel (2010) VBA, I am trying to copy (pass) a constant range of cells (whose values recalculate) to an array. 使用Excel(2010)VBA,我试图复制(传递)一个恒定范围的单元格(其值重新计算)到一个数组。 Then I am trying to pass that array to a new range of cells, directly below it. 然后我试图将该数组传递给一个新的单元格范围,直接在它下面。 After I have done this, I want to again copy (pass) the constant range's new values to the array, and pass these new values to a range directly below the one I previously passed. 完成此操作后,我想再次将常量范围的新值复制(传递)到数组中,并将这些新值传递到我之前传递的范围的正下方。

I know this code is atrocious (I am new to arrays in VBA). 我知道这段代码非常糟糕(我是VBA中的数组新手)。

Sub ARRAYER()

Dim anARRAY(5) As Variant

Number_of_Sims = 10

For i = 1 To Number_of_Sims
   anARRAY = Range("C4:G4")
   Range("C4").Select
   ActiveCell.Offset(Number_of_Sims, 0).Select
   ActiveCell = anARRAY
   Range("C4").Select
Next

End Sub

I sure do appreciate your help! 我确实感谢你的帮助!

Thank you. 谢谢。

Respectfully, 尊敬,

Jonathan 乔纳森

You are off slightly on a few things here, so hopefully the following helps. 你在这里稍微关注一些事情,所以希望以下有所帮助。

Firstly, you don't need to select ranges to access their properties, you can just specify their address etc. Secondly, unless you are manipulating the values within the range, you don't actually need to set them to a variant. 首先,您不需要选择范围来访问它们的属性,您可以只指定它们的地址等。其次,除非您操作范围内的值,否则实际上不需要将它们设置为变量。 If you do want to manipulate the values, you can leave out the bounds of the array as it will be set when you define the range. 如果您确实想要操作值,则可以省略数组的边界,因为在定义范围时将设置它。

It's also good practice to use Option Explicit at the top of your modules to force variable declaration. 在模块顶部使用Option Explicit强制变量声明也是一种好习惯。

The following will do what you are after: 以下将完成您的工作:

Sub ARRAYER()
    Dim Number_of_Sims As Integer, i As Integer

    Number_of_Sims = 10

    For i = 1 To Number_of_Sims
       'Do your calculation here to update C4 to G4
       Range(Cells(4 + i, "C"), Cells(4 + i, "G")).Value = Range("C4:G4").Value
    Next
End Sub

If you do want to manipulate the values within the array then do this: 如果您确实想要操作数组中的值,请执行以下操作:

Sub ARRAYER()
    Dim Number_of_Sims As Integer, i As Integer
    Dim anARRAY as Variant

    Number_of_Sims = 10

    For i = 1 To Number_of_Sims
       'Do your calculation here to update C4 to G4
       anARRAY= Range("C4:G4").Value

       'You can loop through the array and manipulate it here

       Range(Cells(4 + i, "C"), Cells(4 + i, "G")).Value = anARRAY
    Next
End Sub

No need for array. 不需要数组。 Just use something like this: 只需使用这样的东西:

Sub ARRAYER()

    Dim Rng As Range
    Dim Number_of_Sims As Long
    Dim i As Long
    Number_of_Sims = 10

    Set Rng = Range("C4:G4")
    For i = 1 To Number_of_Sims
       Rng.Offset(i, 0).Value = Rng.Value
       Worksheets("Sheetname").Calculate   'replacing Sheetname with name of your sheet
    Next

End Sub

Since you are copying tha same data to all rows, you don't actually need to loop at all. 由于您将相同的数据复制到所有行,因此实际上根本不需要循环。 Try this: 试试这个:

Sub ARRAYER()
    Dim Number_of_Sims As Long
    Dim rng As Range

    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Number_of_Sims = 100000

    Set rng = Range("C4:G4")
    rng.Offset(1, 0).Resize(Number_of_Sims) = rng.Value

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub

When i Tried your Code i got en Error when i wanted to fill the Array. 当我尝试你的代码时,当我想要填充数组时,我得到了错误。

you can try to fill the Array like This. 你可以尝试像这样填充数组。

Sub Testing_Data()
Dim k As Long, S2 As Worksheet, VArray

Application.ScreenUpdating = False
Set S2 = ThisWorkbook.Sheets("Sheet1")
With S2
    VArray = .Range("A1:A" & .Cells(Rows.Count, "A").End(xlUp).Row)
End With
For k = 2 To UBound(VArray, 1)
    S2.Cells(k, "B") = VArray(k, 1) / 100
    S2.Cells(k, "C") = VArray(k, 1) * S2.Cells(k, "B")
Next

End Sub

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

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