I need a way to monitor a row in excel and update a cell at the end of the row for today's date if anything in the row changes.
For Example:
I have a spreadsheet where projects are listed per row. I have a column that lists the "last updated" date, I need that date to update whenever any content in that entire row changes.
Columns A:F are the content
Column G is the "Last Updated" cell.
I hope someone can help.
As your tag suggests excel-vba, I would assume VBA is also an option :
1st you need to see if the current changed row or range is inside your acceptable range ($A:$F), so we use Intersect method.
If yes, in the same sheet, same row but column 'G' enter todays date.
Here is the vba code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("$A:$F"), Target) Is Nothing Then
Range("G" & Target.Row).Value = Date
End If
End Sub
Enter this code inside the same sheet module (in vba editor) where you would want to implement the functionality. Thats it... :)
Edit: This dosen't help much without protection because any one can again change the last edited date. But, thats a totally different question.
Related
I have multiple sheets which I am looking to copy and paste the relevant cells from rows into a new worksheet. The defining factor is whether the rows are "Live" which is determined by whether today falls between the "start date" and "end date" columns. For all rows that are "live" certain cells on that row should then be copy pasted to the bottom of the list on the new worksheet. I would then repeat these steps for the other worksheets so that the new worksheet essentially contains a summary of all "live" lines from the other sheets.
I then ultimately need to create a button so I can re-run this at the end of each day.
Hoping you can help!
You've defined and explained what you want to do nicely. So the next step would be to start researching the actions you want the code to do:
Start with how to copy rows:
http://www.bluepecantraining.com/copying-moving-rows-columns-excel-vba/
Next is how to find the last used row in a worksheet:
https://powerspreadsheets.com/excel-vba-last-row/
Next is to apply your condition on what rows to copy:
Using VBA to check if a date is between two dates
Next is how to create a for loop that runs the code the desired number of times:
https://www.excel-easy.com/vba/loop.html (a tip is to use the LastRow that you defined via the second link i posted. eg: "For x = 1 To LastRow")
And lastly is how to assign a macro to a button or shape:
https://wellsr.com/vba/2017/excel/how-to-assign-a-macro-to-a-shape-in-excel/
Try to build a code with the help of these links. If you get stuck, come back and show us what you've created and where the problem is. I (aswell as and many others) would love to help you on from there.
First, I must admit that I am completely newbie here and in VBA mostly too. Thank you for this forum! :)
I have program that reacts on SheetChange to save the data. However, if there is formula added by macro, it doesn't cause the SheetChange. Copying and inserting as values isn't possible - there are 7k rows and the program acts on every SheetChange (colors and other stuff) - the time is not bearable.
Is there any possibility to have EnableEvents = False (to turn of getting SheetChange), then specify the Range of the changed cells (always rather the whole column - there are 2 columns only that interest me) and then let the program save data. The coloring of the cells and so on would remain (this coloring and so on has to stay in the program)
Is it even possible that it would work? If it could, how should I tell the macro that specific Range has SheetChange?
Apologies if the question is totally stupid.
Thank you very much for reading at least.
In the sheet change event, just specify that it should only save when Target is within the specified range. For the formulas, use the Calculate event and repeat essentially the same code.
So, if you only want it to save when the changed cell is in the first column and only within a certain row range (for example), add If Target.Column = 1 And Target.Row > 5 And Target.Row <= 10 Then to your change event.
For the Formula issue, add the following routine
Private Sub Worksheet_Calculate()
'your code here
End Sub
I'm stuck on what should be a very simple question, which is frustrating me greatly.
Essentially, I'm sorting through many files and manipulating them, but am stuck on this: how do I add in a column with something like the date in every cell down to the end of where the other columns have data?
Say that there are 5 other columns with length of 20 each. What code would I use to make column 6 have a certain value with a length of 20? The length varies with each sheet I work on, and this has been the problem.
In a program like MATLAB, I could just denote this by putting something like F1:end, but finding the answer for Excel is driving me crazy!
Any help would be greatly appreciated.
You could programmatically do this in vb to detect the bottom row with a value in, though it sounds as though you might be after a more straight forwards working solution.
Type a value in the top row, and with the cell selected you should see a square in the bottom right of the cell, double click it to fill to the end of the used range.
In VBA you would typically look from the bottom up to find the last non-blank cell in a column. With this, you can get the .Row to concatenate into a Range object definition. Example:
with activesheet
.range("F1:F" & .cells(rows.count, 5).end(xlup).row) = Format(Now, "dd-mmm-yyyy hh:mm")
end with
This will fill column F from the first row to the cell in column F that is next to the last non-blank value in column E.
Sub Tester()
With ActiveSheet
.Range(.Range("E1"), _
.Cells(Rows.Count, "E").End(xlUp)).Offset(0, 1).Value = Date
End With
End Sub
I am trying to add to a macro I have that will hide every row that has no text in a column named Authorization. Please see the code I have below, I thought this may be on the right track but it does not hide any rows.
Cells.EntireRow.Hidden = False
For Each cell In Range("Authorization").End(xlUp)
If cell = "" And cell.Offset(1, 0) = "" Then cell.EntireRow.Hidden = True
Next cell
Edited to add how to define a dynamic named range
It is the fact that you have set the whole column to the name "Authorisation" that I think makes your code freeze, because the whole column is 1 million rows (if you have 2007 or above), and the code will still check even blank rows, so its doing it 1 million times. 1 Option is to rather than set it to the whole column, you could use a "Dynamic Named Range" which will expand and grow as data is added. There are several different formulas to do this, but based on the fact your data may contain blanks, this version of the formula will expand down to the last populated row in the column. My example uses colum A as you havent specified what column you are using, so change A to suit your needs.
You need to open the Names manager, from the Formulas tab
From the dialog box, find your "Authorisation" name.
Select it and you should see its current formula at the bottom of the dialog box, replace that with the following formula:
=OFFSET(Sheet1!$A$3,0,0,MATCH("*",Sheet1!$A:$A,-1)-2,1)
In the above formula:
Sheet1 is my sheet, replace it with yours a needed
$A$3 is the starting row of the name, so based on your comments, have set this as column A row 3
0,0, Are defaults you should not need to change
$A$A$ is the column it is counting values, so change as required
-1 is a default, leave as is
-2 is subtracting 2 from the count because we are starting on row 3, so if you change the starting row, change this
the last 1, defined how many columns your named range covers, in your example it is just 1, so this should not need changing.
Once you have defined the name in this way, the code below should work a lot quicker as it will only loop through down to the last row of entered data. There is one possible issue I can see with this and that is if the very last cell in column A is blank, but the rest of the row isn't, this will miss out the last row. I could fix this by using a different column to count what constitutes the last row, but need to know whicj column would always have a value in it.
< Original answer and code>
not sure you code matches the description of what you want it to do, namely you seem to be trying to check the row beneath the current cell as well, is this what you really wanted? Anyhow your syntax is slightly wrong. I have written and tested this and it works, I have swapped your offset around so my code is checking the cell in the named range "Authorisation" and then also checking the cell to the right. Amend to suit your needs
Sub test()
Dim c As Range
For Each c In Range("Authorisation").Cells
If c.Value = "" And c.Offset(0, 1).Value = "" Then c.EntireRow.Hidden = True
Next c
End Sub
Range("H25")
The above statement selects the 25th row of H column.Now The thing I want to know is,
How do i increment the row putting H constant?
yeah I have seen these on the google $h24 where column stays constant and row keeps on incrementing.
But i have to increment it from H25 to the end I dont know where does H column end. how do i make that?We can declare a variable right using VBA and incremenet it?
I was thinkin to put it like these Range("Hvariablename")?
The actual Task I need to do is I have to check rows from H25 to the end and get their values to my VBA and make some calcutaions then right it back to them Im stucked at incrementing the rows.And could anyone please let me know the macro to delete a row from the vba itself?Thank you
I believe you can provide some further details about how this range is supposed to looks like. When you mean "have to check rows from H25 to the end and" what's the END here?
If you're manually copying the formula down, it will automatically goes until the last row with values in column G. Is that your case? I'm not assuming the H rows below row 25 already have some values. Is this the case?
If you use as END the last row with values in column G, you can use Selection.AutoFill.
If you already have values in column H and want to go through them, you can define a dynamic range and loop through it in VBA.
To define a dynamic range: Add a new range, based in this formula (you may need to adapt it according to your needs, obviously)
=OFFSET(Sheet1!$H$25,0,0,COUNTA(Sheet1!$H:$H),1)
And then, in VBA, go through it.
Sub TEST()
Dim oCell As Excel.Range
Dim oRange As Excel.Range
Set oRange = [TestRange]
For Each oCell In oRange
Debug.Print oCell.VALUE
Next oCell
End Sub
In time: To delete an entire row in Excel, you'll use
Sheets(1).Rows(25).EntireRow.Delete
A Do While() Loop sounds like what you might need.
Do While(cells(row,col)<>"")
'Some code to be executed on cell(row,col)
Loop
Or you could do the same thing with for loop
and find the last cell using lastRow=Cells("H24").End(xlDown).Row. (It is the same thing as pressing Ctrl+Down. so if there are empty cells between cell H25 and the end you will need to account for that).
Either way I'd definitely look into using the R1C1 cell reference style for moving through cells instead of the A1 style. You moving through lettered columns is much more difficult than numbered columns. Also you should keep in mind that the "last row" in Excel 2007 is a very high number and looping all the way until the end can take a lot longer than looping until the last important cell.
I hope that's on the right track.