Excel: How to let formulas change relative to each new group of formula groups? - vba

I am having an issue in Excel dragging to fill in formulas. I have formulas in rows such that each row has a different formula which reference to rows/cells on other sheets.
Let's say on Sheet 1, the formulas in the cells C2:C9 pulls data from Sheet 2 from cells C4, G4, H4, etc. If I highlight Sheet 1 cells C2:C9 and then click to drag down to form another group of formulas in cells C10:C17, the formulas inside change their reference by 8 rows such that if Sheet 1 C2 pulls data from Sheet 2 G4, the next similar formula in Sheet 1 C10 ends up pulling data from Sheet 2 G12.
This is the problem I am having; instead of the formulas changing to reference every 8 rows, how could I make it so that it only changes by 1 row instead? Or, for every new group of formulas dragged and filled, change the references in those formulas only by 1 row. If there is still confusion, consider if the cells C2:C9 were merged as a single cell. Even if you dragged that formula down, the formula inside would still change by 8 rows despite only forming 1 cell each time.
As far as I am aware, there is no built-in Excel function to do this and I believe the only way to do this would be some VBA code, but I am unfamiliar with VBA language. Since this would be a one time thing, the VBA code could either apply this 'row formatting' as I click and drag, or the VBA code could just automatically fill in the cells for me.

Use INDEX() and some math:
In C2 it would be something like:
=INDEX(Sheet2!C:C,(ROW(1:1)-1)/8+4)
Where 8 is the spacing of the formula and 4 is the first row desired to return.
C2 would return Sheet2!C4, C10 would be Sheet2!C5
Using this you can modify the other formulas to return the desired pattern.

Related

VBA: Highlight a row with a specific column value

I am working on a very large excel 2010 workbook with several sheets.
One of the sheets is called "RatePlan" and one of the columns in this sheet is "RatePlanCde".
Some of these "RatePlanCde" (a total of 2514) are not valid anymore and have been highlighted in Red.
I need to find if "RatePlanCde" column exists in any other sheets. If so, then highlight the row with the invalid "RatePlanCde" value in all the sheets in Red too.
Can this be done using a VBA script and how?
Formula for conditional formatting is a simple VLookup
The following formula assumes that the codes in the "other" sheets is in Col A, the codes are in the Rateplan-Sheet in Col C and the "INVALID"-marker in COl D:
=VLOOKUP(A2;RatePlan!$C$2:$D2515;2;FALSE)="INVALID"

Highlight a cell if its copied to another sheet

I have a formula that will copy values from one column on Sheet B to another column on another Sheet A. What I'd like to do is highlight the cells that were copied on Sheet B and highlight the cells in Sheet A that are not on Sheet B, essentially the inverse of the first part. On Sheet B only columns G and H would be highlighted but Sheet A could be from column A to H.
=IFERROR(INDEX(Sheet2!G$3:G$7,MATCH(1,INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:‌​$B$7=$C3),),0)),G3)
You could set up conditional formatting with the same logic to change the colour or either the cell that you are setting or the one that you are copying from.
Beware: this kind of code can make your spreadsheet very slow if over used.
So in one range (sheet 2) , you would have conditional formatting set up to highlight the cell if
this match failed
MATCH(1,INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:‌​$B$7=$C3),),0)
In the other range (sheet 1 a:h)
you would highlight if ISERROR(INDEX(Sheet2!G$3:G$7,MATCH(1,INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:‌​$B$7=$C3),),0)))
has picked the value from sheet2
I may have incorrectly butchered your code, but you should get the idea. Where you currently have a range of values, select the range, insert the conditional formatting, but edit the range to only check the first cell, it will automatically increment for you (if you remove the appropriate $ signs)
conditional formatting intro

Comparing 2 excel sheets and returning a data

