0

I have a new workbook based on a template that is opened during a procedure, and I want to copy specific cells from an open workbook to cells in the new workbook. But the copy and paste does not seem to be working, not sure where I am running into difficulty. The code just runs through with no copy/paste happening. An example of one of the copy/paste transactions is here.

    Dim wbk as Workbook
    Dim TxtBk as Workbook
    Dim TmpBk as Workbook                                                                     

    Set wbk = ThisWorkbook
    Set TxtBk = ActiveWorkbook
    
'   Get path from worksheet
    fromPath = wbk.Sheets("Instructions").Range("A20").Value
    
'   Make sure there is a backslash at the end of the from path
    If Right(fromPath, 1) <> "\" Then fromPath = fromPath & "\"

'   Open new file based on template
    Set TmpBk = Workbooks.Add(fromPath & "UST37.xltx")

'   Move data from parsed file to template

'   Sheet OAA
'   Running Date
    TxtBk.Sheets("OAA").Activate
    Range("D4").Copy
    TmpBk.Sheets("OAA").Activate
    Range("G4").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False ````

TmpBk is the new workbook based on the template, and TxtBk is the source workbook, which is open. what am I missing? Thanks...

9
  • 1
    Try TmpBk.Sheets("OAA").Range("G4").Value = TxtBk.Sheets("OAA").Range("D4").Value Commented Oct 15, 2024 at 18:47
  • You need to show us the preceding code, that's where you did something wrong. Possibly, both variables point to the same workbook. To prove this, use the following line before: Debug.Print TxtBk.Name, TmpBk.Name. Commented Oct 15, 2024 at 18:47
  • 1
    stackoverflow.com/questions/10714251/… Commented Oct 15, 2024 at 18:48
  • I tried the exact statement provided by taller and it did not work, just bypassed without copying anything. The names from the debug line are... UST37.TXT UST371 Commented Oct 15, 2024 at 19:02
  • 2
    Watch: Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset) Commented Oct 15, 2024 at 20:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.