VBA Code Help

ststern45

Well-known Member
Joined
Sep 17, 2005
Messages
974
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I have a list of 324 sets of data in columns that are identical in size

I tried using Record Macro then changing a few lines of code but received errors on a few lines of code.
The new lines of code are for adjusted cell columns,

Range(aRng & Range("AXO2").Value & ":" & aRng & Range("AXO3").Value)

The code below is the Record Macro
Sub SortDelete()

Range("BL2:BN326").Select
ActiveWorkbook.Worksheets("FLRLSets").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("FLRLSets").Sort.SortFields.Add Key:=Range( _
"BN2:BN326"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("FLRLSets").Sort
.SetRange Range("BL2:BN326")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("BL2:BN131").Select
Selection.Delete Shift:=xlUp
Range("AXQ1").Select
End Sub

===========================================================================
This is the code I tried to use:

Sub SortDelete1()

Dim aRng As String

aRng = Sheets("FLRLSets").Range("AXO1").Value

With ActiveSheet
For Each cll In .Range(aRng & Range("AXO2").Value & ":" & aRng & Range("AXO3").Value).Cells

.Range("AXO4").Value = cll.Value


SortDelete2


Next cll
End With

End Sub


Sub SortDelete2()

Dim aRng As String
Dim bRng As String


Application.ScreenUpdating = False

aRng = Sheets("FLRLSets").Range("AXO5").Value
bRng = Sheets("FLRLSets").Range("AXO7").Value

Sheets("FLRLSets").Select

Range(aRng & Range("AXM2").Value & ":" & bRng & Range("AXM3").Value).Select

ActiveWorkbook.Worksheets("FLRLSets").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("FLRLSets").Sort.SortFields.Add Key:=Range( _

"aRng & Range("AXM2").Value & ":" & bRng & Range("AXM3").Value"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ "this line gives an error
xlSortNormal
With ActiveWorkbook.Worksheets("FLRLSets").Sort
.SetRange Range("AXM2").Value & ":" & bRng & Range("AXM3").Value") 'this line of code gives an error
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply 'This line gives an error
End With

Range(aRng & Range("AXO8").Value & ":" & bRng & Range("AXO9").Value).Select
Selection.Delete Shift:=xlUp


Sheets("FLRLSets").Select
Range("AXQ1").Select

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Note the difference in structure between this:
Code:
[COLOR=#333333]Range(aRng & Range("AXO2").Value & ":" & aRng & Range("AXO3").Value)[/COLOR]
and this:
Code:
[COLOR=#333333]Range("AXM2").Value & ":" & bRng & Range("AXM3").Value")[/COLOR]
Your second value is just a string, not a range (because you are not enclosing the whole thing in Range(...), like you do the first. Note the parentheses, and how quickly you close out the first range reference.
Your other errored line has a similar issue.

Give it a try fixing it up, and see if you can get it. If not, post back.
 
Last edited:
Upvote 0
OK Thanks for all your help. I changed the code and worked.

Thanks you!!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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