Two VBA lines essentially identical not showing same results, Why?

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
775
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - I had a VBA to copy a formula down then insert the value right over it. The first way doesn't properly execute the formula. if I remove copying the values the formula executes correctly so i know the formula is correct. But when I break the VBA into two lines one for column A and one for column B it works! Anyone know why I cant combine like the first way?

Doesn't Work - shows wrong formula result
VBA Code:
With Sheets("Calculation")
lr = .Cells(rows.count, "C").End(xlUp).row
    .Range("A3:A" & lr).Formula = "=D3&E3&G3"
    .Range("B3:B" & lr).Formula = "=IF(ISNA(VLOOKUP(A3,'AS'!$A$2:$A$10000,1,FALSE)), ""No"", ""Yes"")"
    .Range("A3:B" & lr).Value = .Range("A3:B" & lr).Value
    .Range("2:2").AutoFilter

Works - shows correct formula result
VBA Code:
With Sheets("Calculation")
lr = .Cells(rows.count, "C").End(xlUp).row
    .Range("A3:A" & lr).Formula = "=D3&E3&G3"
    .Range("B3:B" & lr).Formula = "=IF(ISNA(VLOOKUP(A3,'AS'!$A$2:$A$10000,1,FALSE)), ""No"", ""Yes"")"
    .Range("A3:A" & lr).Value = .Range("A3:A" & lr).Value
    .Range("B3:B" & lr).Value = .Range("B3:B" & lr).Value
    .Range("2:2").AutoFilter

Works - shows correct formula result
VBA Code:
With Sheets("Calculation")
lr = .Cells(rows.count, "C").End(xlUp).row
    .Range("A3:A" & lr).Formula = "=D3&E3&G3"
    .Range("B3:B" & lr).Formula = "=IF(ISNA(VLOOKUP(A3,'AS'!$A$2:$A$10000,1,FALSE)), ""No"", ""Yes"")"
    .Range("2:2").AutoFilter
 
I have looked at this also and I agree, the three code examples are basically the same.

The one exception is the last example that is reported as 'Works', doesn't have the line that removes the formulas, other than that it is the same as the first example that is reported as not working.
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Just for interest can you try inserting this code before the .value = .value line.
VBA Code:
Do Until Application.CalculationState = xlDone
    DoEvents
Loop
 
Upvote 0
Like this?

VBA Code:
Do Until Application.CalculationState = xlDone
    DoEvents
Loop

With Sheets("Calculation")
lr = .Cells(rows.count, "C").End(xlUp).row
    .Range("A3:A" & lr).Formula = "=D3&E3&G3"
    .Range("B3:B" & lr).Formula = "=IF(ISNA(VLOOKUP(A3,'AS'!$A$2:$A$10000,1,FALSE)), ""No"", ""Yes"")"
    .Range("M3:M" & lr).Formula = "=IF(B3=""No"",""No Change"",IF(AND(J3<>0,K3=0),VLOOKUP(A3,'AS'!$A$2:$J$10000,9,FALSE),""No Change""))"
    .Range("N3:N" & lr).Formula = "=IF(AND(J3=0,K3<>0),0,""No Change"")"
    .Range("A3:B" & lr).Value = .Range("A3:B" & lr).Value
    '.Range("B3:B" & lr).Value = .Range("B3:B" & lr).Value
    .Range("M3:M" & lr).Value = .Range("M3:M" & lr).Value
    .Range("N3:N" & lr).Value = .Range("N3:N" & lr).Value
    .Range("2:2").AutoFilter
    .Activate
End With

Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
 
Upvote 0
Like this?
No like the below. After entering the formula and before replacing the formula with the result of the formula as a value.
(I was just wondering if the formulas being entered had not finsied calculating by the time the code tried to replace it with values.)

VBA Code:
With Sheets("Calculation")
lr = .Cells(rows.count, "C").End(xlUp).row
    .Range("A3:A" & lr).Formula = "=D3&E3&G3"
    .Range("B3:B" & lr).Formula = "=IF(ISNA(VLOOKUP(A3,'AS'!$A$2:$A$10000,1,FALSE)), ""No"", ""Yes"")"
    .Range("M3:M" & lr).Formula = "=IF(B3=""No"",""No Change"",IF(AND(J3<>0,K3=0),VLOOKUP(A3,'AS'!$A$2:$J$10000,9,FALSE),""No Change""))"
    .Range("N3:N" & lr).Formula = "=IF(AND(J3=0,K3<>0),0,""No Change"")"

   ' Ensure that calculations have finished before replacing formulas with values
    Do Until Application.CalculationState = xlDone
       DoEvents
    Loop

    .Range("A3:B" & lr).Value = .Range("A3:B" & lr).Value
    '.Range("B3:B" & lr).Value = .Range("B3:B" & lr).Value
    .Range("M3:M" & lr).Value = .Range("M3:M" & lr).Value
    .Range("N3:N" & lr).Value = .Range("N3:N" & lr).Value
    .Range("2:2").AutoFilter
    .Activate
End With

Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
 
Upvote 0
It is working today even without your line of code. The only thing that changed from today to yesterday was I restarted my PC / excel. Kind of confused and annoyed. I am going to closely monitor my test results and will repost here if it comes up again while testing your example Alex. Hopefully it doesn't because I didnt quite get what was happening yesterday
 
Upvote 0
I found it. I was setting calculation to manual prior to the code. It resurfaced. so the formulas weren't calculating then pasting values. the code was embedded in another vba i dont own so I am setting it to automatic before and now works. you sort of led me to it Alex. thanks
 
Upvote 0
Arghh ?‍♂️, that code I gave you is often proceeded with the line
VBA Code:
Application.Calculate
but since you didn't have an
Code:
Application.Calculation = xlCalculationAutomatic
at the end with all your other Application reset commands and the fact that your initial code indicated that some of the options were calculating, I thought it superfluous.

Glad you worked it out. If you haven't already you will want to add the below line to the end of your code though.
Code:
Application.Calculation = xlCalculationAutomatic
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,666
Members
448,977
Latest member
moonlight6

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