Formula Not Pasting Through VBA

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
714
Office Version
  1. 365
Platform
  1. Windows
I have the code below, which is almost an exact copy of code elsewhere in the same workbook; but for some reason, the formula isn't being entered into the workbook. I've stepped through the code, and every line works as expected, except for the formula entry. Thoughts?

Code:
Sub IDDupBKI()
Application.ScreenUpdating = False
Dim UV As Worksheet
Set UV = ThisWorkbook.Sheets("Unique_Volume")
If UV.FilterMode = True Then
    UV.ShowAllData
Else
End If
'Sorts the Unique Volume sheet by Delete, Month, Queue
SortAsc UV, "Q1", "P1", "I1"
With UV
    'On Error Resume Next
    .UsedRange.AutoFilter Field:=9, Operator:=xlFilterValues, Criteria1:=Array("BKI Holds", "BKI Issues")
End With
'Sorts the visible cells by Loan, Bucket 1, Bucket 2, Date, Calculated Time
SortAsc UV, "A1", "B1", "C1", "D1", "G1"
UVLR = UV.Range("A" & Rows.Count).End(xlUp).Row
If UVLR > 1 Then
    Range("Q2:Q" & UVLR).SpecialCells(xlCellTypeVisible).Value = "=IF(AND(RC[-8]=R[-1]C[-8],RC[-16]=R[-1]C[-16],RC[-15]=R[-1]C[-15],RC[-14]=R[-1]C[-14],RC[-13]=R[-1]C[-13],RC[-12]=R[-1]C[-12]),""Yes"",""No"")"
Else
End If
UV.Range("Q2:Q" & UVLR).Copy
    UV.Range("Q2").PasteSpecial xlPasteValues
Application.CutCopyMode = False
UV.ShowAllData
Application.ScreenUpdating = True
End Sub
 
Ahhh...the value of UVLR is 6034. Once the filter happens, I'm expecting the formula to be entered in Q3426:Q6034.
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
In the code that you've posted this line is commented out
Code:
On Error resume Next
Is it actually commented out in the code you're running?
 
Upvote 0
So just to be sure when we test, rows 2 to 3426 are being hidden when you run the filter and you have put UV. in front of range?

Edit: don't miss Fluff's post at the same time as mine.
 
Last edited:
Upvote 0
Here is the code that works. It works with the On Error Resume Next commented out, or not.
Code:
Sub IDDupBKI()
Application.ScreenUpdating = False
Dim UV As Worksheet
Set UV = ThisWorkbook.Sheets("Unique_Volume")
If UV.FilterMode = True Then
    UV.ShowAllData
Else
End If
'Sorts the Unique Volume sheet by Delete, Month, Queue
SortAsc UV, "Q1", "P1", "I1"
With UV
    On Error Resume Next
    .UsedRange.AutoFilter Field:=9, Operator:=xlFilterValues, Criteria1:=Array("BKI Holds", "BKI Issues")
End With
'Sorts the visible cells by Loan, Bucket 1, Bucket 2, Date, Calculated Time
SortAsc UV, "A1", "B1", "C1", "D1", "G1"
UVLR = UV.Range("A" & Rows.Count).End(xlUp).Row
If UVLR > 1 Then
    UV.Range("Q2:Q" & UVLR).SpecialCells(xlCellTypeVisible).Value = "=IF(AND(RC[-8]=R[-1]C[-8],RC[-16]=R[-1]C[-16],RC[-15]=R[-1]C[-15],RC[-14]=R[-1]C[-14],RC[-13]=R[-1]C[-13],RC[-12]=R[-1]C[-12]),""Yes"",""No"")"
Else
End If
UV.Range("Q2:Q" & UVLR).Copy
    UV.Range("Q2").PasteSpecial xlPasteValues
Application.CutCopyMode = False
UV.ShowAllData
Application.ScreenUpdating = True
End Sub

Here is the code that isn't working, with the UV in front of .Range
Code:
Sub IDDupHLMS()
Application.ScreenUpdating = False
Dim UV As Worksheet
Set UV = ThisWorkbook.Sheets("Unique_Volume")
If UV.FilterMode = True Then
    UV.ShowAllData
Else
End If
SortAsc UV, "Q1", "P1", "I1"
With UV
    On Error Resume Next
    .UsedRange.AutoFilter Field:=9, Operator:=xlFilterValues, Criteria1:=Array("HLMS")
End With
SortAsc UV, "B1"
UVLR = UV.Range("A" & Rows.Count).End(xlUp).Row
If UVLR > 1 Then
    UV.Range("Q2:Q" & UVLR).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=IF(RC[-15]=R[-1]C[-15]),""Yes"",""No"")"
Else
End If
UV.ShowAllData
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Comment out the
Code:
On Error Resume Next
What happens if anything?
 
Upvote 0
I get an application undefined error on this line...
Code:
    UV.Range("Q2:Q" & UVLR).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=IF(RC[-15]=R[-1]C[-15]),""Yes"",""No"")"
 
Upvote 0
It would have helped if you had posted the non working code to begin with rather than the working code.
Your formula is wrong, remove the ) in red
Code:
"=IF(RC[-15]=R[-1]C[-15][COLOR=#ff0000])[/COLOR],""Yes"",""No"")"
 
Upvote 0
Well crap! I completely spaced on that. My sincere apologies for the oversight. Removing that closing parenthesis appears to have worked. What I don't understand is, why wouldn't it have tried to insert the incorrect formula, resulting in an error on the spreadsheet? Then I would've known to go and review the formula more closely.
 
Upvote 0
Just a tip, if you are going to use error handling for a specific action (the only reason to have it) then disable it immediately after the action has passed.

Code:
With UV
    On Error Resume Next
    .UsedRange.AutoFilter Field:=9, Operator:=xlFilterValues, Criteria1:=Array("HLMS")
    [COLOR="#FF0000"]On Error Goto 0[/COLOR]
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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