Excel VBA 代码基于对另一个工作表的单元格引用复制数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7890521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:20:33  来源:igfitidea点击:

Excel VBA Code to Copy Data based on a cell reference to another sheet

excelvba

提问by ExcelForFreelancers

I would like to have data from one sheet copied to another sheet in Excel. The location of the data in the firs sheet is static, while the Sheet, Column, and Row in which the data is being copied is variable, and based on cell data. I would like a bit of help with the VB Code. Consider the following Excel Table Sample Data:

我想将一张工作表中的数据复制到 Excel 中的另一张工作表。第一个工作表中数据的位置是静态的,而复制数据的工作表、列和行是可变的,并且基于单元格数据。我想要一些有关 VB 代码的帮助。考虑以下 Excel 表示例数据:

`Sheet1`
'A1 = Apples'
'B1 = Sheet2'
'C1 = 5'
'D1 = 10'

In the above sample, i would like to have a VB code place "Apples" on Sheet2 at Column 5 (can be column E as well), Row 10. On the click of a button, this will be copied over. Thanks so much in advance for your help in this matter. Randy

在上面的示例中,我想在 Sheet2 的第 5 列(也可以是 E 列)、第 10 行放置一个 VB 代码“Apples”。单击按钮,这将被复制。非常感谢您在这件事上的帮助。兰迪

回答by Tim Williams

Dim sht, rw, col, val

With ThisWorkbook.sheets("Sheet1")
  val = .Range("A1").Value
  sht = .Range("B1").Value
  col = .Range("C1").Value
  rw = .Range("D1").Value
End with

if isnumeric(col) then
   thisworkbook.sheets(sht).Cells(rw,col).value = val
else
  thisworkbook.sheets(sht).Range(col & rw).value = val
end if