VBA Formulas not allowing copy and paste as values

JazzSP8

Well-known Member
Joined
Sep 30, 2005
Messages
1,227
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
Hey All

I've got a strange one here.

I've a Workbook that pulls in data from Various Sources via VBA, mixes it altogether and then spits it back out in to a new Workbook that will be emailed out.

It works quite well, almost.

As part of the final part of the process I decided it was going to the easiest to use Formulas to build the last sheet before saving it out as its own Workbook.

When the workbook has been saved, because I've used formulas, this still links to the original Workbook that created it so I need to break this and so I Copy and Paste the formulas as values as that is all I need.

This bit doesn't work so well - The workbook is left with blank columns where the results of the formulas should be.

If I remove the Copy and Paste the values the Workbook retains the formulas linking back to the book.

While trying to debug I've noticed that if I set a Break Point anywhere in the code then it will work correctly, on resuming and the final Workbook has all the columns filled in as they should be with no formulas involved.

I am baffled by this now to be honest - I've even reached the point where I had a message box appear that said "Press OK" before the final workbook was saved, just in case it was the little break that was allowing the Copy / Paste to function correctly.

This is the code that deals with that section, again, this works but just not without a breakpoint somewhere. You can put a break point anywhere after the formulas have been inserted and it will paste as values correctly when the code completes.

If I remove the Copy / Paste values stuff then the workbook will save with Formulas linking back to the original workbook.

