简体   繁体   English

打开另一个 Excel 工作簿并使用 VBA 获得一列

[英]Open another Excel workbook and attain a column using VBA

I am trying to create a UserForm with VBA where it would silently open a Workbook (with a CommandButton) and copy the contents of specified column to a ListBox in the same GUI, but upon running, it opens the sheet and sticks there without data copyback.我正在尝试使用 VBA 创建一个用户窗体,它会以静默方式打开工作簿(使用命令按钮)并将指定列的内容复制到同一 GUI 中的列表框,但在运行时,它会打开工作表并粘贴在那里,而没有数据回传. Here is the bulk part of the code.这是代码的大部分内容。 Can anyone please help?有人可以帮忙吗?

Sub OpenBox()

Dim owbSource As Workbook
Dim rSource As Range

With Application.FileDialog(msoFileDialogFilePicker)
    .Show
    fullPath = .SelectedItems.Item(1)
End With

Application.ScreenUpdating = False

Set owbSource = Workbooks.Open(fullPath, True, True)

ThisWorkbook.Activate

Set rSource = owbSource.Worksheets("Property").Range("B5:B77")

ListBox1.RowSource = rSource

End Sub

I have change some lines, please try this我改变了一些线路,请试试这个

Dim owbSource As Workbook
Dim rSource As Range
Dim rsheetsource As Worksheet
With Application.FileDialog(msoFileDialogFilePicker)
    .Show
    fullPath = .SelectedItems.Item(1)
End With
Application.ScreenUpdating = False
Set owbSource = Workbooks.Open(fullPath, True, True)
Set rsheetsource = owbSource.Sheets("Property")
ThisWorkbook.Activate
Set rSource = rsheetsource.Range("B5:B77")
ListBox1.RowSource = "'" & owbSource.Path & "/[" & owbSource.Name & "]" & rsheetsource.Name & "'!" & rSource.Address
Application.ScreenUpdating = True

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

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