Find and correct all inconsistent column formulas with VBA - 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.

Related

Macro changing format of unreferenced cells

I've got something weird happening in a macro worksheet. I have a macro that runs an SQL statement, and then copies the returned table into a worksheet. However, for some reason, the line of code below:
oWorkSheet.Cells(iRow + 1, iColumn).CopyFromRecordset (objMyRecordset)
changes three cells on a different worksheet (not the one it is pasting the data on) from number format to date format. It's got me completely stumped. The three cells aren't next to each other - just scattered throughout the sheet.
Any thoughts? I know I could just put in some code to change the format back, but that seems messy.

Excel Formula to VBA code Conversion

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

complex VBA to remove duplicates from multiple pages and then delete blank rows looping all sheets

I've spent hours on the internet trying to work something out but can not get a vba fix to work on a complex sheet like mine! Your help would be appreciated.
For all these queries I'd like to loop through all sheets, but skip the sheets named "timekeeper code", "bill date" & "summary" - these names will always be the same but I can not name the sheets. The script needs to loop as the names and quantity will vary.
On all other sheets it will loop I need to select the range "A1002:A2003" and if there is a duplicate delete the row.
I also need to do the same for range "A2005:A3006".
Please note that both of these ranges are with in tables but again the table names can't be named as they will vary.
I'm not sure if it helps but the full table range for A1002:A2003 is A1002:B2003
and the full table range for A2005:A3006 is A2005:AD3006
I can also not go from A1002 straight to A3006 as some values will be shown in both ranges but I will need them both
looping the same sheets,
I then need to delete all rows from A1001 upwards to the last used cell where the cell value is = ""
I have a very limited skill on VBA and quite a complex query so I'm basically stuck starting from scratch so any code you have to do this would be amazing!
I hope this makes sense.
Many thnaks
First google entry solves your problem:
Delete all duplicate rows Excel vba
now you have to generate a loop around this function to target the correct sheets.

Check if value in Sheet A is in Sheet B, return true, or false to respective row

I'm working with two Excel sheets of over 500k rows, basically I need to validate if value from Sheet A (Column A) is in Sheet B (Column B), if so, return "True" to Sheet A (Column C) in it's corresponding row or "False" if not found.
Initially I tried the validation with vlookup (also tried match) in a separate sheet, but since sometimes I hide rows (filtered list) I encounter problems when trying to paste back the results to the original sheet because it won't paste to visible only cells although I've tried countless times. (Yes I've seen the tutorials on how to paste to visible cells only but it won't work, maybe Excel version?) Excel version is 2010.
Also things get a bit iffy when working with so much data!
How can this be accomplished in VBA?
Update: I tried what Jeeped suggested Is there a faster CountIF and it looks like what I need, I was able to actually paste the formula into all the rows without Excel crashing. Thank you Jeeped! I'd still like to know how to accomplish this with VBA to perhaps expand it later with more customization.

vba excel AdvancedFilter method for a table with dates criteria does not work