Code:
Range("C2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[1],Pricing!C[-2]:C[14],17,FALSE),"""")"
Range("C2").AutoFill Destination:=Range("C2:C" & LastRow)

Range("E2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1],Pricing!C[-4]:C[13],18,FALSE),"""")"
Range("E2").AutoFill Destination:=Range("E2:E" & LastRow)

Range("G2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-3],Pricing!C[-6]:C[12],19,FALSE),"""")"
Range("G2").AutoFill Destination:=Range("G2:G" & LastRow)

For x = 2 To LastRow
    If Cells(x, "H").Value = "" Then
        Range("H" & x).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-4],'Old Brochure'!C[-7]:C[-4],4,FALSE),"""")"
    End If
Next x

Range("I2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-5],'Old Brochure'!C[-8]:C[-3],6,FALSE),"""")"
Range("I2").AutoFill Destination:=Range("I2:I" & LastRow)

Range("J2").FormulaR1C1 = "=IFERROR((RC[-1]-RC[8])/RC[-1],"""")"
Range("J2").AutoFill Destination:=Range("J2:J" & LastRow)

Range("K2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-7],Pricing!C[-10]:C[-5],6,FALSE),"""")"
Range("K2").AutoFill Destination:=Range("K2:K" & LastRow)

Range("L2").FormulaR1C1 = "=IF(OR(NOT(VLOOKUP(RC[-8],'Old Brochure'!C[-11]:C[-3],9,FALSE=0)),ISERROR(VLOOKUP(RC[-8],'Old Brochure'!C[-11]:C[-3],9,FALSE))),"""",VLOOKUP(RC[-8],'Old Brochure'!C[-11]:C[-3],9,FALSE))"
Range("L2").AutoFill Destination:=Range("L2:L" & LastRow)

Range("M2").FormulaR1C1 = "=IFERROR((RC[-1]-RC[5])/RC[-1],"""")"
Range("M2").AutoFill Destination:=Range("M2:M" & LastRow)

Range("N2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-10],Data!C[-13]:C[-8],6,FALSE),"""")"
Range("N2").AutoFill Destination:=Range("N2:N" & LastRow)

Range("O2").FormulaR1C1 = "=IFERROR((RC[-1]-RC[2])/RC[-1],"""")"
Range("O2").AutoFill Destination:=Range("O2:O" & LastRow)

Range("P2").FormulaR1C1 = "=IFERROR(RC[-6]-RC[-1],"""")"
Range("P2").AutoFill Destination:=Range("P2:P" & LastRow)

Range("Q2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-13],Pricing!C[-16]:C[-15],2,FALSE),"""")"
Range("Q2").AutoFill Destination:=Range("Q2:Q" & LastRow)

Range("R2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-14],Pricing!C[-17]:C[-15],3,FALSE),"""")"
Range("R2").AutoFill Destination:=Range("R2:R" & LastRow)

Range("S2").FormulaR1C1 = "=IF(RC[-2]=0,""A"",IFERROR((ABS(RC[-2]-RC[-1])/RC[-2]),""""))"
Range("S2").AutoFill Destination:=Range("S2:S" & LastRow)

Range("T2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-16],Pricing!C[-19]:C[-15],5,FALSE),"""")"
Range("T2").AutoFill Destination:=Range("T2:T" & LastRow)

Range("U2").FormulaR1C1 = "=IFERROR((RC[-1]-RC[-3])/RC[-1],"""")"
Range("U2").AutoFill Destination:=Range("U2:U" & LastRow)

Range("V2").FormulaR1C1 = "=IFERROR(RC[-2]-RC[-13],"""")"
Range("V2").AutoFill Destination:=Range("V2:V" & LastRow)

Application.StatusBar = "Making things look pretty"

Range("H2:H" & LastRow).NumberFormat = "$#,##0.00"
Range("I2:I" & LastRow).NumberFormat = "$#,##0.00"
Range("J2:J" & LastRow).NumberFormat = "0.00%"
Range("K2:K" & LastRow).NumberFormat = "General"
Range("L2:L" & LastRow).NumberFormat = "$#,##0.00"
Range("M2:M" & LastRow).NumberFormat = "0.00%"
Range("N2:N" & LastRow).NumberFormat = "$#,##0.00"
Range("O2:O" & LastRow).NumberFormat = "0.00%"
Range("P2:P" & LastRow).NumberFormat = "0.00%"
Range("Q2:Q" & LastRow).NumberFormat = "$#,##0.00"
Range("R2:R" & LastRow).NumberFormat = "$#,##0.00"
Range("S2:S" & LastRow).NumberFormat = "$#,##0.00"
Range("T2:T" & LastRow).NumberFormat = "$#,##0.00"
Range("U2:U" & LastRow).NumberFormat = "0.00%"
Range("V2:V" & LastRow).NumberFormat = "$#,##0.00"

Sheets("Imported Brochure File").Select
Sheets("Imported Brochure File").Copy

ChDir ActiveWorkbook.Path

MM = Month(Now)

If MM = 1 Then FDate = "January"
If MM = 2 Then FDate = "February"
If MM = 3 Then FDate = "March"
If MM = 4 Then FDate = "April"
If MM = 5 Then FDate = "May"
If MM = 6 Then FDate = "June"
If MM = 7 Then FDate = "July"
If MM = 8 Then FDate = "August"
If MM = 9 Then FDate = "September"
If MM = 10 Then FDate = "October"
If MM = 11 Then FDate = "November"
If MM = 12 Then FDate = "December"

FinalFile = FinalFile & " for PMs " & FDate & " Selection"

With ActiveWorkbook
    .KeepChangeHistory = True
    .ChangeHistoryDuration = 100
End With

    Columns("C:C").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    Columns("E:E").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Columns("G:G").Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Columns("H:I").Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    Columns("K:K").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    Columns("L:L").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    Columns("N:N").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    Columns("Q:Q").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Columns("R:R").Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Columns("T:T").Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

ActiveWorkbook.SaveAs Filename:=FinalFile & ".xlsx", FileFormat:=xlOpenXMLWorkbook, AccessMode:=xlShared

Has anyone had any experience with this at all?

I've tried other Copy and Paste methods, I've moved the code to different parts - I've tried Application.Calculate, I've activated Calculation before Save, I've used Application.Wait.

Really am at a loss as to why a break point sorts out my problem but it won't "just work".

Thanks in advance for any help that can be provided.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Not sure on this but if a break point works, is it worth trying a pause in the Macro just a few seconds to allow it to process results
 
Upvote 0
Does it make any difference if you remove the Selects and Selection which is slowing the code and put DoEvents afterwards?
 
Upvote 0
Not sure on this but if a break point works, is it worth trying a pause in the Macro just a few seconds to allow it to process results

No, I tried that - Had it wait for 10 seconds but nothing. Even made it pop up a Message Box with a simple "Press OK" to see if that'd work, nope :-/

Does it make any difference if you remove the Selects and Selection which is slowing the code and put DoEvents afterwards?

Not 100% sure if I know what you mean with DoEvents afterwards - Not something I've used before I don't think? - Can you give an example please?
 
Upvote 0
It is literally one line
Code:
DoEvents
Just Google DoEvents VBA and you still need to remove all those Selects and Selections.
 
Upvote 0
No, still doesn't work :(

I put the DoEvents after the Formula / Formatting stuff and put my Copy and Paste back to how it was before.

Same thing, if I put a break point in then it works, if I just let it run the columns are empty in the exported Workbook.

Code:
Range("C2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[1],Pricing!C[-2]:C[14],17,FALSE),"""")"
Range("C2").AutoFill Destination:=Range("C2:C" & LastRow)

Range("E2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1],Pricing!C[-4]:C[13],18,FALSE),"""")"
Range("E2").AutoFill Destination:=Range("E2:E" & LastRow)

Range("G2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-3],Pricing!C[-6]:C[12],19,FALSE),"""")"
Range("G2").AutoFill Destination:=Range("G2:G" & LastRow)

For x = 2 To LastRow
    If Cells(x, "H").Value = "" Then
        Range("H" & x).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-4],'Old Brochure'!C[-7]:C[-4],4,FALSE),"""")"
    End If
Next x

Range("I2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-5],'Old Brochure'!C[-8]:C[-3],6,FALSE),"""")"
Range("I2").AutoFill Destination:=Range("I2:I" & LastRow)

Range("J2").FormulaR1C1 = "=IFERROR((RC[-1]-RC[8])/RC[-1],"""")"
Range("J2").AutoFill Destination:=Range("J2:J" & LastRow)

Range("K2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-7],Pricing!C[-10]:C[-5],6,FALSE),"""")"
Range("K2").AutoFill Destination:=Range("K2:K" & LastRow)

Range("L2").FormulaR1C1 = "=IF(OR(NOT(VLOOKUP(RC[-8],'Old Brochure'!C[-11]:C[-3],9,FALSE=0)),ISERROR(VLOOKUP(RC[-8],'Old Brochure'!C[-11]:C[-3],9,FALSE))),"""",VLOOKUP(RC[-8],'Old Brochure'!C[-11]:C[-3],9,FALSE))"
Range("L2").AutoFill Destination:=Range("L2:L" & LastRow)

Range("M2").FormulaR1C1 = "=IFERROR((RC[-1]-RC[5])/RC[-1],"""")"
Range("M2").AutoFill Destination:=Range("M2:M" & LastRow)

Range("N2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-10],Data!C[-13]:C[-8],6,FALSE),"""")"
Range("N2").AutoFill Destination:=Range("N2:N" & LastRow)

Range("O2").FormulaR1C1 = "=IFERROR((RC[-1]-RC[2])/RC[-1],"""")"
Range("O2").AutoFill Destination:=Range("O2:O" & LastRow)

Range("P2").FormulaR1C1 = "=IFERROR(RC[-6]-RC[-1],"""")"
Range("P2").AutoFill Destination:=Range("P2:P" & LastRow)

Range("Q2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-13],Pricing!C[-16]:C[-15],2,FALSE),"""")"
Range("Q2").AutoFill Destination:=Range("Q2:Q" & LastRow)

Range("R2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-14],Pricing!C[-17]:C[-15],3,FALSE),"""")"
Range("R2").AutoFill Destination:=Range("R2:R" & LastRow)

Range("S2").FormulaR1C1 = "=IF(RC[-2]=0,""A"",IFERROR((ABS(RC[-2]-RC[-1])/RC[-2]),""""))"
Range("S2").AutoFill Destination:=Range("S2:S" & LastRow)

Range("T2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-16],Pricing!C[-19]:C[-15],5,FALSE),"""")"
Range("T2").AutoFill Destination:=Range("T2:T" & LastRow)

Range("U2").FormulaR1C1 = "=IFERROR((RC[-1]-RC[-3])/RC[-1],"""")"
Range("U2").AutoFill Destination:=Range("U2:U" & LastRow)

Range("V2").FormulaR1C1 = "=IFERROR(RC[-2]-RC[-13],"""")"
Range("V2").AutoFill Destination:=Range("V2:V" & LastRow)

Application.StatusBar = "Making things look pretty"

Range("H2:H" & LastRow).NumberFormat = "$#,##0.00"
Range("I2:I" & LastRow).NumberFormat = "$#,##0.00"
Range("J2:J" & LastRow).NumberFormat = "0.00%"
Range("K2:K" & LastRow).NumberFormat = "General"
Range("L2:L" & LastRow).NumberFormat = "$#,##0.00"
Range("M2:M" & LastRow).NumberFormat = "0.00%"
Range("N2:N" & LastRow).NumberFormat = "$#,##0.00"
Range("O2:O" & LastRow).NumberFormat = "0.00%"
Range("P2:P" & LastRow).NumberFormat = "0.00%"
Range("Q2:Q" & LastRow).NumberFormat = "$#,##0.00"
Range("R2:R" & LastRow).NumberFormat = "$#,##0.00"
Range("S2:S" & LastRow).NumberFormat = "$#,##0.00"
Range("T2:T" & LastRow).NumberFormat = "$#,##0.00"
Range("U2:U" & LastRow).NumberFormat = "0.00%"
Range("V2:V" & LastRow).NumberFormat = "$#,##0.00"

DoEvents

Sheets("Imported Brochure File").Select
Sheets("Imported Brochure File").Copy

ChDir ActiveWorkbook.Path

MM = Month(Now)

If MM = 1 Then FDate = "January"
If MM = 2 Then FDate = "February"
If MM = 3 Then FDate = "March"
If MM = 4 Then FDate = "April"
If MM = 5 Then FDate = "May"
If MM = 6 Then FDate = "June"
If MM = 7 Then FDate = "July"
If MM = 8 Then FDate = "August"
If MM = 9 Then FDate = "September"
If MM = 10 Then FDate = "October"
If MM = 11 Then FDate = "November"
If MM = 12 Then FDate = "December"

FinalFile = FinalFile & " for PMs " & FDate & " Selection"

With ActiveWorkbook
    .KeepChangeHistory = True
    .ChangeHistoryDuration = 100
End With

Range("C1:C" & LastRow).Value = Range("C1:C" & LastRow).Value
Range("E1:E" & LastRow).Value = Range("E1:E" & LastRow).Value
Range("G1:G" & LastRow).Value = Range("G1:G" & LastRow).Value
Range("H1:I" & LastRow).Value = Range("H1:I" & LastRow).Value
Range("K1:K" & LastRow).Value = Range("K1:K" & LastRow).Value
Range("L1:L" & LastRow).Value = Range("L1:L" & LastRow).Value
Range("N1:N" & LastRow).Value = Range("N1:N" & LastRow).Value
Range("Q1:Q" & LastRow).Value = Range("Q1:Q" & LastRow).Value
Range("R1:R" & LastRow).Value = Range("R1:R" & LastRow).Value
Range("T1:T" & LastRow).Value = Range("T1:T" & LastRow).Value

ActiveWorkbook.SaveAs Filename:=FinalFile & ".xlsx", FileFormat:=xlOpenXMLWorkbook, AccessMode:=xlShared

:oops:
 
Upvote 0
I am going to ask a silly question but when you run the code what workbook/worksheet is active and the same question when you run it with the break point.
Asking because the code you posted doesn't show that part.
 
Last edited:
Upvote 0
I am going to ask a silly question but when you run the code what workbook/worksheet is active and the same question when you run it with the break point.
Asking because the code you posted doesn't show that part.

LOL - At this point it's not silly - I'm willing to try anything!

It's the right Worksheet though, one of the first things I checked!

Also the Formulas are still present and correct on the original worksheet that gets copied so I know it isn't copy and pasting on top of itself there as well :)

I have in fact also tried copy and pasting the values in the original sheet and then copying it, but that doesn't work either - It's as though the formulas don't get calculated until the macro is interrupted or ends (Calculation is never switched off and I've used Application.Calculate to force it on a previous attempt)

It is really bizarre, I've done this kind of thing in the past with no issue at all - I just don't understand this at all.

I've just tried separating the Copy and Paste into it's own Sub. I saved the Workbook with the Formulas in and then called another Sub that reopens it, copy and paste as values and saves again - Still doesn't work. Except when I set a break point.

It is especially frustrating that I can't debug this in any meaningful way as the break points I set make it work :-/
 
Upvote 0
If the workbook isn't confidential can you upload it to either www.box.com or www.dropbox.com, mark it for sharing and post the link it provides in the thread.
I won't be able to look at it for a few hours but if no-one else does I will look at it then and see if I can see the issue.
 
Upvote 0
Hmmm - Does have sensitive data in unfortunately and part of the process accesses a Database on our Network drive so the code wouldn't actually run.

I'll have a play around for the rest of the day and see if I can get anywhere with it - If not then I'll knock some random dummy data up and simulate the database query (somehow) for it and post a link for that. It'll be the same code just the data will be different :)

Heh - While I'm here, I've just got back from testing it on a colleagues machine to see if it'd work there - That is pretty much where I am up to with it now :-/

Didn't work though, so, actually quite relieved.

Thanks by the way, for the thoughts so far - It is appreciated :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,453
Messages
6,124,925
Members
449,195
Latest member
Stevenciu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top