Excel VBA: Insert rows and paste values - excel

I will preface this by stating that I would not be surprised if the answer is floating around out somewhere and I just haven't seen it, so if this is truly a duplicate, I apologize. Here is the situation I'm facing, though.
I use Excel at work to help keep track of patients and their statuses as far as clearance for procedures at my unit. I have 5 sheets open: Tracker, Removed, Added, Old, New, and Compare. Every morning, I generate a report of the updated schedule. I take what is in "New" and copy it over to "Old". I take what was generated and copy it to "New". In "Removed" and "Added", there are formulas that populate fields based on if a patient exists in Old but not New (removed) or vice versa (added). If the patient has been added, I need to insert them into Tracker at the appropriate location (e.g. if a patient was added for an appointment on 9/3 at 2:45 pm, I would need to add them between 9/2 2:30 pm and 9/6 7:30 am). I can do this one of two ways currently: Right-click on the appropriate space, Insert Row above, and then go over and copy/paste the values, or I can copy the row first (which is a formula), right-click and Insert Copied Cells which pastes the formula, right-click again and select 'Paste values'. Either way, it's two steps. It boggles me that Excel does not have an 'Insert Copied Cells special' where it will just to the values.
I understand that VBA may be used in this case, but I have no idea how I would go about doing so. Every example I have now seems to indicate that a specific row must be named, but I do not have that luxury. It literally needs to go above the selected row. Is there a way this can be done, or am I doomed to just do the dual steps forever? Or am I just overcomplicating things in general?

Related

Copying rows from one sheet to another - checking previously copied data and inserting data above row that says "Subtotal" in column G

A bit of background before I start. Essentially, I've got all workers to fill out a form of their 'whereabouts' and what work they did, this then automatically populates into the correct fields etc. Using Power Automate, however, this is in table format and will literally update a a type of master.
My answer to this has been to just pull every cell from the Master table (e.g =A1 of x sheet) that my Power Automate flow creates and just bring it onto a centralised spreadsheet. Then from my forms submission there is a unique reference for form submission in cell G on every row, which will be the time and date they submit their response.
My data all has formulas in (as explained above), therefore the last row will be pretty difficult to find as I can't just find the last row as I usually would - so this would usually just go to the last cell where there aren't formulas, right?
How would I be able to find the correct rows to copy over?
Ideally, I would check to see the values value in column G that appears in any other sheet, if they do, do not copy. If they don't then copy over and insert rows above the cell which contains the text 'Sub Total'
Not sure if I've just fully confused myself and it's super easy but I just can't figure it out and even get started on it.
I am struggling to even think of a solution, apologies.

How to copy and paste formatting from the bottom cell of a table to the next cell below it?

I have a macro that cuts cells out of a table if the entries' date is > 2 weeks old. It then Deletes these cells. However, this causes my formatted area for data entry to shorten each time, so I am trying to find a way to fix this. My idea was to write a piece to copy the formatting from the last formatted row and paste it directly below it, but I am not sure how to go about doing this in VBA. I am newer to VBA and was wondering if anyone knew how to help? Thank you!
It sounds like your goal is to basically have your data entry table ALSO be your summary table, hence wiping out all entries greater than 2 weeks old. This seems like it will end up backfiring on you at some point - for example, what if you wanted to pull a report on everything you entered over the last year?
A better route would seem to be to have a single data entry sheet where you keep EVERYTHING and then a summary display that actively just pulls the data from the last two weeks. For that, you could use VBA to filter your data set based upon the date and then copy the information over to the new sheet (just do it once manually using the filter tool from the Home tab on the ribbon, while recording your actions to a new macro, and you should be able to clean it up pretty easily).
If you MUST edit in place, and presuming you don't mind it being sorted by date, record a macro where you select your full formatted range, turn on filtering, filter by your date column, delete the contents (just use the delete key) of the cells you want to wipe out, and then sort by date on the rest.
The cause of your problem is that in using the range.delete command you are removing the cell from existence (and hence shrinking your formatted range) - you just want to wipe out the contents and then move all your data up to fill the blank space.
good luck!

Copy & Paste From Different Workbooks to MasterSheet

