简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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