Shared workbook - updating data before action - vba

I have shared workbook in excel, so multiple users can change content. Sheet works as list of projects and tasks, which user can insert, edit and delete. Edit and delete works fine for multiple users, but i have issue with inserting. When two (or more users) insert new project/tasks (new rows appears) after save, there is huge conflict in whos data should be saved.
So i have an idea, that before each insertion i would like to load other user changes. Will be enough to use this line before each row insertion?
Application.CommandBars("Reviewing").Controls("&Update File").Execute
(i googled, that this line should trigger shared sheet update)
Or is here better aproach? Maybe after new line save sheet, etc?

So i made bit of testing and when i use
Application.CommandBars("Reviewing").Controls("&Update File").Execute
or
Application.CommandBars.FindControl(ID:=455).Execute
changes wasnt refreshed correctly.
Then i accidentaly found solution using basic saving (ALT + S) which have only issue which was prompting information about "Other user made change in excel.."
This lead to my correct solution, where im using
ActiveWorkbook.Save
on macro start and at the end. On macro start because i need actual data from all users which have saved changes, so i get first empty row to which i can safely insert data and at the end so my change is saved for another users.
This also provides data refresh, when user edits previous data. Hope this will help someone in similar issue.

Related

access macro to copy record contents

(Apologies if this is a dumb question - I'm a complete tyro here)
Suppose you have open a Customers table,
with fields Id, LastName, FirstName. The user
selects a record (row) of the table with the
mouse. At this point I'd like to be able to run
a macro that:
(1) reads the fields in the selected record
(2) opens an existing form say FormA
(3) Pastes (writes) the LastName element of
the record read in (1) into the XYZ field
of FormA
I've looked through the available macro actions
and I don't see how to do (1). Can anyone help?
(If it can't be done via a macro, how about VBA?).
I can probably figure out how to do (2) and (3)
but if it's not too much to ask, I'd certainly
appreciate some guidance here too.
Thanks!
First, trying to run a macro or code on a table from the table to update a form is not going to work. Besides, you are not actually updating anything in the target form. You are adding a record. Record actions have very little to do with "forms" and everything to do with the database.
Second, the action you want is a SQL insert (create a new record). It might be possible to do via a macro or a query. VBA is the better choice.
Put a command button on a form. Let the user select the record and click the button. (OnClick event)
Run code behind (write your SQL statement and run it as DoCmd.RunSQL to copy the record from wherever it is (table) to wherever you want it (table). You can add the code to open another form showing the new record if you want.

How do I prevent unlocked cells from being selected/editable? (VBA)

I have a very specific question that I am looking for help in answering. I have been researching for hours and I feel like I am not able to find what I am looking for. Below is a quick overview of the criteria that my document must follow:
I am using Excel 2013
I will just be using rows for data input (instead of an excel object
table).
The very top/first row will act as my "column header".
This top row will have AutoFilter enabled.
THE DOCUMENT MUST BE PROTECTED (a must-have)!
I will be using VBA code
Now, the final issue I am having with finishing this document are the last two criteria points that I must have:
The first/top row (column headers) must NOT BE EDITABLE.
Each column must be able to SORT AND FILTER.
Now, in a perfect world, I would just "Lock Cells" for the entire first row that acts as my column headers and when I protect the worksheet I would make sure to check the "Sort" and "Use AutoFilter" boxes.
However, this option does not work because there seems to be an issue when I try to sort the data. If I just filter the data there is no problem, but when I try to sort a column in ascending/descending order I will get an error informing me that I can't sort locked cells while in Protected mode. This is because when excel uses the Sort function, it counts the header as part of the data that is being sorted (I found this out through my research) even though I really just want the data below it to be sorted.
I have been trying to brainstorm on how to get past this issue as well as researching different methods, and I am having trouble coming to a final conclusion. However I have narrowed it down to 2 possible solutions:
I want to be able to keep the cells in the first row officially unlocked to allow the AutoFilter's sort command to work as intended, but make it "behave" like the cells are locked when a user tries to make changes to it (AKA, make the entire row un-editable or un-selectable).
The other option would be to keep the first row locked but somehow have an event in VBA that can tell when a user tries to "Sort" the column, which will then temporarily unprotect the worksheet, follow through with the intended sort command, then protect the worksheet again (apparently though, upon my research there is no such event that can trigger off the AutoFilter's sort command alone).
These 2 solutions are the most logical I can think of based off my research, but if someone out there is an Excel genius and knows another way I am open to suggestions.
Thanks in advance for your help/suggestions,
Travo
Consider using two header rows. The top row would be protected and the second row would facilitate filtering and sorting of the data in rows 3 and below.

In SQL how do I update a table with a similar table?

In my current Database I have a table whose data is manually entered or comes in an excel sheet every week. Before we had the "manual entry option", the table would be dropped and replaced by the excel version.
Now because there is data that only exists in the original table this can not be done.
I'm trying to find a way to update the original table with changes and additions from the (excel) table while preserving all rows not in the new sheet.
I've been attempting to simply use an insert query and an update query /but/ I can't find a way to detect changes in a record.
Any suggestions? I can provide the current sql if you'd find that helpful.
Based on what I have read so far, I think I can offer some suggestions:
It appears you have control of the MS Access. I would suggest adding a field to your data table called "source". Modify your form in the access database to store something like "m" for manual entry in the source field. When you import the excel, store an "e" for excel in the field.
You would need to do a one time scrub of the data to mark existing records as manual entries or excel entries. There are a couple of ways you can do it through automation/queries that I can explain in detail if you want.
Once past these steps, your excel process is fairly simple. You can delete all records with source = "e" and then do a full excel import. Manual records would remain unchanged.
This concept will allow you to add new sources and codes and allow you to handle each differently if needed. You just need to spend some time cleaning up your old data. I think you will find it worth it in the end.
Good Luck.

Unable to move / delete rows in shared workbook - Not enough resources

this one's a bit of a painful one so thank you for your help and patience with me.
We have an Excel spreadsheet that we use as a master file for our website products. As such there are quite a few sheets and quite a few products on each running along side some macros to provide some extra functionality (turning entered data into HTML for product page, etc).
My issue is that one of our most used spreadsheets has become a trouble in that it has some phantom formatting all the way down to the millionth-and-something row and all the way across, causing the last cell to be the very last cell possible.
The issue that has finally popped up as a result is that we can no longer move rows in, out or around the sheet (a required functionality) as it results in an 'out of resources error'.
I've tried:
Highlight all rows below used range to right-click> delete - Results in runtime error (from macro)
Highlighting large chunks of rows and using Clear All - Resulted in the 38MB file bloating to 380MB
Deleting a chunk of rows at a time - Maxed out at 1,000 before it caused Excel to crash
Moving to new spreadsheet - Broke all our macros (which I did not write and am not proficient enough to fix on a new sheet)
Disabling macros and trying the above options, only marginally more efficient but still out of resources
I'm at my wits end on this one and, while we can continue with most day-to-day functions, we will soon be completely unable to use this particular sheet as we need it at all.
I'm wondering if there might be a way to run a VBA script to remove these rows, potentially one by one? I've tried running a short script that went something like rows[960,1000000].Delete (forgive my terrible VBA markup), but this also resulted in not enough resources errors.
I'm wondering if there's anything like:
row = 960;
while(row<=1048576){row.Delete};
Continuing, the runtime error debug points me to the below if statement within the macro:
If Target.Count > 1 Then Exit Sub
Where Target is the variable passed to the sub.
Which strikes me as very odd because my (limited) understanding of VBA and IF's in general simply recognizes that 'if my selection is larger than 1 (row?), do not run this code..
Thanks again in advance.
Use this method only if you don't have any links into or out of the sheet that will get broken. Also might have Sql connections that might get broken. Might need to disable macros. There are many possible problems with this approach. Use at your own risk.
Note the exact "Name" and "(Name)" of the sheet; Look in the VBA code window at the properties for the sheet. "Name" is the name displayed on the worksheet tab. "(Name)" is the code name visible only in the properties window.
Make a list of range names on the sheet.
Copy the data to a new sheet.
Copy any macros to the new sheet.
Delete the old sheet.
Rename the "Name" and "(Name)" of the new sheet the same as the old one.
Recreate range names.
A better method if you don't have too many formats:
Disable macros and set calculation to manual. This avoids recalculating while doing your delete operation.
Select entire sheet and clear formats.
Delete all rows below your data.
Redo your formatting. Select entire column (not just used area) to apply format if applicable.
It is important to remove formatting on the entire sheet from A1 to the end. Otherwise you'll get the bloat you mentioned. Just that step may solve your problem. If not then proceed with removing all the rows below the data. This should not cause file size bloat.

Open OFFICE CALC Macro's: how to get the old data that was in a cell after it had been changed?

I did a macro and used ChartDataChangeEvent to get a value of a cell after it had been changed, but i need the old value too for going to a specific column in another sheet and changing there the value according to the cell that had been changed.
is there a way to get the old data(before modification)?
(i don't want to copy all the sheet, because my tables are very big and it will take a long time).
Thanks
Just thinking out loud here, would this be a viable path: create a script based "on data change" (executes every time when data is modified by user). Then copy the "new" data from the active cell, then perform an "undo", to get the old data back, copy the old data, then put the new data back where it was. Don't know if you would programmatically have access to the internal undo stack, because that would be easier.