Macro help from Barrie or anyone please! :)
Posted by RoB on August 10, 2001 10:29 AM
Hi, Barrie was nice enough to write a macro for me that copies the user defined range and moves it to another sheet called "Returned Sort". I have made a few modifications, but I'm stuck on a few things. Before I ask the questions, I'll post the code.
Sub Button1_Click()
'Macro written by Barrie Davidson
'Variables used by the macro
Dim FilterCriteria
Dim ColumnFilterSelect
Dim CurrentSheetName As String
Dim RangeSelect As Range
'Defines Range Size <------make dynamic range? last cell with data will be the range selected?
'RangeSelect = "A1:F1000"
RangeSelect = Range("A1", Range("F65536").End(xlUp))
'Get the current file's name
CurrentSheetName = ActiveSheet.Name
'Select the defined range
Range(RangeSelect).Select
'Apply Autofilter
Selection.AutoFilter
'Asks the user to choose which column to sort by <----sort by first column headers?
ColumnFilterSelect = InputBox("Enter the Column Number you wish to Sort")
'Get the filter's criteria from the user
FilterCriteria = InputBox("Enter the Condition to Sort out:")
'Filter the data based on the user's input
'NOTE - this filter is on column A (field:=1), to change
'to a different column you need to change the field number
Selection.AutoFilter field:=ColumnFilterSelect, Criteria1:=FilterCriteria
'Select the visible cells (the filtered data)
Selection.SpecialCells(xlCellTypeVisible).Select
'Copy the cells
Selection.Copy
'Selects the "Returned Sort" Sheet which will have the results of the sort
Sheets("Returned Sort").Activate
'Selects the defined range<------ clear contents first?
'Range(RangeSelect).Select
'Selection.ClearContents
'Make sure you are in cell A1
Range("A1").Select
'Paste the copied cells
ActiveSheet.Paste
'Go to A1
Range("A1").Select
'Clear the clipboard contents
Application.CutCopyMode = False
'Go back to the original file
Sheets(CurrentSheetName).Activate
'Clear the autofilter
Selection.AutoFilter field:=1
'Take the Autofilter off
Selection.AutoFilter
'Go to A1
Range("A1").Select
End Sub
Ok, I have bolded the areas I have questions on.
First: I put a piece of code in to make the variable range "A1:F last cell with data" (i think i got this from Aladin). It worked in my other macro, but not here, maybe I'm using it incorrectly.
Second: The macro sorts the column Number the user puts in. Is there a way to have the user input a column header, and this way having the macro sort by this column instead of by column number?
Finally: Is there a way to clear the contents of the selected range at the end before it pastes? I tried putting a piece of code to do it, and it DID clear the contents, but then i got an error when the macro tried to paste.
If anyone can help me, i would really appreciate it. Thanks :)