How to input a formula into an empty cell if another cell is not empty in excel - vba

I have searched through stackoverflow and found a few topics very very similar to mine, but the answers are not specific and usually are just vba code without any explanation of how it works. Below is what I am trying to do.
1. If empty cell is populated (lets say C3)
2. then input formula into another empty cell at AT3
3. =VLOOKUP(C3,'WC Chart'!A3:G351,3,FALSE) this is my vlookup formula, it works.
I just need it to only populate in the empty cell when c3 has populated text so I need a vba way of doing it.

Like this...
'This line will place the formula in AT3 only if C3 is not blank
If Range("C3").Value <> "" Then Range("AT3").Formula = "=VLOOKUP(C3,'WC Chart'!A3:G351,3,FALSE)"
'This line will place the formula in AT3 and if C3 is blank, formula will return a blank in AT3
'And if C3 is not blank, the VLookUp formula will return an output
Range("AT3").Formula = "=IF(C3="""","""",VLOOKUP(C3,'WC Chart'!A3:G351,3,FALSE))"

Related

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

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.

Copy a number starting with 0 in vba and paste is as such

say cell A1, A2, A3 contains value "00V", cell An contains value "029"
I'm doing a comparison for consecutive cells in column A like:
If A1 not equal to A2 then ill copy the cell value of A2 and paste it into a new worksheet in Column A first consecutively.
When I compare 00V and 029 (i.e. both are unequal) ill copy 029 and paste into new sheet
But "029" gets pasted as "29"
How do I fix this in Excel vba?
You could use Format(val, "000") where val is the value of whatever you're copying.
Or you could use rng.NumberFormat = "000" where rng is the destination range of where you're copying the values to.
Convert the target column format to text and then use paste special values. You'll be able to preserve the zeros.
The easiest way is to add a apostrophe (') in front of the values. When copied, the 0 will be maintained.

EXCEL VBA Countifs with dynamic range based on cell value [duplicate]

I'd like to know how to pull cell references from the value of another cell and insert them into a formula.
For a simple example:
In cell A1 I have this: COUNT(B4:H4)
Instead of choosing the range when I set up the formula, I'd like this range to be dynamic, based on the value in other cells.
So, I want to type in cell references: in cell B4, I'd type in C5, and in H4 I'd type in C8. How can I set up my formula to look inside cells B4 and H4, but then return the range that is defined by the values in these cells (C5:C8 in this case...)
So in the end, cell A1 would return the count of C5:C8
I hope that makes sense. It seems like something pretty simple but I can't seem to find info on it.
Use INDIRECT()
=SUM(INDIRECT(<start cell here> & ":" & <end cell here>))

Setting FormulaArray to Formula skips every other row and column

A very fast way to enter formulas is like this:
Range("E5:H10").Formula = "=A1"
The column and row references adjust, so that for example the formula this code enters in cell H10 is "=D6".
However, that doesn't work for array formulas:
Range("E5:H10").FormulaArray = "=A1"
That puts the array formula {=A1} in each of the cells; no adjustments for rows or columns.
Of course you can loop through all the cells and enter a unique array formula in each one, but that isn't fast.
So I tried this:
Range("E5:H10").Formula = "=A1"
Range("E5:H10").FormulaArray = Range("E5:H10").Formula
But the results were surprising -- the references skip every other row and every other column; for example:
-- The formula in F5 is {=C1} instead of the expected {=B1}, and
-- The formula in D6 is {=A3} instead of the expected {=A2}, and
-- The formula in H10 is {=G11} instead of the expected {=D6}.
WTF?? Why would it skip rows and columns like that?? BTW I'm in Excel 2007 SP3.
For now I'm doing this, which was the fastest way I found that worked:
Range("E5").FormulaArray = "=A1"
Range("E5:H10").FillDown
Range("E5:H10").FillRight
That works but is probably slower than the direct way would be. So -- why does setting the FormulaArray property to the Formula property skip referenced rows and columns as shown above?
The A1 formula is evaluated relative to the top left cell of the range you are populating. So A1 works fine in E5, but B1 is up 4 and left 3 from E5, so the formula entered into F5 refers to the cell up 4 and left 3, which is C1. This repeats for the other cells. The simple solutions is to use:
Range("E5:H10").FormulaArray = Range("E5:H10").FormulaR1C1
since that formula is the same for every cell.

Copy and paste column data based on date specified

In an Excel worksheet, cell C1 contains a date. I would like a macro that checks the date in C1 and if the same date is mentioned in row 3, then copy all matching date data beneath it from D6 downwards, paste to Sheet2, cell B3 and also copy column A downwards from row 6 to Sheet2, cell A3.
Use Excel's macro recorder to record your keystrokes and mouse actions (as Excel actions) then inspect the resulting macro. Using Excel's help, check the topic Create a macro or visit http://office.microsoft.com/en-us/excel-help/create-a-macro-HP005204711.aspx. This will get you started.
are you checking the date in C1 against any column in row 3? or in D3? or C3? It is not clear.
copy A6 down and D6 down, but switch column D to B on sheet2 and A to C on sheet2?
This could be easy if it were clearer what you are checking.