Excel Formula to VBA code Conversion - vba

I am trying to create a macro that allows me to scan columns and rows of data and insert a formula into the blank cells. I am able to complete this task with the following excel formula:
=IF(ISBLANK(W4),((IFERROR(DATEDIF(MAX($P4,DATE(2016,5,1)),MIN($Q4,DATE(2016,8,1)),"d"),0)/(DATEDIF(P4,Q4,"d")))*$T4),W4)
My question is, is there a way I can put this into vba code so that I can run a macro that will automatically apply this formula in a column of my excel sheet across 30 rows? Therefore, the next row would read:
=IF(ISBLANK(W5),((IFERROR(DATEDIF(MAX($P5,DATE(2016,5,1)),MIN($Q5,DATE(2016,8,1)),"d"),0)/(DATEDIF(P5,Q5,"d")))*$T5),W5)
Thanks in advance for the help!

You can use
Range("RangeToCopyFormulaTo").Formula = Range("CellToCopyFormulaFrom").Formula
Excel will take care of updating the cell references, same as when you copy/paste

Related

How can I import Excel cell reference or formula store in SQL table?

I have the following SQL table.
I imported this table into Excel sheet by following
Data-->Get Data-->Database-->Import from SQL database
On the Excel sheet, I have income calculated in cell J1.
I want to give reference of cell J1 in my SQL table which I have done above.
Excel output is
In cell C2 cell reference/formula is importing from SQL table and in cell C4 it is written directly.
It is not working in cell C2 but the same formula is working on cell C4.
How can I bring cell reference or formula from the SQL table?
There is a quick way to do evaluate the formula in Excel after refreshing the data from the SQL server.
Replace = with = in Excel. It's the same character but it evaluates the formula.
I found this interesting answer in another SO answer post. You can also create a macro and run it every time after refreshing the data as described in another answer in the same link.

How to copy selected columns and filter them before hand in excel VBA

I am just a beginner in VBA. I am trying to copy some data from one workbook that is updated daily to a master woorkbook and generate a report. I want it to first filter one of the columns for nonzero values and copy it with three selected columns for example columns T,C,N. I have looked everywhere for an answer but I haven't succeeded yet. Please help.
You can check if a given cell has value 0 by something like this If Sheets(sheetname).Cells(rownumber,columnnumber)=0 Then
You haven't specified what do you want to do on the other workbooks with the cells that were empty.

Find and correct all inconsistent column formulas with VBA

I have an excel workbook with 5 worksheets in each of this worksheets there is a table with values.
I then delete some of the rows (using VBA) depending on user selection (using dropdown list).
After my VBA code deleted all the unnecessary rows Excel states that I have "inconsistent column formulas" which I'd like to resolve with VBA before the user sees it.
Is there any way to do this with VBA?
I've searched Google the whole day now and still found nothing usefull and the only thing I'd have in my mind would be iterating through all Rows and Columns with formulas in it, checking, if the formula contains an error, which would definitely be super slow...
Note: If this counts as duplicate of Find inconsistent formulas in Excel through VBA I'm sorry, but the only answer there doesn't work with tables as data range
If you are trying to reset a formula in a Table, you can use the DataBodyRange.Formula property to reset the formula for the entire column. This addresses one way to get the Inconsistent Calculated Column Formula.
Example of the error was obtained by setting the formula for the column, changing one cell, and then telling Excel not to change the formula column after that edit.
To revert this back to a column formula (and remove the error), you can run VBA that changes the formula for the DataBodyRange.
Code to change back
Sub ResetTableColumnFormula()
Dim listObj As ListObject
For Each listObj In ActiveSheet.ListObjects
listObj.ListColumns("b").DataBodyRange.Formula = "=[#a]"
Next
End Sub
Note that I am being a bit lazy by iterating through all ListObjects instead of using the name of the table to get a reference. This works since there is only a single Table on the Worksheet.
Formulas after that code runs:
Note that this answer is very similar to the answer here.

Changing Formula sheet reference with Macro

Hello everyone i'm new to this site! i wanted to see if anyone could assist with a concept i believe is possible but don't know how to achieve it.
Essentially i have a formula that has Vlookups and references other sheets, this formula is the same but the sheet referenced changes for each column as each column references a different sheet. this is going to be done 135 times over 8 times.
=IF((IFERROR(VLOOKUP(D3,'[2015_Big_Book_Communication_10_19_15.xlsx]**Credit P-1**'!$C$2:$O$5000,9,FALSE),"Not Scheduled"))=0,1,IFERROR(VLOOKUP(D3,'[2015_Big_Book_Communication_10_19_15.xlsx]**Credit P-1**'!$C$2:$O$5000,9,FALSE),"Not Scheduled"))
I want to use a macro to change the bolded sheet reference based upon a cell.
my idea is to have all the sheet names listed in a column and have the macro edit the equation for each row and then just paste the formulas transposed.
Is this possible?
Use the following formula:
=IF((IFERROR(VLOOKUP(D3,INDIRECT("'[2015_Big_Book_Communication_10_19_15.xlsx]"&H3&"'!$C$2:$O$5000"),9,FALSE),"Not Scheduled"))=0,1,IFERROR(VLOOKUP(D3,INDIRECT("'[2015_Big_Book_Communication_10_19_15.xlsx]"&H3&"'!$C$2:$O$5000"),9,FALSE),"Not Scheduled"))
In a column ("col H" in this example) write the sheet names and use indirect formula to refer to those names.

How To Display Excel rows on selection of first Excel Column Value

I have Two Excel sheets. My requirement is when I select a reason value from one Excel sheet Reason_Name column, it will display that reason value in a second Excel sheet.
So using Macro, I want to display the second Excel rows on selection of reason in first Excel.
Please Help.
This is the first Excel sheet - Reason_Name column contains Reason1, Reason2, etc.
alt text http://www.freeimagehosting.net/uploads/a10d6be7a5.png
This is the second Excel sheet
alt text http://www.freeimagehosting.net/uploads/99e0ff4cdb.png
Here's something that may get you started. (I think this is close to what you would like to do.)
Create a named range for the data on the second sheet. Named "new_range" in my example.
Then create the following procedure in a new module:
Sub FilterSheetTwo()
Worksheets("Sheet2").Range("new_range").AutoFilter Field:=6, Criteria1:="Reason1"
End Sub
When you run this procedure, it should filter the results on Sheet2.
You can then hook this procedure up to an Worksheet_Change event on Sheet1.
If you can sort your Reason column on the second sheet and place it as the left-most column you don't need a macro--you can do this using VLOOKUP. Steps:
Sort Data by your Reason column on the second sheet.
In each column of the first sheet enter the following formula:
=VLOOKUP(E2, DataRangeOfSheet2, ColumnYouWantFromDataRange)
See VLOOKUP for more info.