I have data from A2 to K100. How can I have VBA copy the entire row (A to K) when the values in Col K is 1 or 2. To paste in another sheet as values. "no error msg required"
Thanks
I'm finally going for the below in the destination sheet. However, how can I resolve the issue with Range("A16").select
Selection.AutoFilter
ActiveWorkbook.Worksheets("List of attendees").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("List of attendees").AutoFilter.Sort.SortFields.Add2 _
Key:=Range("H1:H300"), SortOn:=xlSortOnValues, Order:=xlAscending, _DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("List of attendees").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveSheet.Range("$A$1:$I$300").AutoFilter Field:=8, Criteria1:="="
Range("A16").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("D1").Select
Selection.AutoFilter
Related
I am trying to sort the Buy column from Largest to smallest then copy and paste into another column, then I will sort the Sell column from largest to smallest and again copy and paste into another column. The code will not sort, copy/paste, re-sort, then copy/paste; in other words it keeps the original sorting of only the buys. Is there a way to sort the buys, paste them elsewhere, then sort the sells and copy likewise? I was thinking about trying a do loop but Im not sure why I cant do this in a more simple way. Any insight would help. Thank you!
' buys
rows("3:3").Select
Selection.AutoFilter
ActiveSheet.AutoFilter.Sort.SortFields.Add Key:=Range("D3" _
), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortTextAsNumbers
With ActiveSheet.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'copy top ten buys
Range("A5:I14").Select
Selection.Copy
Range("k3").Select
ActiveSheet.PasteSpecial
Application.CutCopyMode = False
' sells
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add Key:=Range("E3" _
), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortTextAsNumbers
With ActiveSheet.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A5:I14").Select
Selection.Copy
Range("k14").Select
ActiveSheet.paste
Application.CutCopyMode = False
Since you only add a new sort key your second sort will be on the Buy and the Sell column. Do Sortfields.clear before the second search.
I am trying to delete the first row of the Excel sheet and sort a specific column using its name "CUST_RELPO". I am using the column header name since the name may change.
Sorting and copying the column from the second row since I do need to copy the column header.
Sub ClearFirstRow()
'
' ClearFirstRow Macro
'
'
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Cells.Select
Dim rngcustrelpo As Range
xindex = Application.ActiveCell.Column
Set rngcustrelpo = ActiveSheet.UsedRange.Find("CUST_RELPO")
If rngcustrelpo Is Nothing Then
MsgBox "CUST_RELPO column was not found."
Exit Sub
End If
'Cells.Select
Range(rngcustrelpo, rngcustrelpo.End(xlDown)).Select
ActiveWorkbook.Worksheets("BACKORDER").Sort.SortFields.Add Key:=ActiveSheet.UsedRange, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("BACKORDER").Sort
.SetRange ActiveSheet.UsedRange
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Set rngcustrelpo1 = rngcustrelpo.Offset(1, 0)
Range(rngcustrelpo1, rngcustrelpo1.End(xlDown)).Select
Selection.Copy
End Sub
However, it is not sorting the data like I am expecting. I am not sure what I am missing here.
Key:=ActiveSheet.UsedRange is a complete misunderstanding of the sort method. (Usedrange covers the whole used area on the sheet - often "empty" cells, too.) The same applies to .SetRange ActiveSheet.UsedRange. It is not bad just needless. SetRange is needed when you want to limit the area to be sorted. If you want to sort on only one key (column), then change this
ActiveWorkbook.Worksheets("BACKORDER").Sort.SortFields.Add Key:=ActiveSheet.UsedRange, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("BACKORDER").Sort
.SetRange ActiveSheet.UsedRange
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
to this:
With ActiveWorkbook.Worksheets("BACKORDER").Sort
.Key rngcustrelpo
.Header = xlYes
.MatchCase = False
.Order:=xlAscending
.Orientation = xlTopToBottom
.SortOn:=xlSortOnValues
.DataOption:= xlSortTextAsNumbers
.SortMethod = xlPinYin
.Apply
End With
And you can find more info here: Excel SortFields add then sort and here: Most efficient way to sort and sort syntax VBA
Thanks so much for reading my questions. I tried to search the similar questions in the stackoverflow but failed to get the answer...thanks so much if you can help me.
My purpose here is to copy and paste the data from Sheets("Parts") to Sheets("Summary"), and then sort by A column, ignoring the empty cell.
A1: 2.1
A2:
A3: 1.1
A4: 1.2
After sorting :
A1: 1.1
A2: 1.2
A3: 2.1
A4:
The macro is success in the Macro builder but then failed in the worksheet (First row empty). Indeed I try to not copying the empty cell with "SkipBlanks" but not functional...
Sub IEMacro()
Dim Lastcell As Range
Sheets("Parts").Range("A3:A300").Copy
With Sheets("Summary")
Set Lastcell = .Range("A65536").End(xlUp)
.Range("A2", Lastcell).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=True, Transpose:=False
.Range("A2", Lastcell).Sort Key1:=ActiveCell, Order1:=xlAscending, Header:=xlGuess
End With
This should apply the sorting key properly :
Sub IEMacro()
Dim LastCell As Range
Sheets("Parts").Range("A3:A300").Copy
With Sheets("Summary")
Set LastCell = .Range("A" & .Rows.count).End(xlUp)
.Range("A2").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=True, _
Transpose:=False
With .Sort
.SortFields.Clear
.SortFields.Add Key:=Sheets("Summary").Range(Sheets("Summary").Range("A1"), LastCell), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
.SetRange Sheets("Summary").Range(Sheets("Summary").Range("A1"), LastCell)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
End Sub
In that case you need to select complete range till the last column of your data then sort it to ascending order, fianlly go with the auto filter.
Sub sort()
'selecting complete range change it as required
Range("A1:O12").Select
' Replace the sheet name with yours
ActiveWorkbook.Worksheets("Sheet1").sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").sort.SortFields.Add Key:=Range("A1"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").sort
'sorting complete range change it as required
.SetRange Range("A1:O11")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
I have many worksheets with sequential linear xy data that vary in length. The objective is to delete all rows where x data is not divisible by 50. Below is the generated macro that uses a helper column to search for integers to be deleted.
Sub Divis50()
Sheets("VERT SCALES").Select
Range("C2").Select
ActiveCell.FormulaR1C1 = _
"=IF((OR((RIGHT(RC[-2],2)=""50""),(RIGHT(RC[-2],2)=""00""))),""YES"",""NO"")"
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C6062")
'sort filtered results
Range("C2").Select
ActiveWorkbook.Worksheets("VERT SCALES").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("VERT SCALES").Sort.SortFields.Add Key:=Range("C2") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("VERT SCALES").Sort
.SetRange Range("A2:C6062")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' scroll to first no and delete rows
Rows("123:123").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
'sort "A" back to consecutive numbers
Range("A2").Select
ActiveWorkbook.Worksheets("VERT SCALES").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("VERT SCALES").Sort.SortFields.Add Key:=Range("A2") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("VERT SCALES").Sort
.SetRange Range("A2:C122")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'delete filtered column
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
End Sub
This will delete rows that don't equal a whole number when divided by 50
Sub Button1_Click()
Dim FrstRng As Range, Lrw As Long
Dim UnionRng As Range
Dim c As Range
Lrw = Cells(Rows.Count, "A").End(xlUp).Row
Set FrstRng = Range("A2:A" & Lrw)
For Each c In FrstRng.Cells
If Int(c / 50) / (c / 50) <> 1 Then
If Not UnionRng Is Nothing Then
Set UnionRng = Union(UnionRng, c) 'adds to the range
Else
Set UnionRng = c
End If
End If
Next c
UnionRng.EntireRow.Delete
End Sub
I would recommend a helper column that flags your data appropriately. Either through formula or VB.
Then use an Autofilter to select the the flags and then delete.
try here for sample code that will delete filtered data.
http://www.mrexcel.com/forum/excel-questions/460513-visual-basic-applications-code-delete-only-rows-filtered.html
I am trying to copy a set of filtered data from one sheet to the bottom of another sheet. And my code works great except for the first time upon opening the file I get a:
Run Time error 1004
If I quit the debugger and re-run the macro it works great.
Here is my code: noted where the problem occurs.
Sub MoveData_Click()
'Select the filtered alarm data and paste on the master spreadsheet
Sheets("DailyGen").Select
ActiveSheet.UsedRange.Offset(5, 0).SpecialCells _
(xlCellTypeVisible).Copy
Sheets("2015 Master").Select
If ActiveWorkbook.ActiveSheet.FilterMode _
Or ActiveWorkbook.ActiveSheet.AutoFilterMode Then
ActiveWorkbook.ActiveSheet.ShowAllData
End If
Range("C4").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, -2).Range("A1").Select
ActiveSheet.Paste '~~> THIS IS WHERE IT ERRORS
'Sort newest to oldest in the date column
ActiveWorkbook.Worksheets("2015 Master").ListObjects("Table44").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("2015 Master").ListObjects("Table44").Sort.SortFields.Add _
Key:=Range("Table44[[#All],[Active Time]]"), _
SortOn:=xlSortOnValues,
Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("2015 Master").ListObjects("Table44").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
When you ShowAllData (same as Data->Clear in the Filter section) you are emptying the clipboard and telling Excel to forget about the copied Range. Do it outside of VBA to confirm if you want. Excel loves to empty the clipboard when you edit a cell or do much of anything other than selecting.
To fix, do the Copy after the ShowAllData. In your case, you will have to Select the Worksheet back and forth.
You should generally work to avoid using Select and Activate for your VBA. See this post for details.
Here is the final code with the changes made:
Sub MoveData_Click()
'Select the filtered alarm data and paste on the master spreadsheet
Sheets("2015 Master").Select
If ActiveWorkbook.ActiveSheet.FilterMode Or ActiveWorkbook.ActiveSheet.AutoFilterMode Then
ActiveWorkbook.ActiveSheet.ShowAllData
End If
Sheets("DailyGen").Select
ActiveSheet.UsedRange.Offset(5, 0).SpecialCells _
(xlCellTypeVisible).Copy
Sheets("2015 Master").Select
Range("C4").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, -2).Range("A1").Select
ActiveSheet.Paste
'Sort newest to oldest in the date column
ActiveWorkbook.Worksheets("2015 Master").ListObjects("Table44").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("2015 Master").ListObjects("Table44").Sort.SortFields.Add _
Key:=Range("Table44[[#All],[Active Time]]"), SortOn:=xlSortOnValues, Order _
:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("2015 Master").ListObjects("Table44").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub