Hi bluefeather8989
this will not work with the "." before Range.
Yes it does.
That's the way to indicate that the method/property refers to the object in the With construct.
Ex.
Code:
Sub Test()
With Worksheets("Sheet2")
.Range("B3") = "CDE"
Range("A1") = "ABC"
.Rows(2).Hidden = True
End With
End Sub
This code sets the value for B3 and hides row 2 in the worksheet Sheet2. This is what the "." is telling excel, you call it qualify the object.
The Range("A1"), however, is not qualified. This means that it will write the value in whatever the worksheet is active at the moment of the execution of the code.
As you see, qualifying the object may help you to clarify the code.
Second when you use "clear" this will clear all info in that cell including Format, and conditional formatting. as far as i know using "" will clear text only.
You are right. I chose to clear everything.
The Range object has many clear methods:
Clear
ClearComments
ClearContents
ClearFormats
ClearHyperlinks
ClearNotes
ClearOutline
You can choose the one more adequate to your needs.
In this case, since it seems that you only want to clear the contents of the cell, use the ClearContents method.
Note that if you use:
This will not necessarily clear the contents of the cell. For ex., format A1 as text and then execute the statement.
Now in other cells use the formulas
=ISBLANK(A1) ' returns False, the cell is not empty
=ISTEXT(A1) ' returns true, the cell has a text value
This means that you have not cleared the cell. The cell has a null string.
Repeat it, still with the cell formatted as text:
Code:
Range("A1").ClearContents
You'll see that now the cell has really no contents, IsBlank() returns True, the cell is empty, and IsText() returns False, no text value.
The cell now has really be cleared.
You can also use vba to do the test. Just execute this code and it will show you the difference.
Code:
Sub x()
Range("A1").NumberFormat = "@" ' Sets the cell format to Text
' The option you used:
Range("A1") = ""
MsgBox _
"After executing: Range(""A1"") = """"" & vbNewLine & _
"A1 is " & IIf(IsEmpty(Range("A1")), "empty", "not empty") & vbNewLine & _
"A1 is " & IIf(VarType(Range("A1").Value) = vbString, "", "not ") & "a text value"
Range("A1").ClearContents
MsgBox _
"After executing: Range(""A1"").ClearContents" & vbNewLine & _
"A1 is " & IIf(IsEmpty(Range("A1")), "empty", "not empty") & vbNewLine & _
"A1 is " & IIf(VarType(Range("A1").Value) = vbString, "", "not ") & "a text value"
End Sub
These are small details, but it's good to know, so that we can decide exactly what we want.
Live and learn.
That's a good motto.