1

对于知道的人来说这可能很快,所以我将不胜感激任何帮助...

我有这个代码:

  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
4

1 回答 1

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
于 2013-06-23T00:47:18.067 回答