Deleting data in last cell of data in Column A - vba

I am in need of assistance agian. When my macro works, the number 1 is populated in the final cell in column A because the coding is set up to place a number 1 or 2 depending on other data until the last row of data. Since the last row of data has necessary information in other columns, I wanted to delete the 1 in column A because it is not needed. How do I code to take away the value in the final cell of a column? I've tried referencing to it and if statements. I am sure I am missing something simple.
This is my first macro using VBA.
Thanks.

I just started working with VBA in the last day or two, but I believe the thing you are looking for is: Cells(x,y).End(xldown).ClearContents
Alternatively, I think Range("A1").End(xldown).ClearContents would be equally effective.
But I just started doing this myself, so take with spoonful of salt.

Related

VBA : re-sort/re-order rows based on column values through specific range

I'm fairly new to VBA, and have found this to be a great resource. I understand enough to generally find someone else's code and modify it to what I need it to do, but I've been stumped here. I've tried coding this several times in different ways, but can't seem to get it to work, and am not overly convinced I'm approaching it the right way to start with. I'm hoping someone is willing to help me.
Here's a theoretical example of what I'm trying to do.
I have a sheet that contains groups of orders of a certain product type with their quantities listed by the step of the process they're currently at:
Current orders in process
I need to "sort" them or re-order them so that:
1. The orders are copied into a tab based on process step, with the furthest process step being listed first working back to the closest, and...
2. Orders listed in the "complete" step need to be excluded completely, and...
3. I need to do this for multiple product types, which have different numbers of process steps, so the end column needs to be identified somehow (as a dynamic variable, or some trigger)
For the example, it would look like this:
Sorted data
I've thought through a half-dozen ways to logically go about this and tried to figure out how to then do that with code, but I'm stuck ... and I have the feeling that there is probably 1 or 2 "best" ways to go about this that someone in this community probably knows off the top of their head. Thank you in advance.
Edit: Here's my first attempt at coding the outline. I realize there are some components missing, but would love feedback on whether this seems to be a usable, or efficient approach.
Sub test1()
Dim skuStartRow As Long, skuEndRow As Long
Dim qty As Integer
Dim colCheck as long, lastCol as Long
skuStartRow = .Range("A:A").Find(what:=sku, after:=.Range("A1")).Row
skuEndRow = .Range("A:A").Find(what:=sku, after:=.Range("A1"), searchdirection:=xlPrevious).Row
' Code here to identify last column (lastCol) either by .UsedRange or by header?
For colCheck = 3 To lastCol
For rowCheck = skuStartRow To skuEndRow
qty = Cells(rowCheck, colCheck).Value
If qty <> "" Then
'copy current row and paste into destination worksheet
MsgBox qty 'test to see if correct qty is displayed
End If
Next rowCheck
Next colCheck
End Sub
Update:
Still having problems.
I've copied/pasted the whole range of orders based on product type to a new sheet where it will be sorted.
I've inserted a column and used the match/index formula provided by Variatus (autofill down) to create an index column.
Insert new column and use RANK function to create a rank ordered list, as there are duplicates in the Helper/Index column.
Used INDEX/MATCH functions to return values into rank ordered lists for both work order number and quantity value.
The code I currently have works - except that it's hard-coded using FormulaRICI = ... commands. I'm having major challenges figuring out how to convert the code to allow use of MATCH/INDEX functions with variables. I need to do this b/c depending on product type there are varying amounts of total orders, as well as varying amounts of number of steps; so I need to have a dynamic range for both rows/columns.
I've looked at a bunch of examples but can't seem to find something that works correctly. The most promising I've seen appears to be using the "replace" function in VBA, but my head's about to explode.
Any suggestions are much appreciated.
I wonder if you really need VBA for this. Please consider these steps.
Insert a column in your worksheet with this formula in row 2, copied down.
=IFERROR(MATCH(TRUE,INDEX($D2:$I2<>0,0),0),0). The range D2:I2 is defined by D2 = Step 1 and I2 = "Complete". The formula finds the first non-blank cell in the range. The column could be hidden.
Sort the sheet on the helper column, largest to smallest.
Apply a filter.
Filter on the product.
What you see at this point is the list you wish to create using VBA, however including the 'Complete' column and any other columns to the right of it.
There are a number of ways of automating this process. You might write code to either insert the formula in the helper column automatically or its result. This could be done when needed, without having a permanent extra column. You could write code to do the sorting and filtering automatically, for example, based on a currently selected product. And, finally, you might write code to copy the result to another sheet, remove the unwanted columns there, and restore the original sheet to its original condition (presumably sorted by order number).

Excel formula not working as expected

