VBA: Fill formula down column

CarolynS

Board Regular
Joined
Oct 27, 2015
Messages
56
Hi I have a data set where i need to filter on 2 criteria and then perform a simple calculation for the remaining records to show the numberof days between 2 dates. I had previously been looping through cells with an if statement to do this but it seems to slow down the code so I'd like to try a different way.

The first part of my code works fine as it's using the same dates all the way down the column:

Code:
    ColRef = WS.UsedRange.Find(What:="Certification - Status", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False).Column
    ColRef2 = WS.UsedRange.Find(What:="Person - Start Date", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False).Column
    ColRef3 = WS.UsedRange.Find(What:="Days Overdue", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False).Column
    
    dDate = DateSerial(2012, 5, 21)
    lDate = dDate
    
    With WS.UsedRange
        .AutoFilter field:=ColRef, Criteria1:="=Not Yet Complete"
        .AutoFilter field:=ColRef2, Criteria1:="<" & lDate
    End With
    
    RwLast = WS.Cells(Rows.Count, 1).End(xlUp).Row
    
    WS.Cells(2, ColRef3).Resize(RwLast - 1).Value = DateDiff("d", lDate, Date)
    
    WS.ShowAllData


However, I'm struglling with the 2nd part where I need tofind the number of days between todays date and the cell to the left of the cell my calculation is in. I have the following code but it's giving an "applpication defined or object defined" error. The line .FormulaR1C1 = "= today()- RC[,-1]" seems to be wrong but I'm not sure of the correct way to write it.

Code:
    With WS.UsedRange
        .AutoFilter field:=ColRef, Criteria1:="=Not Yet Complete"
        .AutoFilter field:=ColRef2, Criteria1:=">=" & lDate
    End With
    
    RwLast = WS.Cells(Rows.Count, 1).End(xlUp).Row
    
   With WS.Cells(2, ColRef3).Resize(RwLast - 1)
    .FormulaR1C1 = "= today()- RC[,-1]"
    .FillDown
    .Value = .Value
    End With


Also, is this way of writing the code more efficient than looping? or is there a better way?

thanks
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I have now amended part of the code (see below) and the formula is now being populated in the required column. However, when i get to the line .value=.value (i'm trying to remove the formula and just leave the values in place) most of the values dissapear and the cells become blank. If i scroll down the column though there appear to be some values showing at (what looks like) random intervals. I've got no idea why this is happening. I'm using very similar code in another workbook and it works fine

With WS.Cells(2, ColRef3).Resize(RwLast - 1)
.FormulaR1C1 = "=today()- RC[2]"
.FillDown
.Value = .Value
End With
 
Upvote 0
It may be because you've turned calculation mode to manual so the values aren't updating prior to hardcoding and have you tried removing .FillDown, it doesn't seem necessary there. Try:
Code:
With WS.Cells(2, ColRef3).Resize(RwLast - 1)
  .FormulaR1C1 = "=TODAY()-RC[2]"
  .Calculate
  .Value = .Value
End With
 
Upvote 0
Hi Thanks for your reply. I've just tried yur suggestion but the same thing is happening and calculation mode is set to automatic. The values are all showing but then they just dissapear at .value=.value. Could it be something to do with the data being filtered and thereofre isn't a continuos selection?
 
Upvote 0
Missed part of filtered rows, untested, but try:
Code:
With ws.Cells(2, ColRef3).Resize(RwLast - 1)
  .FormulaR1C1 = "=TODAY()-RC[2]"
  .Calculate
  .SpecialCells(xlCellTypeVisible).Value = .SpecialCells(xlCellTypeVisible).Value
End With
 
Upvote 0
Thanks. Just tried it and all cells stay populated but value in every cell changes to 405. That was the value in the 1st cell in the selection. The rest of the cells should all be different values
 
Upvote 0
I'm stepping through the code and can see all the cells update with the formula and display correct values but then they all changed to 405 when i got to .SpecialCells(xlCellTypeVisible).Value = .SpecialCells(xlCellTypeVisible).Value
 
Upvote 0
I'm not sure, maybe try a simple copy and paste?
Code:
With ws.Cells(2, ColRef3).Resize(RwLast - 1)
  .FormulaR1C1 = "=TODAY()-RC[2]"
  .Calculate
  .Copy
  .PasteSpecial xlPasteValues
End With
 
Upvote 0
It won't paste because the column is filtered but I think i'll just take the filter off and copy and paste special the whole column. Looking on google it seems that everyone is looping through cells to do the same thing but i just find this so slow. Thank you for your help :)
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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