shtDestination基于引用的单元格

时间:2013-06-23 00:33:09

标签: excel vba

对于知道这一点的人来说,这可能会很快,我会感激任何帮助...

我有这段代码:

  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

......并且工作正常......我需要根据位于另一个单元格中的名称(例如H5)更改名为“Opex Pers Sala Admi”的工作表。该单元格中的内容会根据指示要使用广告目标的工作表的某些条件而有所不同。

谢谢

来自评论的

更新

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

1 个答案:

答案 0 :(得分:0)

尝试以下代码

  • 在for循环中,不需要明确地增加计数器变量

 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