Auto delete a cell after 180 days and shift row - excel

Thank you for taking the time to read my question. I have looked everywhere and I am unable to figure out the code on how to accomplish my goal.
enter image description here
I am looking for a code to auto delete a cell after 180 days and shift the remaining contents of the row over. EX: i have a date in cell D9 once that date highlights blue and is past 180 days from todays date...I would like it to auto delete and shift the remaining row details to the left 2 cells. So D9 and and E9 would auto delete and would move F9 to D9 and so on for the remainder of the row. Please let me know if that makes sense. Thank you again for taking the time to review my question.

If by "auto delete" you mean you want it to run every time you open the workbook, you can do this:
1) With workbook open:
2) Hold down your "Alt" key and hit "F11" to open the VBA editor
3) In the Project Explorer on the left, under "Microsoft Excel Objects", double-click "ThisWorkbook".
4) Paste the following code into the editor:
Sub Workbook_Open()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
If CDate(ws.Range("D9")) < DateAdd("d", -180, Now) Then
ws.Range("D9, E9").Delete Shift:=xlToLeft
End If
End Sub
5) Save and close the workbook
You could also add the same code (without the sub and end sub lines) to any existing macro you are running and it would execute with that other macro. If, by "auto delete" you mean you want it to happen without you even having to open the workbook. You could use the "Task Scheduler" in Windows to open the workbook and run the code according to a preset schedule.
If this answers your question, please remember to mark as answered, so everyone will know you have your solution. If this doesn't answer your question, let me know why not and I'll try to help.

Related

Excel copy-paste including hidden columns

I'm using a Excel 2016 worksheet that has a couple of columns hidden for UI reasons. I need to be able to filter out data and then copy-paste it to another sheet with hidden columns intact and showing after pasting in the destination (it will contain a longer log of similar transactions, not just one copy-paste).
Adding a pic of the objective - i.e. hoping to have the hidden contents of columns B and C being pasted into the destination spreadsheet. Is this possible at all?
Probably not great form to ask 2 questions in one post, however are there alternatives to performing filtering and copy-paste function to another spreadsheet manually? I.e.:
run manual filter to clear blanks in Quantity field;
make a selection
do manual Ctrl+C - Ctrl+V function
Is there a way to make it easier? Unfortunately no VBA or macro experience as of yet.
Edit - Completely misunderstood the question!
You want to include hidden cells when you copy - that's standard behavior for hidden cells but not for filtered columns. If you want to avoid VBA abd you're dealing with small contiguous ranges then a simple formula may be the easiest solution.
Using your example, I will arbitrarily name the source worksheet "Sheet1" and the destination "Sheet2". In Sheet2, click in cell A2 and type this into the formula bar: =Sheet1!A3 Now click the bottom right corner of cell A2 and drag it to the right through D2 then down to D7.
With the range highlighted, press ctrl C to copy, then right click to paste special values.
You're done!
Here's a VBA solution:
Sub copyrng()
Dim srcrng As Range
Dim tmprng As Range
Dim dstrng As Range
Dim srcws As Worksheet
Dim dstws As Worksheet
Set srcrng = Application.InputBox("Area to copy", "Source", Type:=8)
Set srcws = srcrng.Parent
Set tmprng = Application.InputBox("Top Left Corner of Destination", "Destination", Type:=8)
Set dstws = tmprng.Parent
Set dstrng = dstws.Range(tmprng.Address, tmprng.Parent.Cells(tmprng.Row + srcrng.Rows.Count - 1, tmprng.Column + srcrng.Columns.Count - 1))
dstrng = srcrng.Value
End Sub
First answer (answered wrong question)
You can copy visible cells using "Go To..."
Highlight the range you want to copy, press Ctrl G, click "Special...", select "Visible Cells Only", and then press Ctrl C to copy.
Now all hidden cells will be left behind when you paste.
No, you cannot do this with regular Excel features since Excel cannot know which columns/cells to skip when one of the column have blank values, this is something has to be decided and done by a human.
Maybe this is a good time to enter the world of Macros, since you do not need a custom code but can use the recorded macro without any further manipulation. This Excel feature is for inexperienced users just like you.
View / Macros / Record Macro
Name your macro
Do what you need to, keeping in mind that Excel is recording your every move by converting them into VBA codes in the background. For your case, do the following:
Filter the blanks using filter combo-box
Select the range by using CTRL-G / Special / Current Region (do not select the cells by mouse or with your keyboard, your code should be generic should not contain manual ranges since you do not want to do any coding)
CTRL-C to copy
If "to-be-pasted" cell is not fixed for all your cases, then you should stop recording your macro here. If pasting cell is fixed then Paste the contents while the macro is recording.
After the macro is recorded, assign a shortcut to your new Macro using:
Macros / View Macros / Options menu
Voila! Now you are able to do exactly what you have done when recording your macro by using that keyboard shortcut. If you did not paste the content when recording then you s/b using your macro short cut and go to the cell you want to paste and press CTRL-V.
When you feel confident enough, try the Edit menu in the Macros and see what code you have in hand, maybe make some small changes etc. I saw many people who are not familiar with basic coding at the beginning but somehow started writing their own codes after seeing this feature in Excel. Good Luck!
ProfoundlyOblivious code is pretty cool but the
dstws = activesheet
will always be the source since the activesheet passes back straight after the inputbox.
I tried changing it to
Set dstws = tmprng.Parent
but for some reason this then breaks the
Set dstrng = dstws.Range..
I get a Run time error 1004 Method range of object _Worksheet failed?!?!
If I could fix that this solution would work for you with any destination, even other files.
The alternative is to use vba to un-filter the data, then do a copy, then put same filter(s) back on. Once that is done you can go anywhere and paste what is now on the clipboard.

