Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 24

Thread: Vba open workbook and paste from another workbook

  1. #1

    Vba open workbook and paste from another workbook

    hi can you please help me with this code keep getting the error "object variable or with variable not set"

    many thanks

    Sub test() 
     
    Dim fname As String 
    Dim fpath As String 
    Dim owb As Workbook 
     
    fname = "DummyXXAC.xlsm" 
    fpath = "C:\Users\USER\Desktop" 
    owb = Application.Workbooks.Open(fpath & "\" & fname) 
    Windows("CNAV.xlsm").Activate 
    Set rngcopy = Sheets("Raw data").Range("A1:IV1").Find("Ccy", LookIn:=xlValues) 
    Set rngcopy = Range(rngcopy.Offset(1, 0), Cells(Rows.Count, rngcopy.Column).End(xlUp)) 
    Set rngPaste = Workbooks(DummyXXAC.xlsm).Sheets("Sheet1").Range("d2") 
    rngcopy.Copy 
    rngPaste.PasteSpecial xlPasteValues 
    rngPaste.PasteSpecial xlPasteFormats 
    
    End Sub

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Try changing this line:
    owb = Application.Workbooks.Open(fpath & "\" & fname)
    to:
    Set owb = Application.Workbooks.Open(fpath & "\" & fname)

    also the following line may not work:
    Set rngPaste = Workbooks(DummyXXAC.xlsm).Sheets("Sheet1").Range("d2")
    so make it either:
    Set rngPaste = Workbooks("DummyXXAC.xlsm").Sheets("Sheet1").Range("d2")
    or:
    Set rngPaste = Workbooks(fname).Sheets("Sheet1").Range("d2")
    All untested.
    Last edited by p45cal; 02-21-2015 at 05:52 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    You are a Genius thank youuuuu soooooo much. It was really helpful

  4. #4
    hi,

    could you please help me with this code, error say invalid use of property, many thanks

    Windows ("CNAV" & "" & "IACB" & "" & Format(Sheets("Main").Range("C6").Value, "yyyymmdd").Activate)

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Windows("CNAVIACB" & Format(Sheets("Main").Range("C6").Value, "yyyymmdd")).Activate
    ?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    Quote Originally Posted by p45cal View Post
    Windows("CNAVIACB" & Format(Sheets("Main").Range("C6").Value, "yyyymmdd")).Activate
    ?
    hi p45 thanks for your reply but you see the name of the file is CNAV IACB yyyymmdd, there is a space between the words CNAV and IACB.

    should i still proceed waiting for your advice many thanks

  7. #7
    added CNAV and IACB and yyyymmdd

    hi p45 thanks for your reply but you see the name of the file is CNAV IACB yyyymmdd, there is a space between the words CNAV and IACB and yyyymmdd


    should i still proceed waiting for your advice many thanks

  8. #8
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Windows("CNAV IACB " & Format(Sheets("Main").Range("C6").Value, "yyyymmdd")).Activate
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  9. #9
    hi p45 i have tried yur code but it says compile error invalid use of property, can help me on this please thanks

  10. #10
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    You're going to have to do some detective work.
    When you get the error, in the Immediate pane of the vbe (Ctrl+G ifyou can't see it), type or copy:
    ?Sheets("Main").Range("C6").Value
    and press Enter. Do you see the contents of cell C6 as you expect? (Make sure that when you do this the workbook that would normally be active at this juncture in the code, is the active workbook (the sheet Main shoulkd be in the active workbook).)

    If at this point you get the same error as before, put an apostrophe at the extreme left of the highlighted line of code to comment it out temporarily, and try again.

    If all OK, then enter the following in the Immediate pane:
    ?"]" & Format(Sheets("Main").Range("C6").Value, "yyyymmdd") & "["
    and press Enter. Does the result (the date part only of the filename) look right between the ] and [ characters?

    If so then enter:
    ?"]" & "CNAV IACB " & Format(Sheets("Main").Range("C6").Value, "yyyymmdd") & "["
    Press Enter. Does the full file name look right? If so add .xls, or .xlsm or whatever the file name extension is, as follows:
    ?"]" & "CNAV IACB " & Format(Sheets("Main").Range("C6").Value, "yyyymmdd") & ".xlsx" & "["
    Press Enter. Does it look right?
    If so enter the following:
    Windows("CNAV IACB " & Format(Sheets("Main").Range("C6").Value, "yyyymmdd") & ".xlsx").activate
    and press Enter. Does it activate that workbook/window?

    By now you may well have solved the problem..
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  11. #11
    hi, thanks for your reply this part gave me the correct date ?Sheets("Main").Range("C6").Value

    but
    ?"]" & Format(Sheets("Main").Range("C6").Value, "yyyymmdd") & "[" gave me run time error 6 overflow

    could you please help me with this thanks

  12. #12
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    What does
    ?Clng(Sheets("Main").Range("C6").Value)
    give?
    I'm thinking that your date maybe a string rather than a real Excel date (a number).
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  13. #13
    hi thank you so much i got your point yes it was a string - the date, just one question when i had written this

    ?"]" & "CNAV IACB " & Format(Sheets("Main").Range("C6").Value, "yyyymmdd") & ".xlsx" & "[" and pressed enter i got the desired result
    ]CNAV IACB 20150218.xlsx[

    just wanted to know what it did, i mean was it debug because in your post no.10 at last you said to enter

    Windows("CNAV IACB " & Format(Sheets("Main").Range("C6").Value, "yyyymmdd") & ".xlsx").activate this was the same one when i had first input before contacting you.

  14. #14
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by rohan8510 View Post
    [/B]Windows("CNAV IACB " & Format(Sheets("Main").Range("C6").Value, "yyyymmdd") & ".xlsx").activate this was the same one when i had first input before contacting you.
    Windows("CNAV IACB " & Format(Sheets("Main").Range("C6").Value, "yyyymmdd") & ".xlsx").activate << mine
    Windows ("CNAV" & "" & "IACB" & "" & Format(Sheets("Main").Range("C6").Value, "yyyymmdd").Activate) << yours. Note the last close parentheses position (and the inclusion of .xlsx).

    After some experimentation here it seems that even though the date is a string, Format seems to interpret it, although in some cases, depending on your locale, it might get the month and day mixed up if the date string is of the format 12/11/2015 which could be interpreted as 11 December or the 12th of November.

    So you're all sorted now?
    Last edited by p45cal; 02-23-2015 at 11:41 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  15. #15
    Quote Originally Posted by p45cal View Post
    Windows("CNAV IACB " & Format(Sheets("Main").Range("C6").Value, "yyyymmdd") & ".xlsx").activate << mine
    Windows ("CNAV" & "" & "IACB" & "" & Format(Sheets("Main").Range("C6").Value, "yyyymmdd").Activate) << yours. Note the last close parentheses position (and the inclusion of .xlsx).

    After some experimentation here it seems that even though the date is a string, Format seems to interpret it, although in some cases, depending on your locale, it might get the month and day mixed up if the date string is of the format 12/11/2015 which could be interpreted as 11 December or the 12th of November.

    So you're all sorted now?
    yes, Thank you so much for your help i have started to learn so much from you regarding vba

  16. #16
    hi p45, i have this code every time it says run time error 1004 application defined or object defined error. I am trying to run another macro in another workbook from my current workbook. many thanks

    Workbooks("DummyIACB.xlsm").Application.Run& "macro1"

  17. #17
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    try:
    Application.Run "DummyIACB.xlsm!macro1"
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  18. #18
    Quote Originally Posted by p45cal View Post
    try:
    [CODE]Application.Run "DummyIACB.xlsm!macro1"[/CODE
    hi p45 i tried this but it says run time error 1004 application defined or object defined error. i am actually trying to call the macro in another workbook named DummyIACB.xlsm and it is placed in "This workbook" in the file DummyIACB.xlsm.the name of the file from which iam trying to call is CNAV IACB 20150218.xls many thanks

    iam trying to call
    
    
    Sub macro1()
    Sheets("Sheet1").Activate
    Range("a40").Select
         
    End Sub



  19. #19
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    try:
    Application.Run "DummyIACB.xlsm!ThisWorkbook.macro1"
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  20. #20
    Thanks so much this worked you are in uk? isnt it? by the way my name is Rohan 31 year old male from city Kolkata in India.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •