Modify code

dougebowl

Board Regular
Joined
Feb 22, 2010
Messages
55
I recorded the Macro below and it works great. But, the issue I am seeking help with is from day to day the range of data will change. So, I am looking for help in modifying the code so that the range is not hard-coded, but, for the Macro to determine the last row of the data. In the end, the number of columns will not change, they will be A:D. The number of rows needs to be dynamic as each time the report the code is being applied to is pulled, there will be a different number of rows.

Thanks in advance for any assistance.

Current Code:

VBA Code:
Sub LRRValidationMBP()
'
' LRRValidationMBP Macro
'
 
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "Count"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "Concatenation"
    Range("C2").Select
    ActiveCell.FormulaR1C1 = "=IF(RC[-2]=R[-1]C[-2],R[-1]C+1,""1"")"
    Range("C2").Select
    Selection.AutoFill Destination:=Range("C2:C2000")
    Range("C2:C2000").Select
    Range("D2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(RC[-3]=R[-1]C[-3],CONCATENATE(R[-1]C,"";"",RC[-2]),RC[-2])"
    Range("D2").Select
    Selection.AutoFill Destination:=Range("D2:D2000")
    Range("D2:D2000").Select
    Columns("C:D").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Columns("A:D").Select
    Application.CutCopyMode = False
    ActiveWorkbook.Worksheets("MBP_-_LRR_Validation_post_Impor").Sort.SortFields. _
        Clear
    ActiveWorkbook.Worksheets("MBP_-_LRR_Validation_post_Impor").Sort.SortFields. _
        Add Key:=Range("A2:A2000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("MBP_-_LRR_Validation_post_Impor").Sort.SortFields. _
        Add Key:=Range("C2:C2000"), SortOn:=xlSortOnValues, Order:=xlDescending _
        , DataOption:=xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("MBP_-_LRR_Validation_post_Impor").Sort
        .SetRange Range("A1:D2000")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
    MsgBox ("Woohoo, all done!")
End Sub
 

dougebowl

Board Regular
Joined
Feb 22, 2010
Messages
55
Yes, the sheet is called what you have referenced above and that is indeed the sheet I am trying to sort. I can rename the sheet if that is what needs to be done.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,664
Office Version
  1. 365
Platform
  1. Windows
In that case I don't know why you are getting a subscript out of range error.
 

dougebowl

Board Regular
Joined
Feb 22, 2010
Messages
55
Thank you for your patience and all the help. I did double-check the sheet name and it was, in fact, different. I modified the code to use the Active Sheet instead of a specific sheet name and viola, it worked perfectly. I really do appreciate all the assistance as I continue to learn VBA.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,664
Office Version
  1. 365
Platform
  1. Windows
Glad you sorted it & thanks for the feedback.
 

Forum statistics

Threads
1,147,632
Messages
5,742,232
Members
423,714
Latest member
ftp2jz

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
Top