Selection.ClearContents makes macro halt???

F

Fan924

What would make this macro stop after "Selection.ClearContents" It is
random. It makes it to the end most of the time. Very unreliable.
excel97


Sub ModuleImport()
Dim FileNameWithPath As Variant
Dim FNum As Variant
Dim SeparatorSymbol As String
Dim RowNdx As Long
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer
SeparatorSymbol = "!"
'On Error Resume Next
Sheets("DataModule").Select
SaveColNdx = 1
RowNdx = 3

'>>>>>>>>>>>>>>>>>>>> Get File Name <<<<<<<<<<<<<<<<<<<<
FileNameWithPath = Application.GetOpenFilename(FileFilter:="Module
Files (*.dme),*.dme")
If FileNameWithPath = False Then
Exit Sub
End If
'>>>>>>>>>>>>>>>>>>>> Clear old Data <<<<<<<<<<<<<<<<<<<<
Range("A3:N300").Select
Selection.ClearContents
Selection.NumberFormat = "@"
Range("A1").Select
msgbox "Made It"
end sub
 
O

OssieMac

I assume that you have left some of the code out of the post.

I have no idea why it is not working. However, it is almost never necessary
to select ranges to manipulate data. You can simply refererence the range.
Try the following and see if it makes a difference.

Replace the following code
Range("A3:N300").Select
Selection.ClearContents
Selection.NumberFormat = "@"

with this code
Range("A3:N300").ClearContents
Range("A3:N300").NumberFormat = "@"

Note that your code is applied to the currently active sheet.
 
F

Fan924

Range("A3:N300").NumberFormat = "@"
gives "NumberFormat method of range class failed"
 
O

OssieMac

In my previous post I indicated that you had left out some of the code in
your post. Is it running from a Userform? There are some conditions when code
runs from a Userform if it comes across an error then the code appears to
simply abort instead of stopping on the error.

My thoughts are that for some reason your original problem was because the
range could not be selected. In my previous answer I should have been more
specific and said to include the sheet name in the code instead of just
saying it will run on the active sheet.

The sheet name is included with both of the following examples although the
latter is the more professional. Replace "Sheet1" with your sheet name.

Sheets("Sheet1").Range("A3:N300").ClearContents
Sheets("Sheet1").Range("A3:N300").NumberFormat = "@"

or

'Note the dot in from of Range which ties it to Sheets("Sheet1")
With Sheets("Sheet1")
.Range("A3:N300").ClearContents
.Range("A3:N300").NumberFormat = "@"
End With

Using the above methods neither the sheet nor the range need to be selected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top