Prevent unintentional editing of Excel file while under automation

galbre
04-11-2003, 03:40 PM
Hi, I am writing data to Excel from VB. The problem is if someone touches that .xls file (e.g. clicks on a cell) it can mess up the writing of data. Or if someone opens up a new .xls file my code will start writing to their file! I would like to monitor what is being written so I don't want to put the sheet out of site. I just want to prevent either someone from tampering with my .xls file or my program from writing to the wrong .xls file. Does anyone have any suggestions? Thanks!

Crassus
04-14-2003, 06:36 AM
For tampering : Tools > Protection

For writing to files :

create an add-in *.xla

or

modify the code in your macro

Wamphyri
04-14-2003, 08:10 AM
1)To prevent tampering while your code is running make Excel invisible while your code is running. This will prevent your sheet from being tampered with while the macro is running and your macro will run more quickly since there is no screen updating while excel is invisible. At the end of your code make excel visible again.

Application.Visible = False
Application.Visible = True

2)My guess you are using ActiveSheet or ActiveCell or Selection. Stop.
Declare a Worksheet object and set it to the sheet you want to work with. Use the declared worksheet object instead.

Dim xlSh As Worksheet
Set xlSh = Application.Workbooks(1).Worksheets(1)
' Enter values in cells through xlSh
' Cell B1 = Hello
xlSh.Cells(1, 2).Value = "Hello"
' rest of your code
'set xlSh = Nothing at end of code
Set xlSh = Nothing

galbre
04-14-2003, 05:55 PM
Thanks for your answer! You were right I was using the ActiveSheet command. Once I created a worksheet object Excel now writes to the desired worksheet. I have one follow up question though: I need to keep the sheet visible to track if I am getting garbage results (code is making measurements in a lab) and thereby restart my program. Is there any way to leave the sheet visible but prevent tampering? Thanks again.

Crassus
04-15-2003, 03:55 AM
Can't you write code that checks if the results are garbage? In this case you would keep Excel invisible until either the program finishes or you encounter garbage results.

galbre
04-15-2003, 08:27 AM
Can't you write code that checks if the results are garbage? In this case you would keep Excel invisible until either the program finishes or you encounter garbage results.
Unfortunately the results are variable and it really requires someone to look at the measured data (being dumped into Excel) to determine if they are "garbage". Using the code to measure performance of electrical equipment in a lab. Equipment is under development so data is variable so it is tough to programatically quantify "garbage". I wonder if "protect" or "lock" might work?

Crassus
04-15-2003, 08:41 AM
Tools > Protection is the same as the protect method, as suggested in my previous post. Use the protect method on your sheet.

e.g.

ActiveSheet.Protect Password := "xxxx"

galbre
04-15-2003, 11:52 AM
Tools > Protection is the same as the protect method, as suggested in my previous post. Use the protect method on your sheet.

e.g.

ActiveSheet.Protect Password := "xxxx"
:) Thanks for all the help. I am squared away!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum