VBA to Copy/Paste Formula Into Next Cell (Column)

Gerwe1988

New Member
Joined
Mar 21, 2022
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
Hello everyone,

I'm new to macros and need some assistance with establishing a code for my forecast file. The macro will scan row 3 for the current "reporting" month, to decide which column to update (AS). The macro will then copy & paste the prior months formula from one cell to next, into the current "reporting" month. The prior months formula will need to be hard coded. There are certain rows that need to be updated in this process (Rows 16,17, 27, 28). For example, row 16 "New patients", the prior months formula (Jan-22, AR16), will be copied and pasted into the next column (AS16). The prior months formula (AR16) will be hard coded. Thanks in advance!
 

Attachments

  • Example.JPG
    Example.JPG
    97.9 KB · Views: 21

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
many are willing to help if you could give us sample data to work with preferably in xl2bb format, formulas and possible output (if you may) :)
 
Upvote 0
many are willing to help if you could give us sample data to work with preferably in xl2bb format, formulas and possible output (if you may) :)
ADS Forecast - 2021 KLF_temp(AutoRecovered)_check.xlsm
BCDEFGHIJK
1
2
3 Reporting
4ActActActACTACTForeForeForeFore
5Oct-21Nov-21Dec-21Jan-22Feb-22Mar-22Apr-22May-22Jun-22
6Patient Assumptions
7Number of Sales Reps272726262626262626
8New Patients / Rep / Month1081091348893889596104
9Inbound Marketing Patients (manual add)
10Inbound Med Adv (manual add)350350350350
11Inbound Pharmacy Patients (manual add)270260231358231298341286324
12Churn Rate-1.5%-1.6%-1.7%-1.8%-1.7%-1.7%-1.7%-1.7%-1.7%
132,2822,4092,2842,4642,4862,702
14Patient Build
15Beginning Population45,57248,07950,54053,38155,07056,75958,71160,85262,924
16New Patients3,1993,2083,7202,6402,6402,9323,1553,1233,376
17Churn(692)(747)(879)(951)(951)(980)(1,014)(1,051)(1,087)
18Ending Patient Population48,07950,54053,38155,07056,75958,71160,85262,92465,213
1935,058
20Ending Patients by Payor
21MEDICARE33,66034,87536,11135,02436,73538,44240,30442,09544,061
22OTHER14,41915,66517,27014,63518,33518,57918,85919,14019,463
23
24Sensors
25
26New Units by Payor Group
27MEDICARE10,91311,11311,3487,7487,748----
28OTHER8,4218,03010,2307,8797,879----
Sheet1
Cell Formulas
RangeFormula
F3:G3F3=IF(F5=Cover!$C$41,"Reporting"," ")
F5:G5F5=EOMONTH(E5,1)
F7:G7F7='Dexcom CGM'!AR7
F8:G8F8=(F16-F11)/F7
F12:G12F12=IFERROR(F17/F15,"n/a")
F13:G13F13=F7*F8
F15:G15F15=E18
G16G16=SUMIF('Attrition Support (Libre)'!$A:$A,"<>"&"*Grand Total*",'Attrition Support (Libre)'!$J:$J)
G17G17=-SUMIF('Attrition Support (Libre)'!$A:$A,"<>"&"*Grand Total*",'Attrition Support (Libre)'!$K:$K)
F18:G18F18=SUM(F15:F17)
G27G27=SUMIFS('Attrition Support (Libre)'!$O:$O,'Attrition Support (Libre)'!$A:$A,"<>"&"*Grand Total*",'Attrition Support (Libre)'!$H:$H,"*Medicare*")
G28G28=SUMIFS('Attrition Support (Libre)'!$O:$O,'Attrition Support (Libre)'!$A:$A,"<>"&"*Grand Total*",'Attrition Support (Libre)'!$H:$H,"*Other*")
Named Ranges
NameRefers ToCells
'Attrition Support (Libre)'!_FilterDatabase='Attrition Support (Libre)'!$A$2:$P$1395G27:G28, G16:G17
 
Upvote 0
there are missing sheet and the formula to copy is not that clear, but kindly try the following code if it helps...

VBA Code:
Sub Gerwe1988()
    
    Dim rFind As Range, iCharNum As Integer, LColChar As String
    With Range("A3:J3")
        Set rFind = .Find(What:="Reporting", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
        If Not rFind Is Nothing Then
           iCharNum = rFind.Column
            LColChar = Split(Cells(1, iCharNum).Address, "$")(1)
        End If
    End With
    Range("AR8").Formula = Range(Last_Char00 & "8").Formula
    Range("AR13").Formula = Range(Last_Char00 & "13").Formula
    Range("AR15").Formula = Range(Last_Char00 & "15").Formula
    Range("AR18").Formula = Range(Last_Char00 & "18").Formula
    
End Sub     'End of Gerwe1988
 
Upvote 0
Updated Code [TESTED]
VBA Code:
Sub Gerwe1988()
    
    Dim rFind As Range, iCharNum As Integer, LColChar As String
    With Range("A3:J3")
        Set rFind = .Find(What:="Reporting", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
        If Not rFind Is Nothing Then
           iCharNum = rFind.Column
            LColChar = Split(Cells(1, iCharNum).Address, "$")(1)
        End If
    End With
    Range("AR8").Formula = Range(LColChar & "8").Formula
    Range("AR13").Formula = Range(LColChar & "13").Formula
    Range("AR15").Formula = Range(LColChar & "15").Formula
    Range("AR18").Formula = Range(LColChar & "18").Formula
    
End Sub     'End of Gerwe1988
 
Upvote 0
Updated Code [TESTED]
VBA Code:
Sub Gerwe1988()
   
    Dim rFind As Range, iCharNum As Integer, LColChar As String
    With Range("A3:J3")
        Set rFind = .Find(What:="Reporting", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
        If Not rFind Is Nothing Then
           iCharNum = rFind.Column
            LColChar = Split(Cells(1, iCharNum).Address, "$")(1)
        End If
    End With
    Range("AR8").Formula = Range(LColChar & "8").Formula
    Range("AR13").Formula = Range(LColChar & "13").Formula
    Range("AR15").Formula = Range(LColChar & "15").Formula
    Range("AR18").Formula = Range(LColChar & "18").Formula
   
End Sub     'End of Gerwe1988

Appreciate the help! I only need to copy and paste the formulas in 16, 17, 27, 28. I'm receiving the following error when I run the macro.
 

Attachments

  • Error.JPG
    Error.JPG
    17.9 KB · Views: 5
Upvote 0
Appreciate the help! I only need to copy and paste the formulas in 16, 17, 27, 28. I'm receiving the following error when I run the macro.
can you tell me what line is the problem, on my end its working properly.
and could you follow "Fix 2" on this link Runtime Error Fix, might help.
 
Upvote 0
kindly try my sample file based on your data Sample Worksheet (in Macro Enabled Format)
Works with your file. How would you specify a sheet or tab within this code? Is it possible to paste the code in the next column (month), rather than a specific location or cell? Also, how would you copy, paste values (hardcode) the reporting month column so that the formula isn't active in two columns?
 
Upvote 0

Forum statistics

Threads
1,215,479
Messages
6,125,041
Members
449,206
Latest member
Healthydogs

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