Batch add a formula in Excel 2016

I have a spreadsheet with hundreds of cells containing formulas like =('Pricing Master'!$E135*'Pricing Master'!$L$29). I would like to batch add the ROUNDUP formula, so that they all read, for example, =ROUNDUP('Pricing Master'!$E135*'Pricing Master'!$L$29,0). A simple Replace All will not work, as it requires both the function call preceding as well as the Number argument following. Not providing both at the same time produces an error. This creates an issue with batch editing using Replace All.
I am sure that there is a way to do this with the Paste Special function, although if there is another way I would be glad to hear it.
My approach is this and I do this often with large sheets with many formulae:
One : select the row(s) or column(s) you want to work with,
Then edit/replace “=(“ with “xyxy(” (I use xyxy as it just doesn’t come up...
Now all replace operations will be quicker as there is no re-calc happening...
So now do edit/replace “xyxy” with “xyxyroundup(“ and “)” with “,0)”
Then just replace “xyxy” with “=“
And wait for it to finish its calculations...
loop through each cell and add formula to the original formula. Like this
Sub updateFor()
Dim r As Range
For Each r In Selection.SpecialCells(xlCellTypeFormulas)''only cells with formula in
r.Formula = "=ROUNDUP(" & Mid(r.Formula, 2) & ",0)"
Next r
End Sub
just replace the selection with the range you want to edit
if you arnt familiar with vba heres a quick guide
press ALT + F11 to show the vba editor
from insert menu select module
module1 should appear in the project window in the top left,
click on this
a large window should open on the right paste the above code in
go back to your worksheet and select the cells you want to roundup
goto to the view ribbon click the macro button on the far right
select the macro 'updateFor' and press run

Excel If a value in cell a2>8 automatically move to cell D2 to enter text

I am trying to have a spread sheet to record hours worked so that if the total hours for the day are above 8 in cell A2, the cursor is moved automatically to cell D2 on the second tab so a reason for overtime can be entered.
Tab one is called 2013
Tab two is called 2013 overtime.
I'm not very good with excel so any help is greatly appreciated. Thanks!
You don't provide much information. Which cells get changed and contribute to A2? Are there similar columns that would require a different cell to be selected? Please edit your question and add a few more details about the data structure.
The following macro works on the assumption that cell A2 gets changed manually.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim shOvertime As Worksheet
Set shOvertime = ThisWorkbook.Sheets("2013 Overtime")
If Not Intersect(Target, Range("A2")) Is Nothing Then
If Range("A2") > 8 Then
shOvertime.Activate
shOvertime.Range("D2").Select
End If
End If
End Sub
If your data structure is different, please explain it.
To get the code to work, right-click on the sheet tab and select "View Code" to open the Visual Basic Editor (VBE). Paste the code into the code window and close the VBE. Then enter numbers into cells A2.

Latest Modification Time Stamp for Excel [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
Date Modified Time Stamp of an Excel Sheet Using VBA
Hi I've an excel workbook...I want to jot down the time stamp in cell A2 of sheet 1 for the latest modification made in any row or column of sheet2 of the workbook.
So my problem is two fold:
1. Want to trace the latest modification time.
2. Want to make the cell A2 of sheet1 as user protected so that no one can tamper with the traced time stamp.
Here is the code on which i was workung...and its not correct as its not doing anything..if it would have bben doing anything then definitely there was not any problem for me in sharing it but as its not doing anything so its a bit useless.
Private Sub worksheet_change(ByVal Target As Range)
Dim Row, Col
For Row = 2 To Sheet2.UsedRange.Rows.Count
For Col = 1 To Sheet2.UsedRange.Columns.Count
If Target.Cells(Row, Col) Then
Application.EnableEvents = False
Sheet1.Cells("A2") = now()
Application.EnableEvents = True
End If
Next Col
Next Row
End Sub
To get you headed in the right direction:
Right Click on the sheet tab for sheet 2.
Select "View Code".
You'll see two drop-downs at the top of the module. In the first one select "Worksheet". In the second one select "Change".
This will create the template for the Worksheet_Change procedure, which will fire whenever the user changes a value in Sheet2.
Use that to write the time stamp into cell A2 of sheet1.
For the second part of your problem, protect sheet 1. In code, that's:
ThisWorkbook.Worksheets("Sheet1").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
And before you write to cell A2, unprotect it first:
ThisWorkbook.Worksheets("Sheet1").Unprotect
Look up the Protect and Unprotect methods in help if you want to add a password.
(Bear in mind that sheet protection is pretty weak, even with a password. For casual work it's OK, but it CAN be cracked by users if they're determined to do it. A better approach might be to write the value into a sheet which has a visible property of xlVeryHidden so that most users won't even know it's there (if you apply protection to the VBA project). Again, not uncrackable but better than having it in a visible sheet.)

Force Refresh of "Last" Cell of the Worksheet

Pressing Ctrl+End in Excel takes you to the bottom-right-most cell of the worksheet.
If you delete the last rows or columns and save the workbook, this last cell gets updated, as well as the scrollbars.
I remember there was a one line VBA command that you could run that would do the update without having to save the workbook, but I can't remember what the command is - do you have any ideas?
I’ve found something that consistently works to delete those blank rows. You can tell when the “used range” excel is using is too big and is going to add extra blank rows when you use the scroll bar to the right and it goes beyond the last row of actual data when you scroll to the bottom. This will caused extra blank records to be added to the table when it is imported into SQL.
To get rid of them:
Select the entire first row under the last row of data. Hit Ctrl + Shift + Down Arrow to select all the blank rows.
From the Home Tab Select Clear and then Clear All from the Edit menu (picture of a whitish-grey eraser).
Right-click and select Delete.
Goto cell A1 first and then Save.
Press Ctrl + Home and then Ctrl + End (This should take you the correct last cell in the used range (above blank rows).
Click Save again.
Here is the answer:
Sub x()
ActiveSheet.UsedRange
End Sub
Run this and the last cell will be reset.
When none of the above works try this.
Select the unused rows and change the row height.
Now delete the rows and save.
Bingo!
Here's what I did... since none of the above worked (this time that is, which is sad cause this code was running beautifully then all the sudden xlCellTypeLastCell totally failed me.) This will only work if you hardcode the first cell of the region you wanna grab the last cell of... for example I was pasting data tables into a sheet of 12 - 40 columns and 60-90 rows... but since it was a paste, it always started in cell A79...
Worksheets("Data_Import").Activate
Range("A79").CurrentRegion.Select
A = Selection.Rows.Count - 1
B = Selection.Columns.Count - 1
Selection.Offset(A, B).Resize(1, 1).Select
Set DataEnd = Selection
I feel sad to NOT use the cool special cells thing, but alas, if it doesn't work! then I just can't use it. :C
p.s. - you could also throw in a
ActiveSheet.Cells(Rows.Count, 1).End(xlUp).CurrentRegion.Select
This solution works for Excel 2013, but may also work for most recent versions of Excel:
Choose the worksheet where you want to change the last cell, and delete any unused rows and columns
Click on File - Options - Customize Ribbon
Under Main Tabs, check the box next to "Developer", then click OK
On the Developer ribbon that now appears, click Visual Basic
In the upper-left corner, under Microsoft Excel Objects, click on the Sheet Name where you want to force a refresh of the worksheet's last cell
In the menu, click on Run - Refresh, then close the Visual Basic Window
When you hit Ctrl + End, your last cell should now be refreshed.
Check out http://dmcritchie.mvps.org/excel/lastcell.htm#MakeLastCell, found the link from a similar question.
Far from the forgotten one liner, but did solved the problem for me.
Alternatively;
Turn on manual calculation (to preserve references).
create new sheet.
Copy cells and Name of old sheet.
For some reason the code ActiveSheet.UsedRange alone did not work for me on Excel 2016 64-bit to force Excel to reset the last used cell.
This does work for me. Hope this helps.
Rows(25:415).Clear 'deletes contents and formatting
ActiveSheet.UsedRange 'resets last used cell
The solution to change row height to zero and saving worked.
Reopen, set row height to 12 and notice that End Home is no longer at the very bottom right of the worksheet.
THANK YOU. I have been working on this for over two years.
Jim Champaigne
Elkhart, Indiana
I'm not sure why everyone is making it so complicated. Just press Ctrl + PgDn.

Resources