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

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!

Related

Excel Table, using VBA to change data in a column

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

Pause VBA from running until Excel calculated the sheet a second time

A B C D E
1 Selection: DE
2 Value 1 200 =IF($B$1="DE",100,200)
3 Value 2 100 =IF($B$1="DE",300,800)
4
5
In the above Excel Sheet I can enter a country in Cell B1.
Based on the country the values in Range("B2:B3") switch accordingly as you can see in the IF formulas.
I copy the values of one country into Range("C2:C3") and another country into Range("D2:D3").
I use the below VBA to switch the country in Cell B1 and copy the values:
Sub Paste_Values()
Sheet1.Range("B1").Value = "DE"
Sheet1.Range("B2:B3").Copy
Sheet1.Range("C2:C3").PasteSpecial Paste:=xlPasteValues
Sheet1.Range("B1").Value = "US"
Sheet1.Range("B2:B3").Copy
Sheet1.Range("D2:D3").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
In the simple example all this works perfectly.
Now, I have the issue that I want to apply the same logic in a much bigger file.
In this file I get the issue that somehow Excel calculates the IF formulas in Range("B2:B3") two times.
The first time it calculates #NA appears in the cells.
The second time it displays the correct numbers.
However, the VBA does not wait until Excel calculates it a second time.
Therefore, instead of the numbers it copies #NA in Range("C2:C3") and Range("D2:D3").
Is there a way to somehow let the VBA wait until the numbers are displayed in Range("B2:B3") so it is ensured that there is no #NA and the correct numbers are copied?

Macro is not reading the correct value from a cell which contains a math formula

I have a Excel Workbook that is a template and is populated using macros.
In a several cells, I have a formula =abs(sum(H20:P20)).
In my macro, I am trying to hide the row based on the value in that cell.
I reference those cells from a named range using an offset that does not change and then iterate over 8 lines to decide whether to hide them or not.
I have tried various 'Evaluate' and 'Calculate' functions on the cells in question thinking that maybe it was not evaluating the formula in the cell.
For s = 1 To 8
If Worksheets(sheet1).Range("Spend").Offset(s, 15).Value = 0 Then
Worksheets(sheet1).Range("Spend").Offset(s, 15).EntireRow.Hidden = True
End If
Next s
I expect it to hide several of the rows but it does not do anything. After all macros are done in the file; and the rows have not been hidden, I run that same macro again. On this second run, it hides the rows correctly.

Excel 2013 color fill when number changes in a column

I'm working on data-analysis where i would like to be able to automatize color fill when looking through large amount of data where there are abundant amount of ghost logs and taking too much of my time as they are severely irrelevant.
So what i would like to do in Excel, is to be able to color fill a cell when the number changes in a column marking a different set of logs.
For instance if there are six rows of log number 456455, i would like the code to color fill the first cell when the number changes to 456456 so that it helps me identify logs faster when i know where the sets are starting. I'm kind of a newbie when it comes to Excel but this loop would help me a lot!
Thx for your time
This can be done with conditional formatting. Use a rule that compares the current cell with the cell in the row above and format if the two are different. Note that you will need to use relative references without $ signs. In the screenshot below, the conditional format is applied from row 2 to 19 and in row 2 the formula compares A2 with A1, in the next row it compares A3 with A2, and so on. If the two cells are different, the cell will change colour.
If you have some knowledge in VBA, you can implement a macro that looks at the column where you have your log number and if the value changes from one cell to another, then you highlight this cell.
I attached a template of code that works for this task.
Sub highlightChange()
Dim preVal As Integer
preVal = 0
For Each o In Range("A:A")
'Go through column
If o.Value <> preVal Then
o.Interior.Color = vbRed 'Color the selection
End If
preVal = o.Value
Next o
End Sub
There may be other solution without VBA, however, it is quite easy and practical to use a macro.

Using vlookup in vba but updating the userform live

I have an userform with a few texbox and i want that when you put the value on the first one scan the data sheet and if it found the same value it fill the rest of textboxes, i think i can manage to do it if i do it in two steps. First take the value of textbox1 as variable and with it scan the data an generate the second userform with the data already paste on it. But is there a way to do it live? at the moment i put data in textbox1 shows the data of the rest of the columns on the others textboxes?
Also i was trying to do more or less the same thing in a sheet with an vlookup formula (VLOOKUP(A27,BDD!A:B,2,FALSE)) and it worked but the problem is that i want the formula to change the value of the cells only in case it found a mach in the data and also i don't want the formula in the cells i want to change so i can put new data without problem.
Lets say i have in the sheet "bdd" numbers in the first columns and names in the second. I want in another sheet to put a number and if that number already exist in the bdd i want to have the name near to it but i dont want the formula near to the numbers because i want to be able to put new numbers and names if necessary.
bdd:
101 Antonio
102 Luis
sheet
101 Antonio (At the moment i finish writing 101)
103 Peter (nothing happens because it isn't in the bdd yet and i have to type peter to complete that line or put 103 peter in the bdd)
Sorry if it wasn't clear i tried :P Thanks in advance
If data is in this range
Private Sub TextBox1_Change()
'Skip if value is not there
On Error Resume Next
'take any random cell to store textbox1's value
Range("C4").Value = TextBox1.Text
'Vlookup to get the value
TextBox2.Text = Application.WorksheetFunction.VLookup(Range("C4").Value,Sheet1.Range("A:B"), 2, 0)
End Sub

Resources