I have one workbook with 8 sheets per country (i.e. France, Belgium, etc.). Apart from these sheets I have "Mastertab" and "Database".
Country-sheets are created in the same way (they have the same template). These templates are made to track expenses (therefore for example, France may have 10 expenses, Belgium 3 and UK 81).
What I would like to do is to create a macro (because I think it is impossible with formula if I want to avoid blank rows) that will:
Find last row where is any data/value in column K.
Select all data (row that will be indicated in point 1 and columns B-P).
Paste this selection to the Mastertab.
Continue doing 1-3 for other countries/sheets KNOWING that data should be pasted just below the previous one (so it has to find first blank row and paste data there).
Example:
First row with data for each country is 18. This is the place where first line with expense is posted. User will fill in only data in column K (all other columns are filled with formulas, vlookups, etc.)
Assuming that I have 3 expenses for France, I want Excel to select B18:P20 and paste into Mastersheet in cell B18.*
*Next pasting in Mastersheet should be from cell B21.
Hope it is clear. :)
Thank you in advance,
Marek
you would definitely be stretching the limitations of Excel formulas if you tried using formulas to do this.
You can use VBA to do this, but you're going to probably going to need the user to trigger the macro every time they make an entry. If you need the master to be dynamically updated then that's going to be harder again.
I think the cleanest way to do this is to have a macro that builds the master list from scratch every time you run the macro, i.e. deletes the Master sheet, and recreates it again. This way you don't have to deal with reorganizing the master output every time a new row is added. That would require a lot of additional logic.
If you are doing a multi-user environment with concurrent updates, then this approach will not work, as you would need an additional server to handle messaging between each user. At this point, you're better off not using Excel.
If each user will add their data, run the macro and send the sheet to the next user you could concoct a macro approach.
From a very high level:
Create a macro to co-ordinate all that comes below
Delete everything in your master (MasterSheetName.Cells.ClearContents will keep the formats if you need it)
Create an index which keeps track of the last row with data in your master sheet. This will start at 1
Iterate through each of the country sheets
https://support.microsoft.com/en-ie/help/142126/macro-to-loop-through-all-worksheets-in-a-workbook
Iterate through column K looking for values
Iterating through populated rows in Excel using VBA
In this example they are looking at column A. You can replace 1 in the sh.Cells(row,1) to sh.Cells(row,11)
Copy the data to the mastertab
Copy from one workbook and paste into another

Use VLOOKUP but detach the value from the lookup once it is in the cell/looked up

I have the following:
1 Access file with a big list of items that people can order.
1 Excel file with 2 tabs.
In the 2nd tab of the Excel file I import the Access table.
For each row I have a dropdown list in the first column with the values of the first column of the access table.
In the 2nd column of the first tab I use VLOOKUP to show the price linked to the item of the first column.
In the 3rd column of the first tab the user can fill in an amount.
Here's the catch:
in the 4th column of the first Excel Tab I want to show a total so B * C. Nothing hard about that. But I want this value to NEVER CHANGE.
So even if the Access database is updated I do not want the value of the already calculated cells of column 4 to change.
I know it seems pretty unlogical but I really need this.
Any suggestions? Is there a formula that I am missing?
EDIT from post:
The file is to be used be other people than myself. If paste special can be done automatically as in like a formula it would help but that doens't exist or I haven't found it.
Thanks in advance.
If the formula is linked to a connection (e.g. OLAP cube, a table that is linked to an Access table) then remove the connection before sending the sheet out. See link.
If you distribute the workbooks to people who do not have access to the data itself (not on your network) the issue will solve itself, take away that some dummies will complain about their data disappearing not refreshing.
The formula you're asking for would refer to itself once it has a value, confer circular reference.
If used by your close enivronment you might want to have a VBA snipplet insert your formula and then do the PasteSpecial method automatically (you'll have to enable macros on each of the computers involved in this case).

Copying of data skipping blanks

Good Day, can somebody please help me. I have 2 spreadsheets, the second spreadsheet has got the copied cells exactly like the first one. But my problem is that on the first spreadsheet I have blank rows as the invoices are being put in date order and not invoices on every day. The second sheet I use for Vat purposes and want my selection there to be in list form in order to only print 1 page for bookkeeper. Where I have invoices for eg. on 10th, 15th and 20th on first spreadsheet "what formula can I use for the 3 invoices to appear in the second spreadsheet eg. in row 1,2,3 one right underneath the other one.
I have done VBA 20 years ago and came right with everything except this I can not figure out for the life of me. Thanks in advance to anybody that can help me with this.
highlight, press F5, click "special" select either "constants" or "formulas" depending on if it is hardcoded or formulaic data, then hit ok. this will leave all blank cells out of the selection, then copy paste as normal to the other workbook. if your doing it in vba this is all available in the object model as
Selection.SpecialCells(xlCellTypeConstants, 23).Select
Selection.SpecialCells(xlCellTypeFormulas, 23).Select
UPDATE: I forgot about the bug (design flaw?) in excel that makes it error out when you try to copy a non-rectangular selection. See here (the article is older but appears to apply to 2013):
http://support.microsoft.com/kb/100715
I believe there are macros available on the internet to deal with this problem, one of them is here
http://www.extendoffice.com/documents/excel/799-excel-copy-multiple-selections.html
A quick fix would be to take the inverse approach: use the "special" selection to select blanks, and then delete them. then select a rectangle around the remaining cells.
the VBA call to select blanks is
Selection.SpecialCells(xlCellTypeBlanks, 23).Select

Resources