I have this code within a loop that sets autofilter to a dynamically generated table and shows only a few rows out of many based on the filters used. The autofilter within the loop works perfect, but what I need is to sort it alphabetically (A to Z) based on the Values that came out.
My problem is that I can't manage to find a way to select the range of shown data in column A and filter it Alphabetically. Can someone help me out here? I will add a picture of what I mean :D
table_filter
[table_select](https://i.stack.imgur.com/kW2HY.png)
table_sort
Thank you guys in advance
I tried selecting the range with last cell with a value and other selection ways, but I couldn't manage to create a generic way in order to use it within a loop.
I can't find a way to grab the visible data in column A of that table and use it as a range to sort the data from a to Z
Update: I attached the code where I would like to sort the data.
So before this code is used I have a very long table with up to 64000 entries. I filter the table based on multiple criteria within an array in order to get 5 to 7 names filtered on a specific date. which you can see here:
Sheets("5.0").Range("A1:D1").AutoFilter Field:=1, Operator:=xlFilterValues, Criteria1:=ID_range
Sheets("5.0").Range("A1:D1").AutoFilter Field:=3, Operator:=xlFilterValues, Criteria1:=FQ_Date
After this filter is applied I would like the result to be sorted from A to Z in order to grab that sorted info as you can see in the pictures above and paste it at the destination sheet.
Sub filter_with_array_as_criteria_FQ()
Dim FQ_Range As String
Dim FQ_Date As String
Dim FQ_Output_50 As String
For rep = Range("O6").Value To Range("P6").Value
FQ_Range = Sheets("Admin").Range("J" & rep).Value
FQ_Date = Sheets("Admin").Range("M" & rep).Value
FQ_Output_50 = Sheets("Admin").Range("K" & rep).Value
FQ_Output_24 = Sheets("Admin").Range("L" & rep).Value
Dim ID_range, k As Variant
ID_range = Application.Transpose(Sheets("KW_Bericht_Report").Range(FQ_Range))
For k = LBound(ID_range) To UBound(ID_range)
ID_range(k) = CStr(ID_range(k))
Next k
Sheets("5.0").Range("A1:D1").AutoFilter Field:=1, Operator:=xlFilterValues, Criteria1:=ID_range
Sheets("5.0").Range("A1:D1").AutoFilter Field:=3, Operator:=xlFilterValues, Criteria1:=FQ_Date
'Hier muss Tabelle sortiert werden/ This is where the filtered Data needs to be sorted
'Select values from listobject column 4 paste on kwbericht
Sheets("5.0").Select
Sheets("5.0").ListObjects(1).ListColumns(4).DataBodyRange.Select
Selection.Copy
Sheets("KW_Bericht_Report").Range(FQ_Output_50).PasteSpecial Paste:=xlPasteAll
'Select Sheet 2.4
Sheets("2.4").Range("A1:D1").AutoFilter Field:=1, Operator:=xlFilterValues, Criteria1:=ID_range
Sheets("2.4").Range("A1:D1").AutoFilter Field:=3, Operator:=xlFilterValues, Criteria1:=FQ_Date
'Hier muss Tabelle sortiert werden/ This is where the filtered Data needs to be sorted
'Select values from listobject column 4 paste on kwbericht
Sheets("2.4").Select
Sheets("2.4").ListObjects(1).ListColumns(4).DataBodyRange.Select
Selection.Copy
Sheets("KW_Bericht_Report").Range(FQ_Output_24).PasteSpecial Paste:=xlPasteAll
Debug.Print (FQ_Date)
Debug.Print (FQ_Range)
Next rep
Sheets("KW_Bericht_Report").Select
End Sub
Related
I'm trying to Filter a table with more than one parameter stored in a variant (SelectedPiers).
I've tried with this two codes:
For Each Pier In SelectedPiers
.Range("A3:J" & FileLastRow).AutoFilter field:=2, Criteria1:=Pier
Next
.Range("A3:L" & FileLastRow).AutoFilter field:=2, Criteria1:=SelectedPiers
The two of them just filter the table only by the last value. The variant is filled with strings.
I am trying to sort a table where I am sorting based on the data entries in Column E. However, when I am using my code, (below) my rows for which the column become disorganized and mismatched. Each row needs to contain the same data entries as before they were sorted, only moving to be sorted based on the values in column E. I am trying to sort this table, with my expected outcome shown below:
Code:
With Worksheets("Fruits")
Dim FruitsLastRow As Long
Dim FruitsKeyRange As Range
FruitsLastRow = .Range("E" & .Rows.Count).End(xlUp).Row
Set DashboardRange = Range("E17:G" & FruitsLastRow)
DashboardRange.Sort key1:=Range("E17:E" & FruitsLastRow), _
order1:=xlAscending, Header:=xlNo _
End With
This is what I have
This is what I need
You need to specify just the top cell instead of all cells for key1 parameter (Range.Sort example).
DashboardRange.Sort key1:=Range("E17"), order1:=xlAscending, Header:=xlNo
I'll start by saying I have no idea if I'm even asking this question correctly...encouraging, huh?
I have an Excel spreadsheet, the first row of which are column headers with the autofilters activated. What I am trying to do is create a loop in VBA where the first individual criterion from the autofilter field:=2 is "checked" and the filtered data gets displayed; then "unchecked" and is undisplayed; and then the next below criterion in the filter box is "checked" and the filtered data gets displayed; then "unchecked" and undisplayed, and so on.
I do have other intentions on what to do with the data when it is displayed after the criteria is "checked" in the filter box, but I'm confident I can figure out the coding for those actions separately.
What I'm trying, or rather hoping, to avoid is creating a separate comma separated value range for what would be a massive array, since there are a lot of criterion in autofilter field:=2, and the those criteria in the autofilter field:=2 will occasionally change. Maybe that's possible, maybe it isn't.
Any help or guidance is greatly appreciated!
UPDATE - I figured that it may be simpler to filter the unique values in column B and then paste those values in a distant spot, column S, on the worksheet. Then I recorded a basic filter macro and changed criteria1:= to cells(uic,19), the values in column S and created a do while loop to run the filter on the criteria in column S adding +1 before each loop.
The code is below -
Sub filter_example()
Dim lastrowuic, lastrowarray As Long
lastrowuic = Cells(Rows.Count, "B").End(xlUp).Row
lastrowarray = Cells(Rows.Count, "S").End(xlUp).Row
Dim uniquesArray As Variant
uniquesArray = Range("B2:B" & lastrowuic)
Dim uic As Integer
uic = 2
With Sheet1
Sheets("Sheet1").Columns("B:B").AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=.Range("S1"), Unique:=True
End With
Do While lastrowarray
ActiveSheet.Range("$A$1:$S$21").AutoFilter=2,Criteria1:=Cells(uic, 19)
ClearAllFilters = True
uic = uic + 1
Loop
End Sub
The two areas that could afford cleaning up are the activesheet.range and really the end. This loop does do what I want, but I would like it to exit the loop once it gets to the first blank row, following the last row of data, in column "S" (aka 19) and then clear the filter in autofilter=2. That's really it. Otherwise this is doing what I want it to do.
Any additional assistance is appreciated!
I have written a bunch of VBA macros to get my data formatted how I need it, and the last step is to sort by this new column I have generated in ascending order. However, when I hit sort by the new column, the code now places all the empty cells above my newly generated column as I think it is reading the empty as a 0 and sorts it above any alphanumeric data. This is happening because of the UDF I have for sorting the data. I need to insert the new column with the UDF for each new cell that I insert, but I don't know how to define the range in the new column.
I am close to solving this but would love some help.
Essentially what I have tried for placing the data in a new column works, but the way I have set the range is placing it in a bad spot and it can easily be sorted in the wrong order now. I include all of my code, but the issue is in the last portion of it where I am setting a range to place the new data.
I think what is happening is when I set my range from C3-C2000 and populate it, the remaining empty cells are now included in my sort and give me "lower" numbers when I sort it ascending. Thus all the empty cells are ranked higher up in the column.
Option Explicit
Sub ContractilityData()
Dim varMyItem As Variant
Dim lngMyOffset As Long, _
lngStartRow As Long, _
lngEndRow As Long
Dim strMyCol As String
Dim rngCell As Range
Columns("B:B").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove 'make new column for the data to go
lngStartRow = 3 'Starting row number for the data. Change to suit
strMyCol = "A" 'Column containing the data. Change to suit.
Application.ScreenUpdating = False
For Each rngCell In Range(strMyCol & lngStartRow & ":" & strMyCol & Cells(Rows.Count, strMyCol).End(xlUp).Row)
lngMyOffset = 0
For Each varMyItem In Split(rngCell.Value, "_") 'put delimiter you want in ""
If lngMyOffset = 2 Then 'Picks which chunk you want printed out (each chunk is set by a _ currently)
rngCell.Offset(0, 1).Value = varMyItem
End If
lngMyOffset = lngMyOffset + 1
Next varMyItem
Next rngCell
Application.ScreenUpdating = True
'Here is where my problem arises
Range("C:C").EntireColumn.Insert
Dim sel As Range
Set sel = Range("C3:C2000")
sel.Formula = "=PadNums(B3,3)"
MsgBox "Data Cleaned"
End Sub
What I would like instead is a way to insert a new column, then have my UDF "PadNums" populate each cell up to the last cell of the previous column, essentially re-naming all my data from the previous column. I can then sort by the new column in ascending order and my data is in the correct order.
I think perhaps what I should do is copy column B into my newly inserted column C, then use some sort of last row function to apply the formula in all cells. That would give me the appropriate range always based on my original column?
I solved this! What I did was use range and xlDown to last row on column B, then pasted it to C, then inserted my UDF into C using the xlDown range!
Trying to Autofill Column X and Column Y with text
Where Column Z determines the table length
Starting cell for column Z is "Z3" but column "X and Y" are dynamic
Last filled cell in column "X & Y" carries the text required.
Current Last cells is "X56" and "Y56"
Current last cell in column Z is "Z89"
I can easily get to x56 or y56 using
Range("Y3").Select
Selection.End(xlDown).Select
Selection.AutoFill Destination:=Range("Y56:Y89")
Range("Y56:Y89").Select
Range("X56").Select
Selection.AutoFill Destination:=Range("X56:X89")
Range("X56:X89").Select
However the solution eludes me to remove absolute references due to the dynamic nature of the information being imported and added to the column of previous information.
I tried this code i read through my research but couldn't make it work
lastRow = Range("Y3").End(xlDown).Row
Selection.AutoFill Destination:=Range("Y3:Y" & lastRow), Type:=xlFillDefault
Any assistance would be really appreciated as this appears to be the lynch pin to completing this task
Cheers
Mick
with I am trying to stack reports generated on a 12 hourly basis.
into an accrued 24 data table. This will then be accrued into a monthly data table.
As base information is downloaded in csv format. The four reports are formatted differently so i also have to stack the four reports in two table separated and itemised by date and shift.
This then allows the use of lookups, countifs sumifs etc to populate my outputs.
The four reports are dynamic and open to the potential of having a number of blank cells throughout.
I have written code that is robust enough to achieve this short of this one issue.
As the four reports do not have time stamps i am forced to use file names (column A:A) to populate the Date and Shift ranges (column A:B) as well as (Column X:Y) but need to drag the text down to cover all rows of information
Range("Y3").Select
Selection.End(xlDown).Select
Selection.AutoFill Destination:=Range("Y56:Y89")
Range("Y56:Y89").Select
Range("X56").Select
Selection.AutoFill Destination:=Range("X56:X89")
Range("X56:X89").Select
Autofill Columns with text without absolute references to allow for dynamic column range and without known starting point on the column
Do not use xlDown to find the last row. You may want to have a look at this Finding Last Row
Is this what you are trying? I have given you two option. Take your pick.
Option Explicit
Sub SampleA()
Dim ws As Worksheet
Dim lRow As Long
'~~> Change this to the relevant sheet
Set ws = Sheet1
With ws
'~~> Get last row in Col Z
lRow = .Range("Z" & .Rows.Count).End(xlUp).Row
'~~> Autofill formula in 1 go
.Range("X3:X" & lRow).Formula = .Range("X3").Formula
.Range("Y3:Y" & lRow).Formula = .Range("Y3").Formula
End With
End Sub
Sub SampleB()
Dim ws As Worksheet
Dim lRow As Long
Dim rng As Range
'~~> Change this to the relevant sheet
Set ws = Sheet1
With ws
'~~> Get last row in Col Z
lRow = .Range("Z" & .Rows.Count).End(xlUp).Row
Set rng = .Range("X3:Y3")
rng.AutoFill Destination:=.Range("X3:Y" & lRow)
End With
End Sub