first af all i'm glad to enjoy this nice community, this is my first post so apologize me in advance for any mistake i'll do in the post.
I got a table named example generated by importing data from an access file, so the number of rows changes dynamically...I'm trying to apply the AdvancedFilter method to this table via VBA.
I've used the first row of the sheet for copying the header of the table and in the second row i set my parameters for filtering, so in the range A1:D2 i got the criteria. Starting from row 5 i have the table example: it starts with a header row and goes on with all the rows of data, all imported from the access file, for example a range A5:D20.
If i apply the advanced filter manually (click on the advanced filter button and fill the form) specifying the range of the table A5:D30 and the range of the criteria A1:D2 it works fine...but if instead of specifying the range of the data i use the name of the table example the result is different, just one row instead of the five (right) rows i expect...why?!?
Now, if the question above is just for curiosity (but i'm sure it's connected with my problem), the main problem is that i'm trying to do the same thing via VBA code. I tried
Worksheets("name").Range("example[#All]").AdvancedFilter _
Action:=xlFilterInPlace, CriteriaRange:=Range("A1:D2"), Unique:=False
and
Worksheets("name").ListObjects("example").Range.AdvancedFilter _
Action:=xlFilterInPlace, CriteriaRange:=Range("A1:D2")
but both don't work: the filtering action didn't return no lines.
Even if i try
Worksheets("name").Range("A5:D30").AdvancedFilter _
Action:=xlFilterInPlace, CriteriaRange:=Range("A1:D2"), Unique:=False
specifying manually the range (not a solution for my problem because the range changes dynamically, but for test purpose...) the problem persists, the code didn't return no lines.
Note: if i select the range in the codes above instead of AdvancedFilter it the selection is correct: the entire table with the header
The first code is the same automatically written if i start recording a macro, do the filtering manually, and stop recording the macro (code written exactly with the table reference)...the wierd thing is that when i manually do the operation (while recording the macro) it filters correctly, but if i start the macro just recorded...nothing, same as if i run the code written above.
If i open the formula->names tab i can find the example table but the range specifyed is not starting from row 5 but from 6...it's like the header is not considered, don't know why...but it's automatically generated with the importation of the data so i don't know how to change the range keeping the reference to the imported data...maybe is this the issue?!?
Don't know how to solve the problem, please help me :)
Edit:
Two of the criteria i'm using for filter are date criteria, specifically: date <= xx/yy/zzzz and date >= aa/bb/cccc and the problem it's exactly this...if i try the three codes above filtering with a string criteria, they works, if i use them with the dates criteria, they don't. So i guess the problem depends on how VBA hands the dates data types. Any suggestion?
The cells used for criteria contain this formula ("SE" is the italian version of "IF"):
=SE(filtro!F2="";"";">="&filtro!F2)
=SE(filtro!F3="";"";"<="&filtro!F3)
i take the criteria from another sheet ("filtro") and if the cell is filled i report the data inserted with <= or >= for the criteria, otherwise the cell remains empty for don't set any condition.
Thanks
Ettore
Try this below steps. It is working for me.
First we should create a named range (Define Name) (call it is Table),give the range as dynamic using the formula =OFFSET(Sheet1!A5,0,0,COUNTIF(Sheet1!$A$5:$A$100,"<>"),4).
Paste this offset formula while creating the name range - Table.
This formula will changes the range dynamically.
Write VBA code as below
Sub Advanfil()
'
' Advanfilt Macro
'
'
Range("Table").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Range("A1:D2"), Unique:=False
End Sub
I think the above should ease ur difficulty.
Ok, finally i fix the issue and whant to share the solution.
As i mentioned in the edit of my question, two criteria of the filter are dates, i found two problems and both are connected with this:
When i imported the data from other source (access file) and populated the table that i was going to filter i did not set the appropriate format to the fields, i left all as excel decided and this was a problem. The correct way is to set:
The fields in the table (the ones with the data to be filtered) as date with a preformatted format (not simple for me because they were stored as text in the access so i must made a SQL string for extract data in the correct type and format).
The field where store the condition for the filter as general (in my case this field contains the formula mentioned int the edit of my question).
The field where i insert the dates for filtering (the one referenced in the formula) as date with the same format as the fields of the table (point 1.).
The formula mentioned in the edit of my question looks at a cell (where i insert the date) and, if this cell is filled, returns something like <=data-inserted or >=data-inserted (and this is OK) BUT when nothing is inserted in the cell returns "". For a string criteria in the filter this is ok, but for a date criteria is not, this causes the filter to don't find nothing with "" in the table fields filled with dates. I tryed to use null value or other similar values but i couldn't find what is nothing for a date criteria...so the workaround i used is to return <=31/12/2100 or >=01/01/1900 when i don't whant to limit the grater or the lower dates.
Hope this could help someone.
Ettore
Fill CRITERIA for null or "" cell in date-formatted field with just an equal sign =
Excel Advanced Filter understands that means BLANK cell