Convert formulas into a code & get the extract

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello guys,
I have created this new app with the help of formulas. I need your expertise to get the result in the extract sheet as shown in the workbook by converting the formulas into code. The details are in the workbook Data sheet. Thank you in advance.
The date in column B is actually the starting Date to be posted first in the extract sheet.
Loading Google Sheets
 
Last edited:
You haven't stated how the other G column values should be handled ... 365, 24, 1, 7, 120
I just noticed this. For 365 or any number greater than 1 copy the formula column till 365 rows as mentioned. For 1 row do not do anything
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I still am not sure I understand all of what you are asking, but here goes the first swing at it:

VBA Code:
Sub Test()
'
    Dim DataDate                        As Date
    Dim DataFormulaColumnNumber         As Long, DataFormulaStartColumnNumber   As Long
    Dim DataLastRowColumnB              As Long
    Dim DataRow                         As Long
    Dim DataStartRowOfData              As Long
    Dim DateRow                         As Long
    Dim LastRowExtract                  As Long
    Dim MaxRowsToAdd                    As Long
    Dim NextParticularRow               As Long
    Dim DaysToAdd                       As Long, MonthsToAdd                    As Long, WeeksToAdd     As Long
    Dim NextRowExtract                  As Long
    Dim LastColumnNumberInDataSheetRow  As Long
    Dim DataDateColumn                  As String
    Dim DataFormulaStartColumn          As String, DataFormulaColumn            As String
    Dim DataNumberOfRowsColumn          As String
    Dim LastColumnInDataSheetRow        As String
    Dim ExtractAmountColumn             As String, ExtractDateColumn            As String
    Dim LastColumnInDataRow             As String, LastColumnInDataSheet        As String
    Dim wsData                          As Worksheet, wsExtract                 As Worksheet
'
            DataDateColumn = "B"                                                                                                ' <-- Set this to the DateColumn of sheet 'Data'
    DataFormulaStartColumn = "K"                                                                                                ' <-- Set this to the column letter where the formulas start
    DataNumberOfRowsColumn = "G"                                                                                                ' <-- Set this to the 'Number of Rows' column letter
        DataStartRowOfData = 2                                                                                                  ' <-- Set this to start row of data on the 'Data' sheet
       ExtractAmountColumn = "D"                                                                                                ' <-- Set this to the AmountColumn letter on sheet 'Extract'
         ExtractDateColumn = "B"                                                                                                ' <-- Set this to the DateColumn of sheet 'Extract'
              MaxRowsToAdd = 365                                                                                                ' <-- Set this to the maximum # of rows that will need to be added
'
    DataFormulaStartColumnNumber = Range(DataFormulaStartColumn & 1).Column                                                     ' Convert DataFormulaStartColumn to a number
'
       Set wsData = Sheets("Data")
    Set wsExtract = Sheets("Extract")
'
    With wsData
        LastColumnNumberInDataSheetRow = .Cells(DataStartRowOfData, .Columns.Count).End(xlToLeft).Column                        '   Get last column number used in the 'Data' sheet DataRow
'
        If LastColumnNumberInDataSheetRow >= DataFormulaStartColumnNumber Then                                                  '   If previous formulas exist on the 'Data' sheet then ...
            .Range(.Cells(DataStartRowOfData, DataFormulaStartColumnNumber), _
                    .Cells(MaxRowsToAdd + DataStartRowOfData - 1, LastColumnNumberInDataSheetRow)).ClearContents                '       Erase the previous formulas
        End If
'
        DataLastRowColumnB = .Range(DataDateColumn & .Rows.Count).End(xlUp).Row                                                 '   Get last row used in DataDateColumn of 'Data' sheet
'
        DataRow = DataStartRowOfData - 1                                                                                        '   Initialize DataRow
'
        For DataFormulaColumnNumber = DataFormulaStartColumnNumber To _
                DataFormulaStartColumnNumber + DataLastRowColumnB - DataStartRowOfData                                          '   Loop through columns of the columns to place the formulas
            DataRow = DataRow + 1                                                                                               '       Increment DataRow
'
            DataFormulaColumn = Split(Cells(1, DataFormulaColumnNumber).Address, "$")(1)                                        '       Convert DataFormulaColumnNumber to DataFormulaColumn
