如何在输入表和工作簿之间复制粘贴值

时间:2016-05-19 00:09:51

标签: excel excel-vba vba

我想做什么:

  1. 打开输入表
  2. 打开输出表
  3. 点击按钮复制/转移值A1:A10&从输入表到工作簿的B1:B10
  4. 现在是个大问题:

    1. 工作簿的实际文件名存储在C1又名Input sheet.xls
    2. 现在我如何告诉VBA读取单元格C1,以便知道我的输入表名称是什么?

      到目前为止,我找到了一个只有在手动更改VBA文件名

      时才有效的选项
      Sub transfervalues()
      Dim wb1 As Workbook
      Dim wb2 As Workbook
      Dim ws1 As Worksheet
      Dim ws2 As Worksheet
      
      Set wb1 = Workbooks("Input Sheet.xls")
      Set ws1 = wb1.Sheets("COPY")
      
      Set wb2 = Workbooks("Workbook.xls")
      Set ws2 = wb2.Sheets("PASTE")
      
      ws2.Range("A1:A10").Value = ws1.Range("A1:A10").Value
      ws2.Range("B1:B10").Value = ws1.Range("B1:B10").Value
      
      End Sub
      

2 个答案:

答案 0 :(得分:0)

我假设单元格C1在活动表中。

试试这个:

Set wb1 = Workbooks(ActiveSheet.Range("C1"))

希望得到这个帮助。

答案 1 :(得分:0)

感谢您的帮助,经过一番试验和错误,我得到了它的工作。这就是代码的样子:

Sub transfervalues()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet



Set wb2 = ActiveWorkbook
Set ws2 = wb2.Sheets("Input Sheet")

Set wb1 = Workbooks(ActiveSheet.Range("C1").Value)
Set ws1 = wb1.Sheets("Output Sheet")

ws2.Range("A1:A10").Value = ws1.Range("A1:A10").Value
ws2.Range("B1:B10").Value = ws1.Range("B1:B10").Value

End Sub