Hi Ylp
1)I placed a line of code that looks to see if there is no data in A2, if there is no data it is supposed to put a term in A2, "Clear". The first issue is that the term does not appear but rather a "0" which is a curiousity. And Secondly it still clears all the data beginning in A2 and then row 1.
What your code is actually doing is inserting a formula into cell A2 which refers to itself (ie to cell A2) thus creating a circular reference. This happens regardless of whether there actually was any data in A2 to begin with. This is bad because:
i) You are effectively intending to check the value of your variable LRow by inserting a formula into your worksheet. This is both unnecessary and slow - you can do the checking in code and perform any actions as a result without resorting to modifying the original sheet.
ii) You will only get a return value of 0 because you have introduced a circular reference.
Your line of code here:
ws.Range("A2:I" & LRow).ClearContents
always starts at A2 and deletes everything to row LRow - where LRow is row 1 therefore, this will delete the formula you have just inserted and thus your hoped for string "Clear".
To modify (and assuming I have understood your requirements properly) you can use:
Option Explicit Sub Clear()
Dim ws As Worksheet
Dim LRow As Long
Set ws = Workbooks("TGSImporter.xls").Sheets("Update")
'Variable
LRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
If LRow = 1 Then
ws.Range("A2") = "Clear"
ws.Range("A1:I1").ClearContents
Else
ws.Range("A2:I" & LRow).ClearContents
End If
'ws.Range("A2:I" & LRow).ClearFormats
[A2].Activate
End Sub
Hope this makes sense!
Richard