Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88081

Editing a recorded macro to filter for 3 variables

$
0
0

I want to preface this by saying I am fairly new to VBA and I'm open to better approaches this code. I had an initial process, where I would filter values in an array based on conditions (date, money group and measure) and paste into another sheet. I would then run code to copy these values based on conditions and paste into a separate worksheet.

I now want to eliminate the manual bit where I filter the stuff myself and paste into another sheet. I tried to record a macro on the manual process that I use and my plan was to insert a sheet of the same name so that the remainder of the initial code would work as normal.The problem is that I want to read the dates on the worksheet that I am pasting into, use these as variables and then filter my values on the worksheet I am getting data from and pasting into another sheet. Here is the code with comments:

Sub Actual()

Dim rw As Integer
Dim z As Integer
Dim i As Integer
Dim rpt_nm As String
Dim wb As Workbook: Set wb = ThisWorkbook
Dim wb2 As Workbook
Dim dt1, dt2, dt3 As Date
Dim ws As Worksheet
Dim TAmt, VAmt, UAmt, OAmt As Double

Worksheets("PAct").Range("G1").Activate

Let rw = ActiveCell
Let rpt_nm = Range("k1").Value
Let dt1 = Cells(rw, 1)
Let dt2 = Cells(rw + 1, 1)
Let dt3 = Cells(rw + 2, 1)

' Workbooks.Open Filename:=ThisWorkbook.Path & "\2. 2019 Legacy.xlsx"
Set wb2 = Workbooks.Open(Filename:=ThisWorkbook.Path & rpt_nm)
With wb2
    .Sheets.Add After:=.Sheets("Actual Input").Name = "VBA Input"
End With

'This is the macro I recorded in sheet I am trying to copy from
Sheets("Actual Input").Activate
Columns("E:E").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("E2").Select
ActiveCell.FormulaR1C1 = _
                 "=IF(LEN(RC[-1])=9,DATE(RIGHT(RC[-1],4),LEFT(RC[-1],1),MID(RC[-1],3,2)),DATE(RIGHT(RC[-1],4),LEFT(RC[-1],2),MID(RC[-1],4,2)))"
Range("E2").Select         'Formatting on previous lines was my clumsy attempt to try and change text formats to dates using excel
Selection.AutoFill Destination:=Range("E2:E721")
Range("E2:E721").Select
Selection.Copy
Range("E2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                 :=False, Transpose:=False
Range("D1").Select
Application.CutCopyMode = False
Selection.Copy
Range("E1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Columns("D:D").Select
Selection.Delete Shift:=xlToLeft
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$H$721").AutoFilter Field:=7, Criteria1:="Net"
****ActiveSheet.Range("$A$1:$H$721").AutoFilter Field:=4, Operator:= _
                 xlFilterValues, Criteria2:=Array(1, "7/31/2019", 1, "8/31/2019", 1, "9/30/2019") 'This is the place where I want to remove the specific dates selected and use variables
ActiveSheet.Range("$A$1:$H$721").AutoFilter Field:=3, Criteria1:="5M="‘Everytime I try and replace these values with dt1, dt2 and dt3 the filter will not select anything and there is no data****
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveSheet.Next.Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                 :=False, Transpose:=False
Range("I3").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-1]/1000000"
Range("I3").Select
Selection.AutoFill Destination:=Range("I3:I17")
Range("I3:I17").Select
ActiveSheet.Previous.Select
ActiveSheet.Range("$A$1:$H$721").AutoFilter Field:=3, Criteria1:="1M-4.99M"
Selection.Copy
ActiveSheet.Next.Select
Range("K2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                 :=False, Transpose:=False
Range("S3").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-1]/1000000"
Range("S3").Select
Selection.AutoFill Destination:=Range("S3:S17")
Range("S3:S17").Select
ActiveSheet.Previous.Select
ActiveSheet.Range("$A$1:$H$721").AutoFilter Field:=3, Criteria1:="1M<"
Selection.Copy
ActiveSheet.Next.Select
Range("U2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                 :=False, Transpose:=False
ActiveWindow.SmallScroll ToRight:=4
Range("AC3").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-1]/1000000"
Range("AC3").Select
Selection.AutoFill Destination:=Range("AC3:AC17")
Range("AC3:AC17").Select
'This is where the macro recorder ends

For z = 0 To 2
    For i = 0 To 2

        wb2.Worksheets("VBA Input").Activate

        VAmt = Cells(3 + (i * 5), 9 + (z * 10))
        UAmt = Cells(4 + (i * 5), 9 + (z * 10)) + Cells(5 + (i * 5), 9 + (z * 10))
        TAmt = Cells(6 + (i * 5), 9 + (z * 10))
        OAmt = Cells(7 + (i * 5), 9 + (z * 10))

        wb.Worksheets("PAct").Activate
        Cells(rw + i, 16 + (z * 5)) = TAmt
        Cells(rw + i, 17 + (z * 5)) = VAmt
        Cells(rw + i, 18 + (z * 5)) = UAmt
        Cells(rw + i, 19 + (z * 5)) = OAmt

    Next
Next

End Sub

Again fairly new to VBA so if you think there's a better approach (particularly in regards to the recorded macro) please let me know and I can just figure it out myself. Just thought it would be good to learn more about recording macros. Thanks


Viewing all articles
Browse latest Browse all 88081

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>