简体   繁体   中英

Open workbook through excel vba and copy contents

I want to open a particular file and copy cells from that file and paste in active workbook, but I am getting an error '438': Object doesn't support this property or method.

Here is my code:

Sub open_file()
Dim open_book As Variant

com_name = InputBox("Enter Company Ticker", "Enter Company Ticker")
If (IsEmpty(com_name)) Then
    msg = MsgBox("Please, enter company ticker", vbCritical)
Else
    open_book = Workbooks.Open("E:\Mutual Fund\data\nifty 50\" & com_name & ".xlsx")
    Windows(com_name & ".xlsx").Activate
    Range("A:A,H:H").Select
    Selction.Copy
    Windows("try.xlsm").Activate
    Range("A1").Select
    Selection.Paste
End If 
End Sub

Try the below code

Sub open_file()
    Dim open_book As Variant
    com_name = InputBox("Enter Company Ticker", "Enter Company Ticker")
    If (IsEmpty(com_name)) Then
        msg = MsgBox("Please, enter company ticker", vbCritical)
    Else
        Set open_book = Workbooks.Open("E:\Mutual Fund\data\nifty 50\" & com_name & ".xlsx")
        Range("A:H").Copy Windows("try.xlsm").ActiveSheet.Range("A1")
    End If
End Sub

You have a typo error on " Sel e ction.Copy"

You should also define from which worksheet the ranges come from. If you call your macro with a different active page than the one you want your operation to apply, it won't work. Do something like this :

open_book.Sheets("My_Sheet").Range("A:A,H:H").Select

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