Repeating Cell Fill In Randomly Selected Rows - random

I want to auto-fill the text "SOLD" to column K in each row from a pre-chosen set of rows not necessarily in sequential order. For ex: I may select rows 1-3 & maybe rows 8 & 10. Upon selecting said rows, I want to run my macro to enter the text "SOLD" in each of them.
Here is my current macro, which works only on one row at a time:
Sub ItemSold()
Range("K" & ActiveCell.Row).Select
ActiveCell.FormulaR1C1 = "SOLD"
Rows(ActiveCell.Row).Select
End Sub
I assume there's a way to cycle through all of the selected rows with FOR EACH or maybe a REPEAT function/?

Related

Google Sheets - How to Combine Filter Function with Filter View

I've been working on a spreadsheet with over 100 rows, and found a hacky way to incorporate a "hide" checkbox that will hide any row where column C matches a specific value (building type), specified beside the box. To do this, I first created a function like this: =FILTER(Data!A1, OR(Data!$C1<>$O$2, $P$2)) and dragged that across every row and column in a seperate sheet. This reads as, "Display current cell if the corresponding column C in that row in Data does not match the building type, or if the the checkbox is checked. This way, the whole row is hidden when the building type matches, and the box is unchecked. A1 adjusted to each row individually, $C1 referenced the building's type, $O$2 referenced the targeted type to potentially hide, and $P$2 was the checkbox.
Problem #1: This created a lot of formulas in hundreds of cells, and when the building type was not found, it displayed #N/A across the entire row. A Filter View was able to hide these values, but it was inconvenient to have to reset the values every time I wanted to hide or unhide another building type.
My Attempt to Fix: I used a filter function once again to recreate the entire sheet from one cell, hiding the appropriate rows, using this: =FILTER(Data!A2:J191, ARRAYFORMULA((Data!$C2:C191<>$O$2)+(Data!D2:D191*$P$2)) This is the hacky part. I multiplied the checkbox's "true" by an array arbitrary positive numerical values in the D column to "OR" it with each building type value to achieve the same goal as before, but for EVERY cell.
Problem #2 arose: When I get my beautiful sheet, I can not sort it via a filter view, or it will throw an error and display nothing. I'm resorting to sorting the original tab, but intend to have it be ignored entirely. So how do I combine these two, Filter View, and Filter Function, to create a nice spreadsheet where I can SORT AND HIDE rows?
Bonus Problem #3: To add more buttons, my formula is this: =FILTER(Data!A1:J191, ARRAYFORMULA((Data!$C1:C191<>$O$2)+(Data!D2:D192*$P$2)), ARRAYFORMULA((Data!$C1:C191<>$O$3)+(Data!D2:D192*$P$3)), ARRAYFORMULA((Data!$C1:C191<>$O$4)+(Data!D2:D192*$P$4)), ARRAYFORMULA((Data!$C1:C191<>$O$5)+(Data!D2:D192*$P$5)), ARRAYFORMULA((Data!$C1:C191<>$O$6)+(Data!D2:D192*$P$6)), ARRAYFORMULA((Data!$C1:C191<>$O$7)+(Data!D2:D192*$P$7)), ARRAYFORMULA((Data!$C1:C191<>$O$8)+(Data!D2:D192*$P$8)), ARRAYFORMULA((Data!$C1:C191<>$O$9)+(Data!D2:D192*$P$9))) This is ugly, and very slow to load. Is there a way to create a function range to handle the same checks on multiple rows, and crunch it into a single formula?
Here is another monstrosity (this one has less repetition) for you:
=QUERY(
{IGNORE!A2:J, IGNORE!P2:P},
"SELECT * "
& "WHERE Col3 is not null "
& IF(COUNTIF(P2:P9, False) = 0, "", "AND NOT Col3 MATCHES '^" & JOIN("$|^", IFNA(FILTER(O2:O9, P2:P9 = False))) & "$' ")
& IF(COUNTIF(A2:K2, ">0") = 0, "", "ORDER BY Col" & JOIN(", Col", IFNA(FILTER(COLUMN(A2:K2) & IF(COLUMN(A2:K2) = 1, "", " DESC"), A2:K2)))),
0
)
Your checkboxes should remain. The second row can have just True/False values, no need for column number (a simple change will be needed COUNTIF(A2:K2, ">0") -> COUNTIF(A2:K2, True)). Also consequent sort works now (but only in the actual order of columns: if checked 1, 3, 4 then it will be sorted first by 1, then by 2, then by 4). You could place another config table on the right about sorting, where you would select all the columns you wish to sort by, their mutual order, and desc/asc for them.
Edit: added IFNA so FILTER won't return an error, changed multiple ANDS to MATCHES and simple regexes.

Excel for Mac VBA - Go to specific row of table and work up the rows to first row

I´d like to find a specific row of a table in Excel for Mac 2011. From this row, I want to work up the table to the 1st row. I´m sure there is a simple way of doing it.
Also, what is the tweak, to work down to the end of the table from a specified row?
Thanks in Advance!!
You might want to provide an idea of what you have tried...as well as how you want to find the starting row. Regardless, here I have finding the end row of your data (based on column A, change as needed). This goes to the bottom of the sheet, then essentially does Ctrl + up arrow to get the last row that has something in it. Then starts from there and steps backwards through the rows until row 1. Nb you don't have to set i to endRow, I just did in case you wanted to use endRow for something else.
Public Sub testing()
Dim ws As Worksheet
Dim endRow As Integer
Dim i As Integer
Set ws = ThisWorkbook.Worksheets("Sheet1")
endRow = ws.Range("A" & Rows.Count).End(xlUp).row
i = endRow
While i >= 1
'Working up the sheet doing whatever
i = i - 1
Wend
End Sub

Loop through filtered values in excel

I am trying to automatically apply filter on a column in the excel sheet and I have to loop through the filtered values available on different column.I am not getting how to loop through the filtered values?
Applying filter on 19th column.Have to loop through the values available on 12th column.
strPath="C:\Users\PSi\Desktop\CodeIn.xlsx"
Dim ProcessName
ProcessName=Trim(InputBox("Process Name:"))
Set objExcel= CreateObject("Excel.Application")
objExcel.Visible= True
objExcel.Workbooks.Open(strPath)
With objExcel.Activeworkbook.Sheets("All")
.Range("A1").AutoFilter 19,"="&ProcessName
End With
'How to loop through the filtered values ??
'Need to loop through only on the above filtered values..
for i=2 to (How to get end of filtered used rows)
if Mid(objExcel.Activeworkbook.Sheets("All").Cells(i,12).Value,1,3)="Seq"
Then msgbox objExcel.Activeworkbook.Sheets("All").Cells(i,12).Value
End if
Next

How can I more efficiently find the height of a table using Python

I am using openpyxl to copy data from an Excel spreadsheet. The data is a table for an inventory database, where each row is an entry in the database. I read the table one row at a time using a for loop. In order to determine the range of the for loop, I wrote a function that examines each cell in the table to find the height of the table.
Code:
def find_max(self, sheet, row, column):
max_row = 0
cell_top = sheet.cell(row = row - 1, column = column)
while cell_top.value != None:
cell = sheet.cell(row = row, column = column)
max = 0
while cell.value != None or sheet.cell(row = row + 1, column = column).value != None:
row += 1
max = max + 1
cell = sheet.cell(row = row, column = column)
if max > max_row:
max_row = max
cell_top = sheet.cell(row = row, column = column + 1)
return max_row
To summarize the function, I move to the next column in the worksheet and then iterate through every cell in that sheet, keeping track of its height until there are no more columns. The catch about this function is that it has to find two empty cells in a row in order to fail the condition. In a previous version I used a similar approach, but only used one column and stopped as soon as I found a blank cell. I had to change it so the program would still run if the user forgot to fill out a column. This function works okay for a small table, but on a table with several hundred entries this makes the program run much slower.
My question is this: What can I do to make this more efficient? I know nesting a while loop like that makes a program take longer but I do not see how to get around it. I have to make the program as foolproof as possible, so I need to check more than one column to stop user errors from failing the program
This is untested, but every time I've used openpyxl, I iterate over all rows like so:
for row in active_worksheet:
do_something_to(row)
so you could count like:
count = 0
for row in active_worksheet:
count += 1
EDIT: This is a better solution: Is it possible to get an Excel document's row count without loading the entire document into memory?
Read-only mode works row-by-row on the source so you probably want to hook it into it. Alternatively, you could pass the cells of the of a worksheet into something like a Pandas matrix which has indices for empty cells.

Filtering a validation list based on columns within a named range

I am looking for a way to filter a list validation in Excel based on a multi-column named range.
I have a list of product releases on one sheet, contained in a named range that has the columns: Name, Type, Status. On another sheet, I want the user to be able to select from a validation list containing 'Name' only.
Question 3741060 here covers how to make the validation list only contain the 'Name' column. However I also need to filter so that the user cannot select a release with the status 'Completed'. [The status column only allows 'Planned', 'Allocated' or 'Completed'.]
Ideally I would also like to dynamically show only 'Planned' OR 'Allocated' releases based on yet another validation - but I think if I can get the list filtered at all I should be able to do the rest.
BTW - I am forced to use Excel 2003 for this, although I don't believe would be a major factor.
I use
an extra range LOV (for List of Values) in a hidden sheet that I fill with the current criteria the user can choose from (in my case this varies from line to line as he/she fills the sheet)
all cells in the main sheet are validated against this range LOV
a Selection_Change() trigger loads the LOV after each cursor move from the original range of possible choices
This is how I re-generate my LOV (in essence the user has already selected a country code in another cell passed here in string CtyCd, and the sheet now is prepered to offer a selection of possible choices of something called GINI for only this country ... so maybe similar to your demand)
Sub LoadL2LOV(CtyCd As String, LOVL2 As Range)
'
' CtyCd is a selection criterium for the original list in range GINI
' LOVL2 is the target range containing the current list of values
' all cells in sheet have a validation against range LOV defined
'
Dim GINI As Range, Idx As Long, Jdx As Long, LName As Name, Adr As String
' clear current PoP_L2
Set LName = ActiveWorkbook.Names(LOVL2.Name.Name)
Set GINI = Worksheets("GINI Availability").Range("GINI")
LOVL2.ClearContents
' set new LOV for PoP_L2
If CtyCd <> "" Then
Idx = 2
Jdx = 1
' find 1st occurence of CtyCd in GINI
Do While GINI(Idx, 4) <> CtyCd And GINI(Idx, 4) <> ""
Idx = Idx + 1
Loop
' GINI is sorted, just read until the end of selected CtyCd
Do While GINI(Idx, 4) = CtyCd
LOVL2(Jdx, 1) = GINI(Idx, 1) & "-" & GINI(Idx, 2) & "-" & GINI(Idx, 3)
Idx = Idx + 1
Jdx = Jdx + 1
Loop
End If
' redefine LOV name to contain all current valid choices
LOVL2.CurrentRegion.Name = LOVL2.Name.Name
End Sub
In your case, as the data seems to be more or less static, you can copy all valid selections from [Prod_Release] to LOV at Sheet_Activate or any appropriate activation trigger.
Hope this helps .... good luck MikeD

Resources