Add formulas to macro

pwill

Active Member
Joined
Nov 22, 2015
Messages
406
Hi can anyone help with adding formula's to a macro?

I copy data with a Userform from columns ("A2:A" & ", D2:K) to columns ("AI2:AQ)

Column A2:A has Dates
Column D2:D has count values from 1 -12000
Columns E2:K has Data

I am try to find a way of adding formula's to columns ("AI2:AQ) after data has been copied.

ie
If I have copied data from ("A11" & ", D11:K11") to columns ("AI2:AQ2)
Column "AJ2" value would be 10
Then add formula's -
Column "AI2" = "A11"
Column "AJ2" = "D11"
Colums "AK2:AQ2" = "E11:K11"

And then with the data in cells "AK2:AQ2" a formula to arrange the data small to large.
So if the data copied from cells "E2:K2" ie is (5,3,6,7,1,2,4) the formula arranges the data in cells "AI2:AQ2" from (5,3,6,7,1,2,4) to (1,2,3,4,5,6,7)

Any help would be appreciated

Regards
pwill


Edit: Sorry I should have said Sheet1
The code I am using was kindly provided by Roderick_E in another thread 'User Form to Copy Rows
 
Last edited by a moderator:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Re: Add formula's to macro

Just thought I'd say, the code Roderick_E provided for the Userform works fine and is used to quickly add a range of data. I have added +1 to the startrow and -1 to the endrow in the code to adjust the rows.

The reason for adding formula's at the end of the code (if possible? ) is to be able to auto fill down a few rows if need be? rather than have to keep opening the Userform, just to add a few more rows.

Regards

pwill
 
Upvote 0
Re: Add formula's to macro

Hi can anyone help with this? I was using my phone when I post this thread and was limited to text.

Hear is an example of Sheet1 with the data before copying Rows with the UserForm.

Columns "A2:A" & ", D2:K" have the raw Data
Column "AI2" has a formula (=A2)
Column "AJ2" has a formula (=D2)
Columns "AK2:AQ2" have an array formula '=SMALL(E2:K2,{1,2,3,4,5,6,7})'

And then drag down to autofill the rows "AI2:AQ22"

My problem is when I copy rows with the UserForm the formulas get wiped out in columns "AI:AQ" and I need to put them back with the correct row reference as the data range copied with the UserForm could be anything from 1 to 12000.

ABCDEFGHIJKLMAGAHAIAJAKALAMANAOAPAQ
1Date/YearCntResultsDate/YearCntAscending
201/04/18110340429481201/04/181391012404248
301/04/182301261712283701/04/1826121217283037
402/04/18325304344831202/04/183341225303448
502/04/1841542112362192002/04/18411151920234262
603/04/1855568198242003/04/18545819205682
703/04/18648202642471303/04/1864.4281320264247
804/04/187151415243447304/04/1873141515243447
904/04/1882526313839474604/04/18825263138394647
1005/04/1893234334135475405/04/18932333435414754
1105/04/18104343394734477005/04/181033.838.54343474770
1206/04/18115452445332478706/04/181132.3444752535487
1306/04/1812656150593147406/04/1812430.84750596165
1407/04/1813767055652947807/04/1813829.34755657076
1507/04/1814877961712847607/04/1814627.84761717987
1608/04/1815102224324749908/04/18159102224324749
1708/04/18162313233448408/04/181623413233448
1809/04/1817577864249409/04/18174.3356.3337424978
1909/04/1818567939104650309/04/18183.33103946505679
2010/04/18195472662752501010/04/1819102750.3352546672
2110/04/1820688977445851310/04/18202.894451.0858687789
2211/04/1821000000011/04/18210000000
23
24
25

<tbody>
</tbody>


Here is the code for the UserForm

Code:
Private Sub cmdbOk_Click()
Call CopyRows
End Sub
Private Sub UserForm_Initialize()
    Me.Height = 174.75
End Sub
Sub CopyRows()
    Dim Sht1 As Worksheet: Set Sht1 = Worksheets("Sheet1")
    Dim startrow As Long
    Dim endrow As Long
    Dim lRowAI As Long
    Dim cnt As Long
    Dim x As Long
    
        lRowAI = Sht1.Cells(Rows.Count, "AI").End(xlUp).Row
        
            With Sht1
                .Range("AI2:AQ" & lRowAI).ClearContents
            End With
        startrow = Int(UserForm1.TextBox1.Value) + 1
        endrow = Int(UserForm1.TextBox2.Value) - 1
    
            If startrow < 1 Or endrow < 1 Then
                MsgBox "Invalid start or end rows", vbCritical, "ALERT"
                Exit Sub
            End If
            
        'indicate first row where data should copy (could use lastrow+1 of destination column if you want to keep appending)
        cnt = 2
        endrow = startrow + endrow
    
        For x = startrow To endrow
            Cells(cnt, "AI") = Cells(x, "a")
            Cells(cnt, "AJ") = Cells(x, "d")
            Cells(cnt, "AK") = Cells(x, "e")
            Cells(cnt, "AL") = Cells(x, "f")
            Cells(cnt, "AM") = Cells(x, "g")
            Cells(cnt, "AN") = Cells(x, "h")
            Cells(cnt, "AO") = Cells(x, "i")
            Cells(cnt, "AP") = Cells(x, "j")
            Cells(cnt, "AQ") = Cells(x, "k")
            
            cnt = cnt + 1
        Next x
        
End Sub


Any help would be appreciated

regards

pwill
 
Last edited:
Upvote 0
Re: Add formula's to macro

I have managed to find myself a solution to my problem by keeping "AI2:AQ2" Row in play with the formulas.

I have the UserForm copy the Rows to start at "AI3:AQ3".
Allowing to AutoFill back Down with the formulas from in "AI2:AQ2" when finished with the Data added from the UserForm.
I have also added code to sort the rows in ascending order left to right in columns "AK:AQ" for the Rows added with the UserForm.

New Code for the UserForm as -

Code:
Private Sub cmdbOk_Click()
Call CopyRows
End Sub
Private Sub UserForm_Initialize()
    Me.Height = 174.75
End Sub
Sub CopyRows()
    Dim Sht1 As Worksheet: Set Sht1 = Worksheets("Sheet1")
    Dim startrow As Long
    Dim endrow As Long
    Dim lRowAI As Long
    Dim cnt As Long
    Dim x As Long
    
        lRowAI = Sht1.Cells(Rows.Count, "AI").End(xlUp).Row
        
        Range("AI3:AQ" & lRowAI).ClearContents
        startrow = Int(UserForm1.TexBox1.Value) + 1
        endrow = Int(UserForm1.TexBox1.Value) - 1
    
            If startrow < 1 Or endrow < 1 Then
                MsgBox "Invalid start or end rows", vbCritical, "ALERT"
                Exit Sub
            End If
            
        lRowAI = Sht1.Cells(Rows.Count, "AI").End(xlUp).Row
            
        'indicate first row where data should copy (could use lastrow+1 of destination column if you want to keep appending)
        cnt = 3
        endrow = startrow + endrow
    
        For x = startrow To endrow
            Cells(cnt, "AI") = Cells(x, "a")
                Cells(cnt, "AJ") = Cells(x, "d")
                    Cells(cnt, "AK") = Cells(x, "e")
                        Cells(cnt, "AL") = Cells(x, "f")
                            Cells(cnt, "AM") = Cells(x, "g")
                        Cells(cnt, "AN") = Cells(x, "h")
                    Cells(cnt, "AO") = Cells(x, "i")
                Cells(cnt, "AP") = Cells(x, "j")
            Cells(cnt, "AQ") = Cells(x, "k")
            
            cnt = cnt + 1
        Next x
        
        Call SortRows
        
End Sub
Sub SortRows()
        
    Dim iRow As Long
    Dim FirstRow As Long
    Dim LastRow As Long
        
       With ActiveSheet
            FirstRow = 3 'change to 2 if there are headings
            LastRow = .Cells(.Rows.Count, "AK").End(xlUp).Row
            For iRow = FirstRow To LastRow
                With .Cells(iRow, "AK").Resize(1, 7)
                    .Sort Key1:=.Columns(1), _
                    Order1:=xlAscending, _
                    Header:=xlNo, _
                    OrderCustom:=1, _
                    MatchCase:=False, _
                    Orientation:=xlLeftToRight
                End With
            Next iRow
        End With
End Sub

So if the Data on Sheet1 "AI2:AQ" with formulas before copying new Rows was -

Columns "A2:A" & ", D2:K" have the raw Data
Column "AI2" has a formula (=A18)
Column "AJ2" has a formula (=D18)
Columns "AK2:AQ2" have an array formula '=SMALL(E18:K18,{1,2,3,4,5,6,7})'

And then drag down from "AI2:AQ2" to autofill the rows "AI2:AQ15"

ie

ABCDEFGHIJKLM AGAHAIAJAKALAMANAOAPAQ
1Date/Year Cnt Results Date/YearCnt Ascending
228/03/18 1140139138137136135134 05/04/181722232425262728
328/03/18 2133132131130129128127 05/04/181815161718192021
429/03/18 3126125124123122121120 06/04/1819891011121314
529/03/18 4119118117116115114113 06/04/18201234567
630/03/18 5112111110109108107106 07/04/18211234567
730/03/18 610510410310210110099 07/04/18221234567
831/03/18 798979695949392 08/04/18231234567
931/03/18 891908988878685 08/04/18241234567
1001/04/18 984838281807978 09/04/18251234567
1101/04/18 1077767574737271 09/04/18261234567
1202/04/18 1170696867666564 10/04/18271234567
1302/04/18 1263626160595857 10/04/18281234567
1403/04/18 1356555453525150 11/04/18291234567
1503/04/18 1449484746454443 11/04/18300000000
1604/04/18 1542414039383736
1704/04/18 1635343332313029
1805/04/18 1728272625242322
1905/04/18 1821201918171615
2006/04/18 19141312111098
2106/04/18 207654321
2207/04/18 217654321
2307/04/18 227654321
2408/04/18 237654321
2508/04/18 247654321
2609/04/18 257654321
2709/04/18 267654321
2810/04/18 277654321
2910/04/18 287654321
3011/04/18 297654321
3111/04/18 300000000

<colgroup><col><col><col><col><col><col span="5"><col><col><col><col><col span="6"><col><col><col><col><col span="5"></colgroup><tbody>
</tbody>
 
Upvote 0
Re: Add formula's to macro

Just thought someone might find it useful?

Regards

pwill
 
Upvote 0
Re: Add formula's to macro

nice work pwill. Sorry I couldn't help in time. I'm off on weekends.
 
Upvote 0
Re: Add formula's to macro

O que tem de errado na minha macro

Option Explicit


Sub GeraNovaPlanilha()
ActiveCell.Value = ActiveCell.Value


Sheets("ATUAL").Select
Sheets("ATUAL").Copy After:=Sheets(1)
Range("R9:S9").Select
Sheets("ATUAL (2)").Select
Sheets("ATUAL (2)").Name = ActiveCell.Value
ActiveSheet.Shapes.Range(Array("Button 94")).Select
Selection.Delete
Sheets("ATUAL").Select
Range("C9:P9").Select
Selection.ClearContents
Range("R9:S9").Select
Selection.ClearContents
Range("V9:W9").Select
Selection.ClearContents
Range("F10:P10").Select
Selection.ClearContents
Range("D11:G11").Select
Selection.ClearContents
Range("K11:W11").Select
Selection.ClearContents
Range("D13:W13").Select
Range("c9:p9").Select

End Sub
 
Upvote 0
Re: Add formula's to macro

re: Add formula's to macro

i have managed to find myself a solution to my problem by keeping "ai2:aq2" row in play with the formulas.

I have the userform copy the rows to start at "ai3:aq3".
Allowing to autofill back down with the formulas from in "ai2:aq2" when finished with the data added from the userform.
I have also added code to sort the rows in ascending order left to right in columns "ak:aq" for the rows added with the userform.

New code for the userform as -


change line in red:

Rich (BB code):
From-
startrow = int(userform1.texbox1.value) + 1
endrow = int(userform1.texbox1.value) - 1
To-
startrow = int(userform1.texbox1.value) + 1
endrow = int(userform1.texbox2.value) - 1
<strike></strike><strike></strike>


Rehards
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,960
Latest member
AKSMITH

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