简体   繁体   English

将变量从多个工作簿传递到一个工作簿

[英]passing variables from multiple workbooks to one workbook

I am using Visual basic from excel to open a different workbook and pass some things from one to another. 我正在使用Excel的Visual Basic打开另一个工作簿,并将某些东西从一个传递到另一个。 I have the second file opening fine however I cant get the second file to read the things I need. 我已经打开了第二个文件,但是我无法获取第二个文件来读取所需的内容。

Sub createExcel()
Cells(1, 1).Text = "va02"
Dim objExcel
    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = True
    Set objWorkbook = objExcel.Workbooks.Open(filepath)
    End Sub

this is my code to open the second application. 这是我打开第二个应用程序的代码。 I have 2 variables in 2 cells in the first workbook. 我在第一个工作簿的2个单元中有2个变量。 How would one go about getting the variables. 如何获得变量。 the problem i have encountered is; 我遇到的问题是; the second workbook can be called from any file, in any folder. 可以从任何文件,任何文件夹中调用第二个工作簿。

i would try variable = Workbooks(firstWorkbook.xlsm).worksheets(sheet1)... however the worksheets dont have the same names either. 我会尝试变量= Workbooks(firstWorkbook.xlsm).worksheets(sheet1)...但是工作表也没有相同的名称。 please help 请帮忙

thanks 谢谢

Your code is in Workbook1, and opens Workbook2. 您的代码在Workbook1中,并打开Workbook2。 So it's Workbook1 that does everything - so " I cant get the second file to read the things I need" is not meaningful. 因此,正是Workbook1可以完成所有工作-因此“我无法获得第二个文件来读取所需的内容”没有意义。

  Sub Demo(filepath as string)
  ThisWorkbook.Worksheets(1).cells(1,1).text = "This is written by initial Workook"      
  Dim Wb as Workbook
  Set Wb = Workbooks.Open(filepath)
  Wb.Sheets(1).cells(1,2) = "So is this, even though it's in a different workbook"
  End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM