add listrow to empty listobject - vba

With some VBA code in Excel I have an odd problem when adding a row to an empty listobject. Does anyone know what is happening or how to fix it? Here is the problem:
First I delete the contents of a listobject (table), using:
AListObject.DataBodyRange.Delete
Then I have a loop that iteratively adds a row and fills it with data. I add the row by using:
AListObject.ListRows.Add
Here is the odd behavior. First, the code deletes the listobject's data:
AListObject.DataBodyRange.Delete
The immediate window shows AListObject.ListRows.Count equals 0, so all rows are deleted. Then I add a row:
AListObject.ListRows.Add (first iteration)
Immediate window shows AListObject.ListRows.Count equals 2, so the Add method created two rows, instead of one. Furthermore, the data written to row 1 is lost. Row 1 of the listobject (table) displays empty cells.
For the remaining iterations (2 to n) of the loop the ListRows.Add method works fine, creating only one row. Writing data to those rows (2 to n) works fine as well.
Why does the first Add method, called when the listobject is empty, create two rows, and why can't I write to row 1? This code previously worked fine, and it works fine in old back-up versions of my workbook. Does anyone know why this is odd behavior occurs?
Thanks.

JNevill, thanks for responding. With some more persistence I solved the problem. A filter was set, and calling AListObject.DataBodyRange.Delete when the filter was set caused the problem. The solution was to remove all filters prior to calling the delete method, as follows:
AListobject.AutoFilter.ShowAllData
AListObjct.DataBodyRange.Delete
Thanks again for responding, and if I have an occasion to to post again, I'll follow your advice and post the actual code.

Related

VBA Excel - copy, paste loop with offset

I am new to VBA and need some help - if anyone could help me.
I need to copy a range, say A1:F1, and paste it in the row directly under, so A2:F2, on an automatic loop.
The top-most row contains data from a different sheet, therefore I will be copying the formulas of that row which links to the secondary sheet. I would like the loop to end once the other sheet has been exhausted and the macro hits an empty row from the sheet.
Any ideas?
You can use a do until loop to find the length of your list. Then u can use this index to use range, push down the data and push your new data on the now empty top row.
I understand, that u want to push data always in the first row and everything else down, so you add always one new row.

SUMIFS returns 0 using dynamic criteria, with criteria range and sum range on another sheet

Anyone,
I've chatted with and called excel customer service with no luck. I used the formula builder (please see attached screenshot) to make sure each element of the formula is correct and returns the value for the criteria I'm trying to reference.
Everything is accurate, but it returns a value of 0. When I do the same thing in the actual sheet the data is stored in (and click a criteria cell within the criteria range) it returns the accurate value?! I'm not sure why it won't work on the other sheet. The values I am using to select are dynamic and change with a drop down. I have another, advanced, workbook (I did not create) that does the same thing and completes an even more complicated formula, but actually works so I'm not sure why this is returning a 0 value.
Photos and code/syntax: Dynamic Selection, Example 2 of it working, Example 1 of it working, Formula Builder, CountIFs, Advanced Spreadsheet working, VLOOKUP
=SUMIFS('GFEBS Pull'!Q:Q,'GFEBS Pull'!G:G,FMCOP!$C$20,'GFEBS Pull'!H:H,FMCOP!B23)
or:
=SUMIFS('GFEBS Pull'!Q:Q,'GFEBS Pull'!G:G,'FMCOP'!$C$20,'GFEBS Pull'!H:H,'FMCOP'!B23)
When I type ' around FMCOP sheet name, they disappear? I've also tried to lock the columns on the 'GFEBS Pull' sheet with no luck. Cell B23 is not locked because I'm going to copy the formula down to reference other cells. Any help is appreciated!
In this screenshot you can clearly see that both FMCOP!C20 ansd FMCOP!B23 have prefacing spaces; e.g. " HHC".
Since " HHC" will never match "HHC", fix the data returned from 'the lower table in the same screenshot'.
A Text-to-Columns, Fixed Width, Finish should do this. You could adjust the original formula like,
=SUMIFS('GFEBS Pull'!Q:Q, 'GFEBS Pull'!G:G, TRIM(FMCOP!$C$20), 'GFEBS Pull'!H:H, TRIM(FMCOP!B23))
I would caution against the latter 'bandaid' fix. Fix the original data; do not apply bandaids on-the-fly.

