基于引用单元格的shtDestination

这对知道的人来说可能会很快,所以我会很感激任何帮助。

我有这个代码:

Dim shtSource As Worksheet Dim shtDestination As Worksheet Dim nSourceRow As Long, nDestRow As Long Set shtSource = Sheets("Forecast Opex") Set shtDestination = Sheets("Opex Pers Sala Admi") nSourceRow = 46 For nDestRow = 2 To 97 shtDestination.Cells(nDestRow, 3) = shtSource.Cells(nSourceRow, 2).Value 'NewForecast nSourceRow = nSourceRow + 1 Next nDestRow 

…工作得很好…事情是,我需要使名为“Opex Pers Sala Admi”的表格根据位于另一个单元格中的名称(例如H5)进行更改。 该单元中的内容根据指示要使用的广告目标的图纸的特定条件而变化。

谢谢

从评论更新

 Sub Update_Click() Dim shtSource As Worksheet Dim shtDestination As Worksheet Dim nSourceRow As Long, nDestRow As Long Set shtSource = Sheets("Base") Set shtDestination = Sheets(Sheets("Base").Range("L21")) '... End Sub 

尝试下面的代码

  • 在for循环中,不需要显式递增计数器variables

  Sub test() Dim shtSource As Worksheet Dim shtDestination As Worksheet Dim nSourceRow As Long, nDestRow As Long Set shtSource = Sheets("Sheet1") Set shtDestination = Sheets(Sheets("<sheet name>").Range("H5")) nSourceRow = 46 For nDestRow = 2 To 97 shtDestination.Cells(nDestRow, 3) = shtSource.Cells(nSourceRow, 2).Value Next nDestRow End Sub