ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Have macro ignore error (https://www.excelbanter.com/excel-programming/391621-have-macro-ignore-error.html)

Seth

Have macro ignore error
 
I am looking for help on this macro I wrote to get a filename, open a file,
grab some cells, then close the file. What I need help with is having the
macro skip a section (go to line 10) if the file is not found to open. Right
now it will close my workbook I'm dumping info into. Also, any idea why my
variable Caccel would be truncated when it should have 3 decimal places?
Thanks in advance!

For i = 1 To NumMatrices
On Error Resume Next
Fname = "C:\Documents and Settings\My Documents\" & Tname & ".txt"
ChDir _
"C:\Documents and Settings\My Documents\"
Workbooks.OpenText Filename:=Fname, Origin:=437, StartRow:=1,
DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False,
Semicolon:=False, _
Comma:=False, Space:=True, Other:=False,
FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1)),
TrailingMinusNumbers:=True
Cells.Find(What:="release", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Dspeed = ActiveCell.Offset(0, 2).Value
Cells.Find(What:="accel", After:=ActiveCell, LookIn:=xlFormulas,
LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Caccel = ActiveCell.Offset(0, 1).Value
ActiveWorkbook.Close
' Workbook = wb1
If Cells(Row, 11) = "" Then
Cells(Row, 11) = Dspeed
Else

End If
If Cells(Row, 12) = "" Then
Cells(Row, 12) = Caccel
Else

End If
If Cells(Row, 12) = 0 Then
Cells(Row, 11) = 0
End If
10 Row = Row + 1
Cells(Row, 2).Select
Tname = Selection


Next

Leith Ross[_2_]

Have macro ignore error
 
On Jun 19, 9:43 am, Seth wrote:
I am looking for help on this macro I wrote to get a filename, open a file,
grab some cells, then close the file. What I need help with is having the
macro skip a section (go to line 10) if the file is not found to open. Right
now it will close my workbook I'm dumping info into. Also, any idea why my
variable Caccel would be truncated when it should have 3 decimal places?
Thanks in advance!

For i = 1 To NumMatrices
On Error Resume Next
Fname = "C:\Documents and Settings\My Documents\" & Tname & ".txt"
ChDir _
"C:\Documents and Settings\My Documents\"
Workbooks.OpenText Filename:=Fname, Origin:=437, StartRow:=1,
DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False,
Semicolon:=False, _
Comma:=False, Space:=True, Other:=False,
FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1)),
TrailingMinusNumbers:=True
Cells.Find(What:="release", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Dspeed = ActiveCell.Offset(0, 2).Value
Cells.Find(What:="accel", After:=ActiveCell, LookIn:=xlFormulas,
LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Caccel = ActiveCell.Offset(0, 1).Value
ActiveWorkbook.Close
' Workbook = wb1
If Cells(Row, 11) = "" Then
Cells(Row, 11) = Dspeed
Else

End If
If Cells(Row, 12) = "" Then
Cells(Row, 12) = Caccel
Else

End If
If Cells(Row, 12) = 0 Then
Cells(Row, 11) = 0
End If
10 Row = Row + 1
Cells(Row, 2).Select
Tname = Selection

Next


Hello Seth,

1. Delete the On Error Resume statement
2. Below Fname add this code: If Dir(Fname) = "" Then GoTo 10

Sincerely,
Leieth Ross


Seth

Have macro ignore error
 
Thanks Leith. What about my question on why my variable is truncated?

"Leith Ross" wrote:

On Jun 19, 9:43 am, Seth wrote:
I am looking for help on this macro I wrote to get a filename, open a file,
grab some cells, then close the file. What I need help with is having the
macro skip a section (go to line 10) if the file is not found to open. Right
now it will close my workbook I'm dumping info into. Also, any idea why my
variable Caccel would be truncated when it should have 3 decimal places?
Thanks in advance!

For i = 1 To NumMatrices
On Error Resume Next
Fname = "C:\Documents and Settings\My Documents\" & Tname & ".txt"
ChDir _
"C:\Documents and Settings\My Documents\"
Workbooks.OpenText Filename:=Fname, Origin:=437, StartRow:=1,
DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False,
Semicolon:=False, _
Comma:=False, Space:=True, Other:=False,
FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1)),
TrailingMinusNumbers:=True
Cells.Find(What:="release", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Dspeed = ActiveCell.Offset(0, 2).Value
Cells.Find(What:="accel", After:=ActiveCell, LookIn:=xlFormulas,
LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Caccel = ActiveCell.Offset(0, 1).Value
ActiveWorkbook.Close
' Workbook = wb1
If Cells(Row, 11) = "" Then
Cells(Row, 11) = Dspeed
Else

End If
If Cells(Row, 12) = "" Then
Cells(Row, 12) = Caccel
Else

End If
If Cells(Row, 12) = 0 Then
Cells(Row, 11) = 0
End If
10 Row = Row + 1
Cells(Row, 2).Select
Tname = Selection

Next


Hello Seth,

1. Delete the On Error Resume statement
2. Below Fname add this code: If Dir(Fname) = "" Then GoTo 10

Sincerely,
Leieth Ross



Seth

Have macro ignore error
 
Nevermind. It was because I declared Caccel as an integer.

"Seth" wrote:

Thanks Leith. What about my question on why my variable is truncated?

"Leith Ross" wrote:

On Jun 19, 9:43 am, Seth wrote:
I am looking for help on this macro I wrote to get a filename, open a file,
grab some cells, then close the file. What I need help with is having the
macro skip a section (go to line 10) if the file is not found to open. Right
now it will close my workbook I'm dumping info into. Also, any idea why my
variable Caccel would be truncated when it should have 3 decimal places?
Thanks in advance!

For i = 1 To NumMatrices
On Error Resume Next
Fname = "C:\Documents and Settings\My Documents\" & Tname & ".txt"
ChDir _
"C:\Documents and Settings\My Documents\"
Workbooks.OpenText Filename:=Fname, Origin:=437, StartRow:=1,
DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False,
Semicolon:=False, _
Comma:=False, Space:=True, Other:=False,
FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1)),
TrailingMinusNumbers:=True
Cells.Find(What:="release", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Dspeed = ActiveCell.Offset(0, 2).Value
Cells.Find(What:="accel", After:=ActiveCell, LookIn:=xlFormulas,
LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
Caccel = ActiveCell.Offset(0, 1).Value
ActiveWorkbook.Close
' Workbook = wb1
If Cells(Row, 11) = "" Then
Cells(Row, 11) = Dspeed
Else

End If
If Cells(Row, 12) = "" Then
Cells(Row, 12) = Caccel
Else

End If
If Cells(Row, 12) = 0 Then
Cells(Row, 11) = 0
End If
10 Row = Row + 1
Cells(Row, 2).Select
Tname = Selection

Next


Hello Seth,

1. Delete the On Error Resume statement
2. Below Fname add this code: If Dir(Fname) = "" Then GoTo 10

Sincerely,
Leieth Ross




All times are GMT +1. The time now is 06:53 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com