VBA Excel lookup same value in different excel files - vba

Would like to check the value from my master file and copy the data comments from different excel files.
In my master I have a column contained all the user name. Would like to copy the address of the user from different excel file. Would like to open the workbook and look through is the user name in the workbook, if not then then exit and open another workbook.
How should I complete the flow ?

Unfortunately StackOverflow is not a free coding service. Our goal is to help you understand how your code works and engage the community with questions that will also help others in the future. Try running some of the code on your own and see what you can come up with. There are hundreds of sources online, one of my favorite being "Excel Easy", a website that breaks down VBA coding to its simplest form. Check it out and then after you've given it a go yourself feel free to ask any specific question about your code!

Related

Distributing macro as an Add-In to other users

I am looking for some tips how to resolve a problem with distributing macro to other users in my company.
I’ve created a specific workbook which holds a bunch of various types of data such as dates, strings and numbers. Each of the users have to manage the same types of data, so we use this workbook as a template. Data is stored in a columns to which I’ve applied a conditional formatting and data validation to prevent users from inserting wrong values. This Workbook contains few Worksheets with specific names. Names of this worksheets appear in the code – to make some specific calculations for each one of them (based on the name of the worksheet).
The problem is that there is a probability that in the future I would like to make some changes to the code for example to make my macro more efficient or to implement some changes that are necessary.
I’ve searched through the internet to find the best solution and I think the best is to create an Add-In to Excel. But I’ve some questions about that.
Is this really the best solution? Can someone give me a hint to make it in a better way?
If Add-In is the best, is there a way to add this only to specific workbooks (which are my template)?
Is it possible to install Add-in when someone opens a specific workbook (using Workbook_Open) and uninstall it when workbooks is closing (using Workbook_BeforeClose). I’ve looked for answers on the web, but this matter is not clear to me. If this is possible, would it affect speed of the closing/opening workbooks?
Thanks for any help/advice!
Put the add-in on the network drive and mark it as read-only. Use a local copy to make updates. Make sure to set it to read only each time you copy it, and make sure people are running off the network drive.
Often times when people install an Add-In, they answer YES to the question about copying it to their local drive and that is not the right answer. If they accidentally click YES you will need to edit their registry to remove the local reference.
For distributing an add-in through a shared drive with your own development copy I would highly suggest to read the following link.
It has a description of the installation process, including a very important point that braX brought up in his answer - answering No when asked about copying to personal add-in folder.
Below is an adjusted code from the link that I use to save add-ins. It goes into a normal module in the add-in itself. It saves an add-in a folder and sets its attribute to Read only so that whenever it is used by somebody it will not be locked. This setup allows you to update the file by running this macro at any time without needing to chase down users and get them to close Excel.
Private Sub Deploy()
Dim FolderOnSharedDrive as String
FolderOnSharedDrive = "Folder path to store add-in in here"
With ThisWorkbook
On Error Resume Next
SetAttr FolderOnSharedDrive & .Name, vbNormal
On Error GoTo 0
.SaveCopyAs FolderOnSharedDrive & .Name
SetAttr FolderOnSharedDrive & .Name, vbReadOnly
MsgBox "Deploy Completed to " & FolderOnSharedDrive & .Name
End With
End Sub
It is a good idea to add checks for filepath and perhaps some error-handling.
As to some other issues you bring up:
Add-ins can include worksheets that are not visible that you can use to store settings, paths, etc.
It is possible to make add-in appear only on specific workbooks but it will take some effort:
Include a ribbon for an add-in that includes callback for visibility. Set it to visible whenever specific workbook(template) is active. It will still be loaded to memory in any Excel instance
You can programatically add add-ins on open and remove on close as you suggested, check this question to see some options.
Adding/removing add-ins on close or open will definitely affect the speed, but I cannot say if it will be noticeable.

Importing sharepoint data to excell via an existing connection through VBA