INDIRECT If Statments keeps giving #REF

I'm trying to change an if statement to an indirect one as the current one that works keeps changing from C27 to #REF! when I delete Row 27 in the Tracker sheet.
=IF('Tracker Sheet'!C27="","",'Tracker Sheet'!C27)
This is the old if statement that works
=IF(INDIRECT('Tracker Sheet'!C27)="","",INDIRECT('Tracker Sheet'!C27))
This is the if statement that I try to convert but gives me #REF!
Any help would be greatly appreciated
Use index:
=IF(INDEX('Tracker Sheet'!C:C,27)="","",INDEX('Tracker Sheet'!C:C,27))
As long as you use the cell address directly when it is deleted it will return #REF as you are deleting the reference.
This will always look at what is the current 27th row in column C.

Range("CustomTable").SpecialCells(xlCellTypeVisible).Delete now fails. Run-time error '1004'

was wondering if anyone noticed a change in behaviour of the following or similar code:
.Range("CustomTable").SpecialCells(xlCellTypeVisible).EntireRow.Delete
I use it to delete filtered range on a ListObjects table in excel 2013 and until about last week it was working fine; and now, if there are at least two non-sequential lines needed to be deleted, it is throwing an error: "Run-time error '1004': Delete method of Range class failed. I am sure it is not just the case that nothing is visible in filtered data-set, I ran it in debug and it definitely has multiple lines to delete, and it does give a normal entire row address with multiple lines to delete, but it fails to.
I have solved it by stripping out EntireRow and suppressing excel alerts on confirmation menu if I want to delete entire row. I am just quizzed why it suddenly stopped working?
I've come across this problem as well. What I've found to work is to save the range, remove the filter and then iterate through the areas of the range in reverse order (required as ranges change as you delete).
I've added in the "Application.Union" to take care of hidden columns. I just found a case of hidden columns creating multiple areas for the same row. So the solution to that is to get the SpecialCells range with the EntireRow, which still gives you duplicate areas for full rows. Then using the Application.Union you can compress these into a unique set of areas in a range.
Set delete_range = Application.Union(.Range("CustomTable").SpecialCells(xlCellTypeVisible).EntireRow, .Range("CustomTable").SpecialCells(xlCellTypeVisible).EntireRow)
.AutoFilter
Set delete_range =
For i = delete_range.Areas.Count To 1 Step -1
delete_range.Areas(i).EntireRow.Delete
Next
Hope that works for you. The disabling alerts didn't solve my problems. Everything else I tried had corner cases that didn't work. I assume you've already catered for removing the header (if you have one) from the range of interest.
NOTE: I also had another strange case which was resulting in the range = nothing. Can't remember the reasons for this happening, but I also included a check for nothing of the range before processing. I didn't include that in this answer.
Just to clarify: I have found a work around it, therefore not looking for one, I am just trying to understand why this line worked OK previously and doesn't work now.
In case anyone stumbles into this post whilst searching for the solution the line can be replaced by the following 3 lines, giving same result:
Application.DisplayAlerts = False
.Range("CustomTable").SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
P.S. I am personally not a big fan of suppressing alerts, but feel like this is the most optimal fix...

Copy row if value of one cell is "x"; and, auto-hide blanks

I'm trying to copy a row, columns A-J, representing a person and associated info, when column J is filled. I'd fill it with either w, x, y or z based on the reason people are away, and I'd like the row (A-J) copied to another sheet as soon as J is filled.
Here's a screenshot with both Excel documents. Please ignore the "Reason" column in the Pers sheet - I know it could be used, but my production one doesn't have this column, and will not. I just used it to illustrate for you guys.
That other sheet (Main) has headers, as you can see, for each reason why people would be away. This one would permanently be displayed on a big-screen TV. So, what I'm trying to do is :
Copy the rows to Main as soon as the "Departed?" row is filled in Pers;
Make sure those rows go into the proper category, in the first blank row;
Ensure each category remains collapsible.
You can see I've tried using the If function (syntax in screenshot). That worked OK, but :
Left blanks;
Biggest one : only worked row-for-row! (Also the cause of reason #1, I suppose...) Past row 11, nothing. Not too sure what to do about that...?
Hope this is somewhat clear? =) Let me know if you have any questions - thanks for your help!