我的 VBA 代碼是:
Sub hello()
Dim i As Long
Sheets("Sheet10").Select
For i = 3 To 101
Cells(i, 21).Value = Cells(2, 21).Value
Cells(i, 22).Value = Cells(2, 22).Value
Sheets("Sheet10").Calculate
Next i
End Sub
Cells(2,21)并Cells(2,22)包含一個隨機公式,這可能是=RAND()為了簡單起見。我想:
- 復制
Cells(2,21)和Cells(2,22) - 在下一行粘貼
Cells(2,21)和Cells(2,22)取值i; - 重繪
Cells(2,21)和Cells(2,22) - 重復直到
i等于101。
我得到的是,當i=52sub 未命中點 3. 和 from 時i=52,i=101我得到相同的值。我該如何解決這個問題?
uj5u.com熱心網友回復:
由于您似乎正在從其他作業表中提取值,而不僅僅是 Sheet10,因此您可能需要執行更大的重新計算:
Sub hello()
Dim i As Long
With ThisWorkbook.Sheets("Sheet10")
For i = 3 To 101
.Cells(i, 21).Value = .Cells(2, 21).Value
.Cells(i, 22).Value = .Cells(2, 22).Value
Next i
Application.Calculate 'note this affects all open workbooks
End With 'fixed
End Sub
uj5u.com熱心網友回復:
隨機值列
- 在每次迭代中,它會將一列范圍 (
srrg(srrgAddress))的值寫入基于單數的二維陣列 (dData)的一行,并重新計算包含“隨機公式”的范圍。 - 它將從陣列中的值寫入作業表,從
dfRow范圍第一列的行 ( ) 中的單元格開始。
Option Explicit
Sub hello()
Const srrgAddress As String = "U2:V2"
Const dfRow As Long = 3
Const dlRow As Long = 101
Dim srrg As Range: Set srrg = Sheet10.Range(srrgAddress)
Dim cCount As Long: cCount = srrg.Columns.Count
Dim drCount As Long: drCount = dlRow - dfRow 1
Dim dData() As Variant: ReDim dData(dfRow To dlRow, 1 To cCount)
Dim dr As Long
Dim c As Long
For dr = dfRow To dlRow
For c = 1 To cCount
dData(dr, c) = srrg.Cells(c).Value
Next c
srrg.Calculate
Next dr
Dim dfCell As Range: Set dfCell = srrg.Cells(1).EntireColumn.Rows(dfRow)
Dim drg As Range: Set drg = dfCell.Resize(drCount, cCount)
drg.Value = dData
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/388337.html
上一篇:具有隱藏列值的資料框
下一篇:將字串的一部分定義為常量