I have a sheet that shows max values spent anywhere. So I need to find most expensive place and return it's name. Like this:
Whole sheet.
Function.
Function in text:
=IFS((A6=MAX(D2:D31)),(INDEX(C2:C31,MATCH(A6,D2:D31,0))),(A6=MAX(H2:H31)),(INDEX(G2:G31,MATCH(A6,H2:H31,0))),(A6=MAX(K2:K31)),(INDEX(K2:K31,MATCH(A6,L2:L31,0))))
Basically I need to find a word left to value, matching A6 cell.
Thanks in advance.
Ok.. Overcomplicated!
Firstly, why the three rows? it's a lot easier if you just have one long row with all the data (tell me if you actually need 3 I'll change my solution)
=LOOKUP(MAX(D2:D31);D2:D31;C2:C31)
The MAX formula will lookup the biggest value in the list, the Lookup formula will then match it to the name.
Please note: If more than one object has the maximum price, it will only return the first one. The only way I can think of to bypass that would be to build a macro.
EDIT:
Alright.. Multi Column solution is ugly and requires extra columns that you can just hide.
As you can see you'll need 2 new columns that will find the highest for each row, 2 new columns that will find the value for each of these "highest" (in this case tree and blueberries) and then your visible answer will simply be an if statement finding out which one is bigger and giving the final verdict. This can be expanded with an infinite number of columns but increases complexity.
Here are the formulas:
MAX(H2:H31)
LOOKUP(A5;H2:H31;G2:G31)
MAX(L2:L31)
LOOKUP(C5;L2:L6;K2:K6)
IF(A5>C5;B5;D5)

Excel auto-fill any row with formula and prevent it from being deleted - programming only way?

Having an Excel problem, I searched the net. Found old answer on stack overflow, but it was both incomplete and also off topic of this forum.
Since incomplete I need a much better solution and fear it will require some (VBA-) programming. Thus I hope it is on topic.
My problem is this:
I have two sheets, one for input of data, and one for presenting/printing them. Because data is never "consistent", I had to make a trick and have a cell in each data-input-row be filled with consistent data for me to do vlookup sucessfull in the printing/presenting sheet.
And yes, autofilling the whole column (row 1 to 653..) initally does the trick.
But when users 1) delete old entries, at best they mark the rows and right click to delete not only data but the entire row, meaning that the number of rows with the actual formula decreases, or at worst just delete data in the row leaving a row without formulas in the cols I need to have rows in.
2) add new entries, they either insert a new row (thus not containing the formulas), by accident delete the formula(s) in the cells (haven't figured out yet how they manage, since the cols with formulas is hidden)
So, is there any way I can ensure that whatever the user does, there will always be formulas in any row in my hidden cols?
Respectfully I am not at all into VB/VBA, so if that is a solution (programming the col to contain the formula no matter what), please bear with me and provide the rookie-understandable answer for me to implement.
Thank you very much in advance!!!
Best regards,
Steen
Additional info:
Sorry if I'm being a real newbie, but if only I could upload a picture or an example file.
My sheet have headings: A is volunteer#, B is date, C is task (gardening, car wash, babysitting, baking, ....), D is hours worked on task, E is points per hour (using Vlookup to find points per hour for each task), and finally F (my problem) containing a formula (points per hour * hours worked).
Since everyone can (and should be able to) input a record ("I am volunteer no 1, and on the 12. of february I did gardening for 3 hours". The formula in F should result in 3 hours * 15 points).
My real formula is a bit more complicated than the example, but .... A formula is a formula.
So, when people fill in new entries or delete previous week-entries etc, it is quite random if they do it in a way that wil have the function in col F work.
If they insert a new row, me having filled entire col F with the function won't work, so they should go to next blank row (perhaps way down) to insert data.
If they delete marking the rows and right click+delete, they minimize number of rows with formula in it. If they mark rows and click delete, we get blank rows without data, formulas, whatever.
So how do I ensure that each cell in col F always will contain my formula no matter what?
Saw someone pointing to something called arrayformula, but couldn't see how I could implement it to work for me (poor examples, sadly).

VBA- Need help to do average rows if data present in the other columns

I have a excel sheet which we may keep adding rows/ deleting them.
And I have an average value present in some cell.I would want the excel formula to identify if there is text in another column to average the columns
So now if I insert another row, I have to manually update the average formula.
Is there a way to have a formula which check if column A is not empty, it should consider the data in column G for the average
There's a lot of approaches to this. My current favourite is a CELL:INDEX(...) expression. For instance, to find the last populated cell in the first continuously populated range between B1 and B5000, I would use (probably as a named range) $B$1:INDEX($B$1:$B$500,MATCH(TRUE, $B$1:$B$500="", 0)-1).
This approach is great because it's non volatile, so it shouldn't bog your worksheet down. It might be vulnerable to $B$500 gradually shrinking if you're only ever deleting rows, though. Alternatives are referencing the whole column ($C:$C), but that's usually dog slow in modern excel, or using OFFSET which never shrinks, but is volatile.

How do I look backwards for the most recent match of a text string in a column?

I have an Excel sheet with various data entries that are in date order going down the page, with the dates in column A. I need a formula that will take a text string from an adjacent cell, then look back up a neighbouring column for the most recent match then return the date from column A.
Currently I have this formula in cell H100: =LOOKUP(G100,E100:E$5,A100:A$5).
I want it to look for the text in G100 in column E, going backwards to find the most recent example and then return the corresponding date from column A but despite the LOOKUP command being in reverse it always returns the first example in date order, not the most recent.
I would really appreciate some help from an expert, which I am not!
I am not certain to understand the question, but try
=OFFSET($A$1, 1+MATCH(G100, E$5:E100, 0)0,1,1)
this should catch the first (higher in the sheet) instance of the lookup match.
I would bring it into an Access database where that kind of data manipulation is easy.
But then, I know how to use Access to do those kinds of things and I think it's much harder to do in Excel.