I'm trying to compare two Excel sheets, Sheet1 column O to Sheet2 column A:M, if it found a match, it will copy Sheet 2 "down cell of matched data" (ie match data A3, copy A4) then paste to Sheet 1 column R. I'm (very) new to VBA and Macros so I don't know how to do this. Thanks in advance!
In sheet 1 of column O - datas are USD,JPY,TWD, etc. In sheet 2 column A to M, a specified exchange rate per currency. If sheet 1 column O data is USD, it will search in Sheet 2 with matching "USD"; if cell A3's data is USD, the exchange rate is in cell A4 which will be copied and pasted into column R of Sheet 1 corresponding to the row of USD.
So what I'm dealing with is something kind of like this:
This is what I’m looking for after the macro
Since Sheet 1’s cell O1 matches Sheet 2’s cell A3, I want it to return Sheet 2’s cell A4’ s value and paste it to corresponding row of Sheet 1’s cell R1.
Let me know if this makes sense or if you need further clarification.
Thanks!
Most of the code you need you can manage by yourself using macro recorder:
Start recording new macro
Select cell O1
Copy selected cell (by pressing RMB or ctrl+C)
Go to Sheet2
Select your data (A3:M7) with mouse
Press ctrl+F to open "Find" window
Paste value of copied cell
Press Enter. Cell with searched value should be selected
Press down arrow to move selection one cell down
Copy value from selected cell
Go to Sheet1
Press right arrow to move to column R
Paste copied value
Stop recording the macro
After that you need to find how to use For loops and add it to your code. Good luck!

excel vba: Specific data columns moving to the right

I am having trouble with writing a small code of VBA that does the following: In a huge dataset, search a particular column for entries that are obviously wrong (e.g. being above 50,when they should be from 1-10).I want this to find these entries,and copy alla the columns to the right of it and move them 3 columns further to the right.It is some sort of data cleaning.Thanks
Not sure why you would need VBA. Lets say that column A should be populated with numbers between 1 and 10. And columns B,C and D are populated with other data.
Select cell E1 and put in the formula =IF($A1>50,A1,"") in the formula box and push CTRL + Enter.
Use the drag handle to copy the formula into cells F1, G1, and H1.
With cells E1 through H1 selected, double click on the drag handle of cell H1.

Get Unique data from excel by button click?

I am new to excel macros. need some help from you.I have a excel file with 2 sheets.
in sheet one i have some data
In second sheet i need a button which will fetch the distinct C column(Mname) and their ID.
Like most Excel problems, this can be done with a macro, but could also be done with just formulas. Macros are often more trouble than they're worth, so here's the formula option. There are some interesting downsides to using formulas in this way, mainly that if you do too much in a single workbook, calculation will be very slow. But as long as you're dealing with less than a few thousand rows, it shouldn't be a problem.
On your primary worksheet, enter this formula in D2, and then drag it to auto-fill down however many rows you forsee using. When you're done, you can hide column D, if it suits you.
=IF(COUNTIF($C:$C,$C2)<2,MAX($D$1:$D1)+1,0)
The COUNTIF formula looks for how many elements in column C match the contents of $C2 (where $C2 is a (partially) relative reference, so the row number will change when you auto-fill into other rows). If the count is less than 2 (the element in $C2 is unique), then the IF formula returns an index number: 1 for the first unique element in the column, 2 for the second, so on. All cells in column D where the corresponding cell in C is not unique are filled with 0.
On your second sheet, you will fill columns A and B with 'lookup' formulas, searching for positive values in column D of the primary sheet. Most people use VLOOKUP() for lookups, I prefer a combination of INDEX() and MATCH(). And here's a great example of why, since our 'unique index' is not in the first column of the primary sheet (so VLOOKUP wouldn't work!).
=IFERROR(INDEX('Primary Sheet'!$A:$A,MATCH(ROW(A2)-1,'Primary Sheet'!$D:$D,0)),"")
and for the 'Mname' column of the second sheet,
=IFERROR(INDEX('Primary Sheet'!$C:$C,MATCH(ROW(B2)-1,'Primary Sheet'!$D:$D,0)),"")
These lookup formulas are based on their own row number. So the formula in cell A2 is looking for the row in column D of Primary Sheet that contains the unique index 2-1=1. The formula in A3 looks for the index 3-1=2, etc. The IFERROR() formula that is wrapped around the lookup formula ensures that if the sought-after index is not found, the formula returns an empty string (""), which looks like a blank cell. This way you can prepare a large number of rows on your secondary sheet with these lookup formulas. And if there are only a small number of unique Mnames on Primary Sheet, your secondary sheet won't have columns full of #N/A errors.