excel vba – 通过行/列地址间接复制?

我有一个调查数据的工作表,其中每列包含一个调查。 我现在希望复制当前审查调查的特定单元格,并将其粘贴成以后将附加到电子邮件中的格式。 下面的代码是计算正在审查的调查的当前列号,然后尝试复制该列中的特定单元格,但是没有数据被粘贴到该格式中。

以下代码:

Sub test Dim dataSheet, dataStart, dataSurvey As Range dataSheet = "MED-FB-YTD" ' name of the sheet where data is stored Set dataSurvey = ThisWorkbook.Sheets("HeartBeat_Alert").Range("K5") ' survey number of specific record eg. 5 Set dataStart = ThisWorkbook.Sheets("HeartBeat_Alert").Range("K4") ' column number of first survey of specified time period eg. 307 dataCol = dataSurvey + dataStart - 1 ' actual data column is (dataStart + dataSurvey) - 1 eg. = 5+307-1 = column 311 With ThisWorkbook.Sheets("HeartBeat_Alert") .Select Sheets(dataSheet).Cells(25, dataCol) = ActiveSheet.Range("B3").Value ' copy room no Sheets(dataSheet).Cells(16, dataCol) = ActiveSheet.Range("B4").Value ' copy arrival date Sheets(dataSheet).Cells(17, dataCol) = ActiveSheet.Range("B5").Value ' copy departure date end with end sub 

在我看来,像我试图复制数据的方式不工作/支持,因此将不胜感激一些帮助。

谢谢,A2k

首先: dataSheet应该是String ,而不是Range对象。

您可以省略dataSurveydataStart声明并使用它(不会影响任何内容,所以您不必这样做):

 dataCol = Sheets("HeartBeat_Alert").Cells(5, 11).Value - Sheets("HeartBeat_Alert").Cells(4, 11).Value 

另一件事。 如果您不必Select表(“HeartBeat_Alert”),则不要使用.Cells(3, 2).Value而不是ActiveSheet.Range("B3").Value (它也不应该影响结果,但效率更高)。

这些变化之后,它应该工作。