first time poster here
I am looking for a specific VBA solution and I just can't seem to google my way to the exact code I need - thus, I come here
I have an excel sheet with an existing connection to a sharepoint database. It calls this connection "toolbase"
I need code so that when this sheet loads up it fills its second sheet with a dump from that connection as if I pressed the Existing Connection > Connect > import table button. Its the only known connection to the sheet.
That's it - and for the love of my feeble VBA skills then I can't find the code for this. My boss just wants a list that refreshes itself every time the file loads. I know where to put the code - I just don't what to write.
Can someone help me out?
EDIT:
The closest I could find to what I think I need is something akin to this: https://support.microsoft.com/en-us/kb/306125
but that seems to be talking about extracting records - I need the whole table
Once the data connection has been made, just have ActiveWorkbook.RefreshAll assigned when the workbook opens.
You do need to show some research effort or attempted coding in future questions though (even if it's wrong) as this is not a free coding service (i let you off this time as the answer is a one liner ^^).

Importing from multiple workbooks

I have completed some code to open multiple workbooks and copy data into one master workbook. I can't figure out how the code "chooses" which workbook to open first to import. Anybody have any ideas what is the method excel uses to select the files?
Take a look at this post over here. There is a link in the responses to a blog which talks about Window natural sorting method. Unless you specify in the code which files to open, VBA will normally open the files based on the natural sorting method.

Is there any way to block a workbook from "external" access?

first time asking something here, so I'll apologize beforehand in case I do anything I shouldn't do.
I'm working on a workbook and there are some information in it that shouldn't be available to everyone else, save a few users.
While doing some testing around I've found that it was possible to use another instance of Excel, i.e. another workbook, to access that information (in this case, using VBA).
What I want to know is: is it possible to block another instance of Excel or another workbook from accessing this workbook with the information I want to protect?
The point of this sheet in working on is to be used as a 'database' of sorts for performance feedback of other employees. They fill a form with their self evaluation and then their manager also fill it, evaluating them. All this is stored in a sheet, which is hidden (veryhidden, to be more specific). Using a simple login system, I was able to enable access to this sheet to only one user (one with admin privileges), but assuming someone knows the existence of that sheet, it would be perfectly possible for them to, for example, just copy everything from that sheet to a blank sheet in a new workbook.
Ultimately, what I'm trying to achieve is some kind of restriction in this workbook, allowing only it's own subs and functions to work on it
Thanks in advance
EDIT: Added some info. Hope that helped clarify my problem
The only way to achieve this is encryption. As far as I know Excel only supports one form of encryption, full workbook encryption. In that case there is a password to open the workbook, a user either has access to the full workbook or none at all.
Any other form of encryption in Excel, protected cells, passwords on macros, etc. can all be bypassed easily by a knowledgable user.
However, you could achieve this using either an external database server or implementing your own encryption scheme in the workbook.
There is some information here on howto access the Windows CryptoAPI from VBA.

Copy Range from Form, and Paste into Workbook (preferably closed)

I have a macro-enabled form that is intended to allow of a team of data-entry staff to record their daily efforts by transaction number. As forms go, it's pretty basic. However, I need to write a macro for a button that will let each person submit their records to a master sheet at the end of their shifts. I need to copy the range data and paste to a master workbook, with the person's name and the date being added to the individual rows.
I'm not sure how to facilitate the copy and paste to a closed workbook, or how to prevent problems with multiple people submitting to the form at the same time. Can anyone offer any suggestions please?
Make it update a database and then generate the report at the end of the day from that. I would also recommend that it inserts into the database each time the user inputs a record so that in the case of a power outage, all of their work from the day doesn't get lost. This will likely reduce the amount of concurrency issues too as the users will be periodically adding records instead of many records at the same time. Search 'VBA DAO' or 'VBA ADODB' to find examples on how to connect to a database with VBA.
You can do this simply by opening the workbook, inserting, and then closing the workbook. There is no simple way to insert into a closed workbook. Note, you could keep things hidden if you're trying to hide things from the user.
Add a reference to the "Microsoft Scripting Runtime" to get access to the filesystem and then use a simple file semaphore to control access to the common workbook.
Regarding the closed workbook, you use Application.Workbooks.Open(...) and .Close
Primary Choice would be to send the items to a database. Since thats already ruled out, I would suggest you write the data to a plain old .csv file. This will be easier to implement, and will not be limited by excel row limits.