Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50139

VBA changing external Workbook to Read Only

$
0
0
I have a primary workbook with multiple worksheets, code as below for sheet 1

What I want to in this primary code is to execute the first piece of code ONLY ONCE based on the value of cell P2
P2 is linked to another workbook valled Initial.xls where I reset all values to Y for sheets 1 - 32
but in this example I am only referring to sheet 1

Problem is that when I change the linked value in Initial.xls to N (so the code can only be performed once)
the Initial.xls workbook changes to READ ONLY BUT still leaves the original Initial.xls open

Very frustrating as eventually there will be many primary worksheets running and pointing to Initial.xls

Any advice much appreciated (and any commebnts of how the code is written)

Thanks in advance

BazzaBit

======================================================================
VBA code

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

'=======================================================================================================
' First time here
If Range("P2").Value = "Y" Then

Application.EnableEvents = False
'=======================================================================================================
' Change INITIAL to N so this code cannot be repeated
Dim xThisRow As Integer
Dim xThisWB As Workbook, xThisWS As Worksheet
Dim xInitWB As Workbook, xInitWS As Worksheet

Dim xHour As Integer
Dim xMinute As Integer
Dim xSecond As Integer
Dim xCountDown As Integer

Set xThisWB = ThisWorkbook ' Define THIS workbook
Set xThisWS = xThisWB.ActiveSheet ' Define THIS Work Sheet

'=======================================================================================================
' Initialise cells or columns as required

Cells(2, "AM").Value = Now() ' Set START INITIALISED to NOW

Cells(2, "Q").Value = "1" ' Set CURRENT STEP to 1

Cells(3, "Q").Value = "1" ' Set PREVIOUS STEP to 1

Range("B9:B68").ClearContents ' Clear RUNNER NAMES (Refreshed then by BAG)

For xThisRow = 9 To 67 Step 2
Cells(xThisRow, "L").ClearContents ' Clear BET RULES (Refreshed by VBA code)
Next xThisRow

Range("L10:S10").ClearContents ' Clear REPORT (Refreshed then by BAG)
Range("L12:S12").ClearContents
Range("L14:S14").ClearContents
Range("L16:S16").ClearContents
Range("L18:S18").ClearContents
Range("L20:S20").ClearContents
Range("L22:S22").ClearContents
Range("L24:S24").ClearContents
Range("L26:S26").ClearContents
Range("L28:S28").ClearContents
Range("L30:S30").ClearContents
Range("L32:S32").ClearContents
Range("L34:S34").ClearContents
Range("L36:S36").ClearContents
Range("L38:S38").ClearContents
Range("L40:S40").ClearContents
Range("L42:S42").ClearContents
Range("L44:S44").ClearContents
Range("L46:S46").ClearContents
Range("L48:S48").ClearContents
Range("L50:S50").ClearContents
Range("L52:S52").ClearContents
Range("L54:S54").ClearContents
Range("L56:S56").ClearContents
Range("L58:S58").ClearContents
Range("L60:S60").ClearContents
Range("L62:S62").ClearContents
Range("L64:S64").ClearContents
Range("L66:S66").ClearContents
Range("L68:S68").ClearContents

For xThisRow = 9 To 67 Step 2
Cells(xThisRow, "M").ClearContents ' Clear ODDS (Refreshed by VBA code)
Next xThisRow

For xThisRow = 9 To 67 Step 2
Cells(xThisRow, "N").ClearContents ' Clear STAKE (Refreshed by VBA code)
Next xThisRow

For xThisRow = 9 To 67 Step 2
Cells(xThisRow, "O").ClearContents ' Clear STATUS (Refreshed then by BAG)
Next xThisRow

For xThisRow = 9 To 67 Step 2
Cells(xThisRow, "P").ClearContents ' Clear MATCHED ODDS (Refreshed then by BAG)
Next xThisRow

For xThisRow = 9 To 67 Step 2
Cells(xThisRow, "Q").ClearContents ' Clear MATCHED AMOUNT (Refreshed then by BAG)
Next xThisRow

Cells(3, "AM").Value = Now() ' Set FINISHED INITIALISED to Now (Once only)

xHour = Hour(Range("AM3").Value)
xMinute = Minute(Range("AM3").Value)
xSecond = Second(Range("AM3").Value)
' xCountDown = (xHour * 3600) + (xMinute) * 60 + xSecond
' Cells(4, "AM").Value = xCountDown ' Set COUNTDOWN in Seconds (Once only)

Cells(3, "AT").Value = xHour
Cells(4, "AT").Value = xMinute
Cells(5, "AT").Value = xSecond

Set xInitWB = Workbooks.Open("C:\McBot\Initial.xls") ' Define the INITIAL workbook
Set xInitWS = xInitWB.ActiveSheet ' Define the INITIAL work sheet

xInitWS.Cells(3, 1) = "N" ' Change the INITIAL value to N

' xInitWB.Close SaveChanges:=True ' Close the INITIAL workbook

'=======================================================================================================
Application.EnableEvents = True

'=======================================================================================================



End If

End Sub

Viewing all articles
Browse latest Browse all 50139