Need logic for checking range in formula - excel

Need help with logic. I have about 1000 rows of data where formulas were entered to calculate subtotals and totals. The nature of the formulas require each respective range to be correct. The correct range is determined by data in column C (please see illustration). For the most part, the ranges (and in turn the formula) are correct. But I have reason to believe a handful are not.
I can check them all manually or I can ask SO for help ^_^ My current thought is to loop column A and at every Subtotal, dump the rows counted so far into column V and pull the rows in the formula from column G into column W then compare V and W.
But what do I do when I reach a Total? My current thought would become cumbersome. Is there a better way? All the highlighted 0s between columns i:u need to be tested as well.
I will entertain solution to replace all formulas from scratch as well.

I'm not sure if this is what you are looking for, but I wanted to run it by you as an idea.
This example will put the totals from column G into Column Z.
Anyway, check it out, if you don't like it, I'll get rid of it.
You can add another line or make up a loop to get all the columns total.
Sub Button1_Click()
Dim RangeArea As Range
For Each RangeArea In Columns("C").SpecialCells(xlCellTypeConstants, 1).Areas
Cells(Rows.Count, "Z").End(xlUp).Offset(1).Value = Application.Sum(RangeArea.Offset(, 4))
Next RangeArea
End Sub

Related

Automate concatenation process

Here I am stucked with one excel issue where i want to concatenate from column F till column I where the logic is when the benchmark column A3 (for example) is blank it need to concatenate column F till column I till there is a value at column A4.and this logic need to automatically concatenate the mentioned column till there is a value under the benchmark column. currently i need to keep change the concatenate range in order to concatenate it fully with the logic. Appreciate if anyone can help me out.
Below image shows how i am doing manually which very time consuming
You can use the MATCH function (with a wildcard) to find the next non-blank row; and use that in an INDEX function to detect the range to concatenate.
Assuming your data starts in A3 and the lowest possible row is row 1000 (change the 1000's in the formula below if it might be much different:
J2: =IF(A2="","",CONCAT(INDEX(F2:$I$1000,1,0):INDEX(F2:$I$1000,IFERROR(MATCH("*",A3:$A$1000,0),1000-ROW()),0)))
Note: It is possible to also develop solutions using INDIRECT and/or OFFSET. Unfortunately, these functions are volatile, which means they recalculate anytime anything changes on your worksheet. If there are a number of formulas using these functions, worksheet performance will be impaired. INDEX and MATCH are non-volatile (except in ancient versions of Excel - pre-2003 or so)
The OFFSET-function would come on handy here. One solution is to do it like
This works in my worksheet.
Cell Q6 just defines the number of rows downwards that the MATCH-function is checking for the next "HEADER1" value. If "HEADER1" is found, the MATCH-function returns how many rows down-1. If no "HEADER1"-value is found within that range, that value is then the number of rows used.
If the first column also has "HEADER2" and so on, you can add the MID-function to both references inside MATCH to limit which part of the string are to be searched for.
I tried to adjust the references properly to fit your sheet, but I may have missed something:
=IF(ISBLANK($B2),"",CONCAT(OFFSET($B2,0,0,IFNA(MATCH(MID($B2,1,6),MID(OFFSET($B2,1,0,$B$1),1,6),0),$B$1),4)))

How to look through a sheet and find a value, get the column, and then count how many times a value appears in that column

I am working with data that is sent to me. The sheets always contain the same headers though they aren't really headers because it doesn't come in table form, but the columns change every pull so it is never in the same column so I can't do the Index Match like I'm used to. I need to get this to work without converting the data to a table because others that use this don't know how to do that. Is there a way to search the sheet to find the cell containing the value, capture that column address, and then count how many times the column contains a letter?
I have a front excel page that keeps account of how many times something happens. Currently I use this formula =COUNTIF('UDO '!AJ:AJ,"Y"). It works the only thing is that I can't set it up as an array because the column isn't always AJ, so I'm always having to change it manually and I'd like to automate it. So I want to be able to search the sheet that contains the information for the text value example: "Review Required FY*" and get the column that contains this (it should be a unique value) then I want to look down that column and countif it has a "Y" or "y" marked in the cell. The sheets are always varying in length and column numbers. I thought about using an HLookUp but I can't get it to work. I also could not get Index Match to work, because I never know how much data or the column order the Audit tab will be in or have.
So on the Main tab I have a cell that counts how many files I have to audit I want to go to Audit tab, look for "Review Required FY*", capture that column and count how many times "Y" or "y" are there. I'd like to be able to set this up to do it all by itself.
I currently do not have any code because I can't find anything that works.
Using VBA
Option Explicit
Sub Looper()
Dim ws As Worksheet, Found As Range, LR As Long
For Each ws In Worksheets
Set Found = ws.Cells.Find("Review Required FY*")
If Not Found Is Nothing Then
LR = ws.Cells(ws.Rows.Count, Found.Column).End(xlUp).Row
MsgBox Application.WorksheetFunction.CountIf(ws.Range(ws.Cells(1, Found.Column), ws.Cells(LR, Found.Column)), "Y")
End If
Set Found = Nothing
Next ws
End Sub
Assuming the header only appears once, you can find out what row your header is in you can use an array formula like this (apply using Ctrl+Shift+Enter):
=MAX(ROW(A1:A10)*COUNTIF(OFFSET(A1:Z1,ROW(A1:A10)-1,0),"Review Required FY*"))
(looks in the first 10 rows across A:Z)
You can feed the result of that into a lookup to find the column number.
EDIT - something like this:
The first formula needs to be entered using Ctrl+Shift+Enter but the other 2 do not.
The 1000 in the last formula is a best guess at how much data there might be below your header - no problem setting that much larger to be on the safe side, as long as you don't try to count past the end of the sheet.

VBA: if value a in cell matches value in cell b then copy cell C to cell D

I'm working on the automisation of a workbook and I can't seem to fix the next thing. What I want to do:
If the values in column A of worksheet3 matches the values in column A of worksheet7 then copy the value of column K of worksheet7 to column L in worksheet3.
Does anybody knows the code? Tried several things on Google, but I can't seem to find what I exactly need.
Try to split your problem into smaller pieces and then solve them one at a time. If there is a specific step which fails, then ask in here. You problem looks something like this:
Get the value of column A in worksheet 3 and 7
Check if values are identical
If yes: Copy from ws7 column K to ws3 column L
Loop the above for all rows
Now you have four steps to solve. There are many ways to do this, and I am not going to code it for you. But it should not be difficult to find a solution to any of them.

Adding a formula to current cell value

I got a seemingly simple question that I cannot find an answer for, most likely due to my limited Excel vocabulary... Any help would be much appreciated!
I update our company's catalog on a frequent basis, and this involves updating the prices of thousands of products. We keep an Excel version of the price lists for our clients, and these files need to stay up to date as well. All the company files have simple currency values in them, and I'd like to be able to update them all with a cell that contains a percentage multiplier. To better illustrate my needs, here's an example:
Col A Col B
1 $5 105% (multiplier)
2 $10
3 $15
I'm looking for a way to quickly and uniformly insert part of a formula "=$B$1*" into any selected range of cells A1:A3 (which already have values in them) so that their formula line would individually appear as "=$B$1*cell.value*" and produce an updated price, as opposed to "=$B$1*cell.reference".
Ultimately though, I am seeking a method that would allow me to simply change the multiplier cell and the associated range of existing prices would adjust automatically. But I want to do so without deleting or retyping the existing column of prices. If there are smarter ways to do this, please advise.
PS** I know I can Paste Special and modify a selected range of cells (multiply by the value of another cell), but I feel like the multiplier scenario would be even faster since most product categories increase a certain percentage every time there is a change.
something like:
Sub CellFormula()
Dim Cell As Variant
For Each Cell In Selection
Cell.Formula = "=$B$1*" & Cell.Value
Next
End Sub
this?
You can use the find and replace, which will look in formulas. Once it's set up, you can replace all. There is also a feature to use regex expressions in this dialog.

In VBA, looking to write a script that will carry out a function if a cell contains certain text, with ranges set as columns

I have a long string of data with lots of columns. The columns all have names like "Person_1_Date", "Person_1_Name," "Person_2_Name," Person_2_Date" etc.
I'm trying to write a script that will take the titles for each person (Person_1, Person_2), etc. and re-arrange them in a particular order (Date, Name, Date, Name, rather than Date, Name, Name, Date, as it is here).
The rough partial script I'm experimenting with looks like the following so far:
Sub Test()
Dim Rng As Range
Dim k As Long
Set Rng = Columns("B:B")
For k = Columns("B").Column To Columns("DE").Column Step 1
If (IsNumber(Search("Person_x_Date", "k" & 1)) Then 'Looking to make this get called if the Search comes back with a number'
Columns("k:k").Cut
Rng.Selection.Insert Shift:=xlToRight
Next k
End Sub
A few things I'm looking for help with. Is it possible to write a Search function that will look for parts of what's in a cell, while ignoring another? With Person_x_Date I'm hoping it to find a cell with anything in place of the x, so it could find Person_1_Date, Person_2_Date, etc. Is this possible?
The rest is a bit of a mess. Essentially, I want to set a range as a whole column, and if the search finds something, it'll copy and paste it into the correct place, and continue again until it finds what should come next (Like Person_X_Date).
I know the latter part of the code I wrote is quite far from that. I'm just looking for any kind of suggestions to help me put this together.
Be aware that excel sorting can be done horizontally (most people just use it vertically).
This in effect allows you to sort columns into order based on values in a row. (Normal vertical sorting will sort rows based on a value in a column).
In the sort dialogue, click the options button to choose horizontal sorting.
I think this will actually give you what you need.
however, if you need to manipulate the data values, you could create a formula row that changes the values and then sort on the data in the formula row..

Resources