Excel Table, using VBA to change data in a column - excel

I have a Table with a column that I added, that checks the date of that row against a formula.
The result of that formulae is TRUE or FALSE and a subsequent Pivot Table Sums a value in the TRUE rows. I Introduced it to get what is called a Rolling Total Income.
There are two formula and I want to swap from one to another using VBA.
The formula are:
=IF([#Date] = "", "", AND([#Date]>=EOMONTH(TODAY(),-13)+1,[#Date]<EOMONTH(TODAY(),-1)))
for the Rolling data.
=IF(AND([#Date] >=G1,[#Date] <=H1),"TRUE","FALSE")
for the Sum of Income between G1 and H1, the financial year start and end dates.
The VBA I have tried is as follows:
Set lo = Sheet2.ListObjects(1) 'Sheet2 is the Income sheet see Project list
lo.Parent.Activate 'Activate sheet that Table is on
Set lColName = lo.ListColumns(9)
lColName.DataBodyRange.Cells(1).Formula = [=IF(AND([#Date] >=G1,[#Date] <=H1),"TRUE","FALSE")]
Running the above errors and gives #VALUE in the first cell in that column, and doesn't ripple through the rest of the column.
What have I done wrong?

Try
lColName.DataBodyRange.Formula = "=IF(AND([#Date] >=$G$1,[#Date] <=$H$1),""TRUE"",""FALSE"")"

Thanks, those changes have sorted it.

One more refinement I am struggling with.
The IF test statement is testing a date against cells, $G1 and $H1 which I set up for ease of debugging. However I have another worksheet in the workbook where I store all the references/static data I use in the various Sheets and I have placed those dates there. In this Module, before the IF statement, I have set those dates against two variables.
startdate = Range("I2").Value
enddate = Range("J2").Value
I now want to use those in the IF statement but cannot get the correct syntax!
This just errors:
"=IF(AND([#Date] >= startdate,[#Date] <= enddate),""TRUE"",""FALSE"")"
How should I write it?
Thanks

Related

How to clear a cell based on hours old, Excel VBA

I am trying to clear the contents of a cell within Microsoft Excel 2016 on one sheet when its age is over 8 hours. We review all incoming work after 8 hours and allocate additional resources if required.
I have 3 sheets Test, VLOOKUP and Log. Test is the only one visible to the general user, VLOOKUP holds the Vlookup tables to work out age, Log is exactly that, a log of all changes made.
Through the use of a a circular equation I have been able to output the time when a cell is altered, this is put into the VLOOKUP sheet -
=IF(Test!E6<>"",IF(K4="",NOW(),K4),"")
Within the VLOOKUP sheet, I have a cell that shows NOW (H3).
I want to be able to use the data from the VLOOKUP, K column, to feed the clear contents of the Test E:G columns, 1 row at a time and only if the K cell is over 8 hours old.
I have tried a number of options but, have not been able to clear another sheet based on VLOOKUP sheet values.
This is my current working -
Sub DeleteRowsBeforeCutoff()
Application.ScreenUpdating = False
If Worksheets("VLOOKUP").Range(K3) < DateAdd("h", -8, Now) Then
Worksheets("Test").Range(E5).ClearContents
End If
Application.ScreenUpdating = True
End Sub
Thanks in advance for any pointers!

COUNTIF with date range and multiple criteria

I am trying to simplify a process of counting how many of a specified criteria are in a table.
What I need is to count the number of items that meet all of the following criteria:
[BusinessArea] = "Corporate"
[Application] = "CS"
[Status] = "Resolved"
[ResolvedDate] = *if the resolved date is between DateA and DateB eon a
separate worksheet.
I can do it using VBA or Formulas but I just cannot figure out the date part. I have them figured out separately as:
=IF(AND(Sheet1!I71 >= (TODAY()-7), Sheet1!I71 <TODAY()), TRUE, FALSE)
Where i71 is the [ResolvedDate] (it is searching just this one entry without the other filters.
=COUNTIFS(Table8[Business Area], "Corporate", Table8[Application], "CS")
Where it counts the number of entries that are Corporate_CS entries.
What I currently have:
'=COUNTIFS(Table8[Reported Date],AND(Table8[Reported Date]<='Ticket Summary'!F61, Table8[Reported Date]< TODAY()),Table8[Business Area], "Corporate", Table8[Application], "CS")'
Where F61 is a previous date (beginning of range)
Of which it is returning 9 instead of 6. There are 9 entries that match the criteria, 6 matching the date range and criteria
Try this:
=COUNTIFS(Table8[BusinessArea], "Corporate", Table8[Application], "CS",Table8[Status], "Resolved", Table8[ResolvedDate], ">=" & I61, Table8[ResolvedDate], "< " & I71)
I'm assuming that I61 is the start date and I71 is the end date.
I used this as a guide for the dates: count-cells-between-dates
Multi-criteria counting is a little tough in Excel. I used to make extra columns and concatenate values. Then you can use countif without too much trouble. But it was messy. You could also use DCOUNTA by setting up your query parameters on another part of your sheet (but it doesn't like tables - only cell ranges). Again, its a little messy, but it's very flexible.
My preferred method is to use the SUMPRODUCT function. Everything can be done in one formula and it works with tables. To get your count using SUMPRODUCT:
=SUMPRODUCT((Table8[BusinessArea]="Corporate")*(Table8[Application]="CS")*(Table8[Status]="Resolved")*(Table8[ResolvedDate]>DateA)*(Table8[ResolvedDate]<DateB))

Pivot table and Dates

I am a newbie, and I am sort of a enthusiastic learner, a beginner who is making many mistakes. On my pivot table which is picking up two years worth of dates, I am keen to only choose the first two most recent dates, rather than using a slicer. I have tried to make it a little bit dynamic, but I am constantly getting error messages,
I was trying to have cells in my spreadsheet which is from date and a two date, or a code that picks the first two most recent dates. I really want the code to work and pulling my hair out, as I have few other pivot tables. Every time I put the cell reference in between, where the dates are I keep getting coding errors.
I want the pivot items more dynamic as like before, I will try to add a validation on the date from and date to make it lot more dynamic.
I want the pivot items to be x first date and y as the second date referring to two cells on the sheet, which I can amend on the sheet, and another code that just picks the first two most recent dates.
Sub DateSlection ()
With ActiveSheet
.PivotTables("PivotTable4").PivotFields("Date")
.PivotItems("11/2/2016").Visible = True
.PivotItems("10/26/2016").Visible = True
End With
End Sub
kind regards
Ali
For the dynamic filter you could do something like this:
Sub DateSlection ()
Dim startDate As String
Dim endDate As String
With ActiveSheet
'Set your date parameters in the required date format
'- looks like you need US
startDate = Format(.Range("A1").Value, "mm/dd/yyyy")
endDate = Format(.Range("A2").Value, "mm/dd/yyyy")
'clear the existing filter and add the new
.PivotTables("PivotTable4").PivotFields("Date").ClearLabelFilters
.PivotTables("PivotTable4").PivotFields("Date").PivotFilters.Add Type _
:=xlDateBetween, Value1:=startDate, Value2:=endDate
End With
End Sub
I also understand you want to get the first two dates in the pivot table? If so you can do this:
ActiveSheet.PivotTables("PivotTable4").PivotFields("Date").PivotItems(1)
ActiveSheet.PivotTables("PivotTable4").PivotFields("Date").PivotItems(2)
To get the last two dates you can do:
With ActiveSheet.PivotTables("PivotTable4").PivotFields("Date")
ptCount = .PivotItems.Count
last = .PivotItems(ptCount)
secondLast = .PivotItems(ptCount - 1)
End With

Excel VBA changing my formulas in a table?

Has anyone come across a situation where Excel seems to manipulate your formulas.
I have a sheet where I have an Index value in Column A. The First row starts with any non zero Value. Subsequent rows in the column increment the value. Eg
A1 = 1000
A2= A1+ 1
A3= A2 + 1
and so on/
I have another column B whose values will either be blank or a formula pointing to column A(usually the subsequent rows)
Eg:
B1.Formula = "=A2"
B2.Formula = "=A3"
B3.Value = ""
B4.value = "=A6"
Now I have a backup-restore functionality that lets me write out the data/formulas to a text file and then read it back in another workbook.
In the case of columns A and B, I am checking if the text value starts with "=" and then set either the value or formula of that cell depending on whether there is a formula or not.
So far the functionality has worked fine. It lets me restore accurately.
Now, if I convert this data range to a table and modify the code accordingly the behaviour is strange. I am using the ListObject structure to refer to the table. So for Column B my restore code is:
If Left(soureString) = "=" Then
'This is a formula
Sheets("MySheet").ListObjects(1).ListColumns("Next").DataBodyRange(row).Formula = sourcestring
Else
'This is a value
Sheets("MySheet").ListObjects(1).ListColumns("Next").DataBodyRange(row).Value = soureString
End If
once I am done writing a row, I loop to the start and
Dim newRow AS listrow
Set newRow = Sheets("MySheet").Listrows.Add(AlwaysInsert:=False)
row = newRow.Index
But this time when I run the process. this is what I get:
B1.Formula = "=A5"
B2.Formula = "=A5"
B3.Value = ""
B4.value = "=A5"
Why are my formula values all changing to the same value when I use a table instead of a range?
I had the same issue when populating a ListObject (Table) from an Excel Add-in, setting AutoFillFormulasInLists was the solution.
My workaround is to save the current setting, set AutoFillFormulasInLists to false, populate the table with data, formulas etc, then set AutoFillFormulasInLists back to the original setting.
bool OriginalAutoFillFormulaInListsFlag = app.AutoCorrect.AutoFillFormulasInLists;
app.AutoCorrect.AutoFillFormulasInLists = false;
//[ListObject population code....]
if (OriginalAutoFillFormulaInListsFlag == true)
{
app.AutoCorrect.AutoFillFormulasInLists = true;
}
Hope this helps someone.
I faced a similar issue. Ideally you could tell excel to stop doing this but I haven't been able to figure out how. Supposedly doing the following is supposed to keep excel from copying the formulas:
xlApp.AutoCorrect.AutoFillFormulasInLists = false
but it didn't work for me.
Using the answer from this question How to create running total using Excel table structured references? helped me. It doesn't feel like the ideal solution but it does do the job.
I used this formula where Weight is a column name from my table. #This Row is a "Special item specifier" and has a special meaning. The syntax looks a little funky because it's what's called a Structured Reference:
=AVERAGE(INDEX([Weight],1):[[#This Row],[Weight]])
The INDEX([Weight],1) part gives the reference for the 1st row in the Weight column
While the [[#This Row],[Weight]] part gives the reference for the current row in the Weight column.
So for example, if Weight is column J, and the current row is, say, 7 then this is equivalent to
=AVERAGE(J1:J7)
and on the 8th row it will be equivalent to
=AVERAGE(J1:J8) and so on
I have found that the only way to solve the problem of formulas changing in Excel Tables when you insert in VBA is to insert at the first row of the table, NOT the bottom or the middle. You can sort after.
And I always select or reference the EntireRow to do my insert in the Worksheet object not in the table itself. I always put a table in its own Worksheet anyway using xx.Entirerow.Insert.

Suppressing pivot items in a pivot chart

I have created a pivot table that lists the number of visitors to a store on a given date. There are 3 columns of data, the date column and two columns of numbers representing the visitors. When I create a chart from this data I get a bar of data that represents dates before and after the range of dates that make up the data. I can suppress all dates that occur prior to the dates I want to view by using the command:
With Worksheets("Pivot Tables").PivotTables("Weekly Statistics").PivotFields("Date")
.PivotItems.Item(1).Visible = False
However, I can't find a way to suppress the dates that occur after today's date. This leaves me with a column on my bar chart that is blank and has the axis value ">3/27/2013". I can suppress it by actually typing this line:
.PivotItems.Item(">3/27/2013").Visible = False
but having to manually do this every time I update the sheet is laborious and makes the sheet unusable to anyone else.
I tried to create a variable that would update the value inside the () but I can't get it to work.
Dim t
t = Worksheets("Data").Range("i3").Value
.PivotItems.Item(t).Visible = False
(where i3 is a cell in the Worksheet("Data") that is a concatenation of the date; in this case the cell contents are ">3/27/2013" )
Thanks
You could try to use alternative filtering technique:
t="3/27/2013"
Worksheets("Pivot Tables").PivotTables("Weekly Statistics").PivotFields("Date").PivotFilters.Add Type:=xlBefore, Value1:=t
However, it is used as of Excel 2007.
If not working try to start with:
Worksheets("Pivot Tables").PivotTables("Weekly Statistics").PivotFields("Date").ClearAllFilters

Resources