Basically I want to sort
Column A3 until end:
Change all column A3 until end value into "ID"
Column B3 until end:
Sort A to Z
Text to column: column date format to text
This is the code I made by recording my macro but it does not work in every data. Please help me fix this. Thankyou
Sub Sort2()
'
' Sort2 Macro
'
'
ActiveCell.FormulaR1C1 = "ID"
Range("A3").Select
Selection.Copy
Range("A3:A30").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=-135
Range("B3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
ActiveWorkbook.Worksheets("Tickets Received").ListObjects("Tickets").Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets("Tickets Received").ListObjects("Tickets").Sort. _
SortFields.Add2 Key:=Range("B3:B126"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Tickets Received").ListObjects("Tickets").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.TextToColumns Destination:=Range("B3"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 2), TrailingMinusNumbers:=True
Range("C7").Select
End Sub
This will place the word ID on every row in the column headed ID and will sort by the Tickets Received column.
Not sure how to split the Tickets Received column - what data is in there?
Sub Test()
Dim lo As ListObject
Set lo = ThisWorkbook.Worksheets("Tickets Received").ListObjects("Tickets")
With lo
.ListColumns("ID").DataBodyRange = "ID" 'DataBodyRange is the whole column (in the table) minus the header.
.Sort.SortFields.Clear
.Sort.SortFields.Add2 Key:=.ListColumns("Tickets Received").DataBodyRange, _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
.Sort.Apply
End With
End Sub
NB: ThisWorkbook represents the workbook where the current macro code is running.
ActiveWorkbook represents the workbook in the active window (the window on top).
It's easy to reference the wrong workbook using ActiveWorkbook.
Related
Hi have a report that is generated automatically and I want to create a macro that formats the report into something I can use as a database on our network drive. The report generates 2 tables that I want to streamline into 1 table. How do I find the range of the 2nd table when the 2nd table is never in exactly the same place due to the number of rows in each table changing daily.
If it helps, there is a unique header in the second table named "Potential Locations" which is the last row of the 2nd table.
I'm not really sure where to start with this code.
I basically want to run the macro to find the 2nd table and select it, delete the headers then move it 2 rows up and 2 columns to the right.
Thanks for the help, I got there in the end, see code below for how I resolved my issue.
Sub FORMAT_CUSTOMER_ORDER_REPORT()
'
' FORMAT_CUSTOMER_ORDER_REPORT Macro
' RE-ORGANISES CUSTOMER ORDER CLEANUP REPORT AND SAVES TO SHARED DRIVE IN FORRECT FILE FORMAT
'
'Code below adjusts column widths and deleted first row
Columns("A:A").ColumnWidth = 5.14
Columns("C:C").ColumnWidth = 10
Columns("D:D").ColumnWidth = 10.86
Columns("E:E").ColumnWidth = 4.14
Columns("G:G").ColumnWidth = 43.43
Columns("H:H").ColumnWidth = 5.14
Columns("I:I").ColumnWidth = 3.43
'Deletes top row.
Rows("1:1").Select
Selection.Delete Shift:=xlUp
'Code below finds and selects the cell with "POTENTIAL" in it.
Cells.Find(What:="POTENTIAL", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Activate
'Code below 'cuts' the 'current region'.
ActiveCell.CurrentRegion.Cut
'Code below pastes clipboard to the first blank cell in Column C.
Range("C1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
'Code below selects "current region" which in this case is the entire sheet
ActiveCell.CurrentRegion.Select
'Code below finds and selects the cell with "POTENTIAL" in it.
Cells.Find(What:="POTENTIAL", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Activate
'Code below select entire row of currently selected cell.
ActiveCell.EntireRow.Select
'Deletes selection
Selection.Delete Shift:=xlUp
'Selects 'current region'.
ActiveCell.CurrentRegion.Select
'Code below freezes first row
Rows("1:1").Select
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
Cells.Select
Sheet1.Sort.SortFields.Clear
Sheet1.Sort.SortFields.Add2 Key:= _
Range("B1:B2931"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With Sheet1.Sort
.SetRange Range("A1:N10000")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWindow.SmallScroll Down:=-6
'Code below Sorts entire sheet by Column B, A - Z.
Sheet1.Sort.SortFields.Clear
Sheet1.Sort.SortFields.Add2 Key:= _
Range("B1:B10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With Sheet1.Sort
.SetRange Range("A1:N10000")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Code Below Saves File to network shared drive with the correct name
ActiveWorkbook.SaveAs Filename:= _
"R:\6024 Onsite\ONSITE CUSTOMER ORDERS\6024 CUSTOMER REPORT.xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub
I have a file that has a vba macro to paste and sort data.
Now everytime i save the file, when opening it says it has a problem = removed records :sorting from sheet3 ( even tough i do not have a sheet3 in my file) , and my file gets corrupted and 'locked for editing' .
Thank you in advance for any help.
' sum Macro
Range("A3").Select
ActiveCell.FormulaR1C1 = "=Pivot!R[3]C"
Range("A3").Select
Selection.AutoFill Destination:=Range("A3:A1500"), Type:=xlFillDefault
Range("A3:A1500").Select
ActiveWindow.SmallScroll Down:=-3
Selection.AutoFill Destination:=Range("A3:ab1500"), Type:=xlFillDefault
Range("A3:ab1500").Select
Range("AC3").Select
Range("AC3").Select
ActiveCell.FormulaR1C1 = "=RC[-12]+RC[-10]+RC[-8]+RC[-6]+RC[-4]+RC[-2]+RC[-14]+RC[-16]+RC[-20]+RC[-18]+RC[-22]+RC[-24]"
Range("AC3").Select
Selection.AutoFill Destination:=Range("AC3:AC1500")
Range("AC3:aC1500").Select
ActiveWindow.SmallScroll Down:=60
Range("ac3").Select
Range("A3:ac1500").Select
Selection.copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("ac2").Select
Application.CutCopyMode = False
Range("ac2").Select
ActiveWorkbook.Worksheets("Summary table").Sort.SortFields.Add Key:=Range( _
"ac2:ac1500"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Summary table").Sort
.SetRange Range("A2:ac1500")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A3").Select
'
End Sub
Try "File"-> "Options"->"Advanced"-> scroll down to see title,"When calculating this workbook"->check the box "update links to other documents" -> "Ok" and then try executing your code.
Recommended reading: How to avoid using Select in Excel VBAIt is better to get away from Select and Activate sooner than later.
Here is what is actually necessary for your code to execute everything from the original above.
Option Explicit
Sub summmit()
With ActiveWorkbook.Worksheets("Summary table")
.Activate
.Range("A3:AB1500").FormulaR1C1 = "=Pivot!R[3]C"
.Range("AC3:AC1500").FormulaR1C1 = _
"=RC[-24]+RC[-22]+RC[-20]+RC[-18]+RC[-16]+RC[-14]+RC[-12]+RC[-10]+RC[-8]+RC[-6]+RC[-4]+RC[-2]"
.Range("A3:ac1500") = .Range("A3:ac1500").Value
With .Range("A2:ac1500")
.Cells.Sort Key1:=.Columns(29), Order1:=xlDescending, _
Orientation:=xlTopToBottom, Header:=xlYes
End With
.Range("A3").Select
End With
End Sub
There does not appear to be any 'workbook-breaking' operations. Two things do come to mind: a) refresh the pivot table on the Pivot worksheet and b) start writing your formulas in xlA1 rather than the recorded xlR1C1 reference style. Additionally, the solution recommended by arun v shows merit.
I'm trying to remove unwanted lines off all the worksheets. For example:
I have multiples worksheets that I need to make a treatment that consist on finding a special character like "-" and remove everything that is above that line, and I mean exclude all lines.
I'm stuck now.. I can't do it properly to apply to all my worksheets and the cell range is different each day.
So far I could do..
Sub Clean()
'
' Clean Macro
'
'
Cells.Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Sheet1.txt").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1.txt").AutoFilter.Sort.SortFields.Add Key _
:=Range("A1:A66723"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1.txt").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Columns("A:A").Select
Selection.Find(What:="-", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Range("A59044").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.EntireRow.Delete
Selection.End(xlUp).Select
Selection.End(xlUp).Select
End Sub
This works for me:
Sub Clean()
Dim ws As Worksheet
Dim Search As Range
Dim addr As String
For Each ws In Worksheets
ws.Activate
With ws.Sort
.SortFields.Clear
.SortFields.Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Cells
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
With ws.Range("A:A")
Set Search = .Find(What:="-", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not Search Is Nothing Then
addr = Search.Address
Else
Exit Sub
End If
End With
Range(addr).Select
ActiveCell.Offset(1, 0).Select
Range(ActiveCell, ActiveCell.End(xlDown)).Select
Selection.EntireRow.Delete
Next
End Sub
I have a range that length keeps changing. I need to redefine the named range each time it changes and then sort it.
I have this so far:
Sub Macro2()
'
' Name and Sort
'
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
ActiveWorkbook.Names.Add Name:="data4", RefersToR1C1:= _
"='Data Storage'!R3C1:R25C18"
ActiveWorkbook.Names("data4").Comment = ""
Application.Goto Reference:="data4"
ActiveWorkbook.Worksheets("Data Storage").sort.SortFields.Clear
ActiveWorkbook.Worksheets("Data Storage").sort.SortFields.Add Key:=Range( _
"D3:D25"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Data Storage").sort
.SetRange Range("A3:R25")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
But the next time the range changes it only sorts on the previous range. I think it has to do with the R3C1:R25C18 reference but I don't know how to change that each time the range changes.
Thanks for any help.
This code checks for the last row with data, names the range, and sorts the named range
Dim lngRowLast As Long
'Find last row
lngRowLast = Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'Your range goes here
Range(cells(3,1),cells(lngRowLast,18)).Name = "data4"
Range("data4").Sort Key1:=Cells(3, 1), Order1:=xlAscending, _
Header:=xlYes
i have recently added the below piece of code in my macro to sort/filter a sheet and find a particular value and apply some formating based on the value found cell
Sheets("Extract").Select
Range("A1").Select
Selection.AutoFilter
Selection.AutoFilter
' finding the yesterday last resolved(on same day) ticket
Range("A1:AB2164").Select
ActiveWorkbook.Worksheets("Extract").Sort.SortFields.Clear 'sorting with resolved date
ActiveWorkbook.Worksheets("Extract").Sort.SortFields.Add Key:=Range("E2:E2164" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Extract").Sort
.SetRange Range("A1:AB2164")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
'filtering resolved tickets |received today|resolved on same day
ActiveSheet.Range("$A$1:$X$164").AutoFilter Field:=5, Criteria1:="<>"
ActiveSheet.Range("$A$1:$Z$2164").AutoFilter Field:=26, Criteria1:="Yes"
ActiveSheet.Range("$A$1:$AB$2164").AutoFilter Field:=28, Criteria1:="Yes"
' finding the yesterday last resolved ticket (on same day)
Cells.Find(What:=yesterday_resolved_ticket__on_same_day, After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
' selecting date of resolved tickets
Call nextrow
Selection.Offset(0, 1).Select
Range(Selection, Selection.End(xlDown)).Select
cticket_resolved_same_day = Application.CountA(Selection) ' counting tickets resolved on same day
Range(Selection, Selection.Offset(0, 1)).Select
' changing colour of the date
With Selection.Font
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0
End With
but after this sort and filter my excel sheet gets hung up the shet itself is not visible fully
can some one help me to find the problem in this code