'
            .Range(DataFormulaColumn & DataStartRowOfData & ":" & DataFormulaColumn & _
                    .Range(DataNumberOfRowsColumn & DataRow).Value + 1) = "=IFERROR(MROUND(RANDBETWEEN(INDEX($D$1:$D$" & _
                    DataLastRowColumnB & "," & DataFormulaColumn & "$1),INDEX($E$1:$E$" & DataLastRowColumnB & _
                    "," & DataFormulaColumn & "$1)),INDEX($F$1:$F$" & _
                    DataLastRowColumnB & "," & DataFormulaColumn & "$1)),"""")"                                                 '       Formula to place into each formula column of 'Data' sheet
        Next                                                                                                                    '   Loop back
'
        LastColumnNumberInDataSheetRow = .Cells(DataStartRowOfData, .Columns.Count).End(xlToLeft).Column                        '   Get last column number used in the 'Data' sheet DataRow
'
        LastColumnInDataSheetRow = Split(Cells(.Cells(DataStartRowOfData, Columns.Count).End(xlToLeft).Column).Address, "$")(1) '
'
        .Range(DataFormulaStartColumn & DataStartRowOfData & ":" & LastColumnInDataSheetRow & _
                MaxRowsToAdd + DataStartRowOfData - 1).Copy                                                                     '   Copy formula range into memory (Clipboard)
        .Range(DataFormulaStartColumn & DataStartRowOfData & ":" & _
                LastColumnInDataSheetRow & MaxRowsToAdd + DataStartRowOfData - 1).PasteSpecial xlPasteValues                    '   Paste just the values back to range
        Application.CutCopyMode = False                                                                                         '   Clear clipboard & 'marching ants' around copied range
    End With
'
    LastColumnInDataSheet = Split(Cells(wsData.Cells.Find("*", , xlFormulas, , xlByColumns, _
            xlPrevious).Column).Address, "$")(1)                                                                                ' Get updated LastColumnInDataSheet after all formulas added
'
    With wsData.Range(DataFormulaStartColumn & DataStartRowOfData & ":" & _
            LastColumnInDataSheet & MaxRowsToAdd + DataStartRowOfData - 1)                                                      ' Format the range of formulas added to the 'Data' sheet
               .NumberFormat = "0.00"
        .HorizontalAlignment = xlLeft
          .VerticalAlignment = xlCenter
               .ReadingOrder = xlLTR
    End With
'
'---------------------------------------------------------------------------------------------------------------------------
'
    With wsExtract
        LastRowExtract = .Range(ExtractAmountColumn & .Rows.Count).End(xlUp).Row                                                '   Get last row used in ExtractAmountColumn of sheet 'Extract'
'
        If LastRowExtract > 1 Then                                                                                              '   If there are previous results then ...
            .Range("A2:" & ExtractAmountColumn & LastRowExtract).ClearContents                                                  '       Clear contents of previous results in sheet 'Extract'
        End If
'
        NextRowExtract = .Range(ExtractAmountColumn & .Rows.Count).End(xlUp).Row + 1                                            '   Get updated row # to start displaying data to
    End With
'
    DataRow = DataStartRowOfData - 1                                                                                            '   Initialize DataRow
'
    For DataFormulaColumnNumber = DataFormulaStartColumnNumber To DataFormulaStartColumnNumber + _
            DataLastRowColumnB - DataStartRowOfData                                                                             ' Loop through DataFormulaColumnNumbers
        DataRow = DataRow + 1                                                                                                   '   Increment DataRow
'
        DataFormulaColumn = Split(Cells(1, DataFormulaColumnNumber).Address, "$")(1)                                            '   Convert DataFormulaColumnNumber to DataFormulaColumn
'
        With Sheets("Data")
            .Range(DataFormulaColumn & DataStartRowOfData & ":" & DataFormulaColumn & .Range(DataFormulaColumn & _
                    .Rows.Count).End(xlUp).Row).Copy wsExtract.Range(ExtractAmountColumn & NextRowExtract)                      '       Write Formula values to ExtractAmountColumn
'
            LastRowExtract = wsExtract.Range(ExtractAmountColumn & wsExtract.Rows.Count).End(xlUp).Row                          '       Get last row used in ExtractAmountColumn of sheet 'Extract'
            NextParticularRow = wsExtract.Range("C" & wsExtract.Rows.Count).End(xlUp).Row + 1                                   '
            wsExtract.Range("C" & NextParticularRow & ":C" & LastRowExtract) = .Range("C" & DataRow)                            '       Copy particular to "Particular' column on sheet 'Extract'
'
            DataDate = .Range(DataDateColumn & DataRow)                                                                         '       Set DataDate = the date for the range
            DateRow = NextParticularRow - 1                                                                                     '
'
            Select Case .Range(DataNumberOfRowsColumn & DataRow)
                Case Is = 1: wsExtract.Range(ExtractDateColumn & DateRow + 1) = DataDate                                        '           If DataNumberOfRowsColumn value = 1 then just write date
                Case Is = 12                                                                                                    '           If DataNumberOfRowsColumn value = 12 then ...
                    For MonthsToAdd = 0 To 11                                                                                   '               Loop through # of Months to add
                        DateRow = DateRow + 1                                                                                   '                   Increment DateRow
                        wsExtract.Range(ExtractDateColumn & DateRow) = DateAdd("m", MonthsToAdd, DataDate)                      '                   Write result to ExtractDateColumn
                    Next                                                                                                        '               Loop back
                Case Is = 24                                                                                                    '           If DataNumberOfRowsColumn value = 24 then ...
                    For MonthsToAdd = 0 To 11                                                                                   '               Loop through # of Months to add
                        DateRow = DateRow + 1                                                                                   '                   Increment DateRow
                        wsExtract.Range(ExtractDateColumn & DateRow) = DateAdd("m", MonthsToAdd, DataDate)                      '                   Write result to ExtractDateColumn
                    Next                                                                                                        '               Loop back
'
                    DataDate = DateAdd("d", 15, DataDate)                                                                       '               Add 15 days to the DataDate
'
                    For MonthsToAdd = 0 To 11                                                                                   '               Loop through # of Months to add
                        DateRow = DateRow + 1                                                                                   '                   Increment DateRow
                        wsExtract.Range(ExtractDateColumn & DateRow) = DateAdd("m", MonthsToAdd, DataDate)                      '                   Write result to ExtractDateColumn
                    Next                                                                                                        '               Loop back

                Case Is = 52                                                                                                    '           If DataNumberOfRowsColumn value = 52 then ...
                    For WeeksToAdd = 0 To 51                                                                                    '               Loop through # of Weeks to add
                        DateRow = DateRow + 1                                                                                   '                   Increment DateRow
                        wsExtract.Range(ExtractDateColumn & DateRow) = DateAdd("ww", WeeksToAdd, DataDate)                      '                   Write result to ExtractDateColumn
                    Next                                                                                                        '               Loop back
'
                Case Is = 104                                                                                                   '           If DataNumberOfRowsColumn value = 104 then ...
                    For WeeksToAdd = 0 To 51                                                                                    '               Loop through # of Weeks to add
                        DateRow = DateRow + 1                                                                                   '                   Increment DateRow
                        wsExtract.Range(ExtractDateColumn & DateRow) = DateAdd("ww", WeeksToAdd, DataDate)                      '                   Write result to ExtractDateColumn
                    Next                                                                                                        '               Loop back
'
                    DataDate = DateAdd("d", 4, DataDate)                                                                        '               Add 4 days to the DataDate
'
                    For WeeksToAdd = 0 To 51                                                                                    '               Loop through # of Weeks to add
                        DateRow = DateRow + 1                                                                                   '                   Increment DateRow
                        wsExtract.Range(ExtractDateColumn & DateRow) = DateAdd("ww", WeeksToAdd, DataDate)                      '                   Write result to ExtractDateColumn
                    Next                                                                                                        '               Loop back
'
                Case Is = 365                                                                                                   '           If DataNumberOfRowsColumn value = 365 then ...
                    For DaysToAdd = 0 To 364                                                                                    '               Loop through # of Days to add
                        DateRow = DateRow + 1                                                                                   '                   Increment DateRow
                        wsExtract.Range(ExtractDateColumn & DateRow) = DateAdd("d", DaysToAdd, DataDate)                        '                   Write result to ExtractDateColumn
                    Next                                                                                                        '               Loop back
            End Select
        End With
'
        NextRowExtract = wsExtract.Range(ExtractAmountColumn & wsExtract.Rows.Count).End(xlUp).Row + 1                          '   Get updated row # to start displaying data to
    Next
'
    With wsExtract.Range("A" & DataStartRowOfData & ":A" & NextRowExtract - 1)
        .Value = wsExtract.Evaluate("Row(" & .Address & ") - " & DataStartRowOfData & "+ 1")                                    '   Write the line #s to column A of sheet 'Extract'
    End With
'
    wsExtract.UsedRange.EntireColumn.AutoFit                                                                                    ' Autofit the widths of the columns on sheet 'Extract'
End Sub
 
Upvote 0
Solution
:eek: OMG!! It is possible. You have understood it perfectly 👏 👏 . Going through to find at least one issue if any;).
 
Upvote 0
JohnnyL It is Amazing. It is just perfect man. All that I posted in the query is almost done. Here is some more editing to be done to complete it. I have explained that in the extract sheet of this workbook.
I have recreated one module from your old applications and added that in the workbook. I want to combine both the codes to make it run with a single button. When I run my code separately, it is working good.
2Get extract in new sheet .xlsm
 
Upvote 0
On the news I heard about the hurricane IVAN hitting Florida and other states at 270 km/h. I hope you are safe man. Stay safe. Will see you tonight.
 
Upvote 0
I just noticed that in the Data sheet the first row result in column L is missing. It is starting with the 3rd row. Hence, it is not displaying the amounts in the Extract sheet. of the 2nd row.
 
Last edited:
Upvote 0
Going through the comments in your code, I think the problem might be in this line. I am not sure.
Rich (BB code):
        DataRow = DataStartRowOfData - 1                                                                                        '   Initialize DataRow
 
Upvote 0
I just noticed that in the Data sheet the first row result in column L is missing. It is starting with the 3rd row. Hence, it is not displaying the amounts in the Extract sheet. of the 2nd row.
I think you need to check again
 
Upvote 0
I think the rows to extract is interchanged.
Untitled.png
 
Upvote 0

Forum statistics

Threads
1,216,172
Messages
6,129,291
Members
449,498
Latest member
Lee_ray

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