Notifications
Clear all

Importação TXT

5 Posts
1 Usuários
0 Reactions
1,237 Visualizações
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Estou usando este coodigo para importar um arquivo txt , só que se abrir o "mini" explorer de arquivos e clicar em cancelar dá:
ERRO EM TEMPO DE EXECUÇÃO '53': O arquivo não foi localizado.

Como eliminar esse erro?

 If Range("AQ97") = 1 Then
    'Contador de linhas
    i = 1
    'abre um "mini" explorer de arquivos
    ArquivoTxt = Application.GetOpenFilename("Arquivos Texto(*.txt), *.txt")
    'abre o arquivo texto
    Open ArquivoTxt For Input As #1
     ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & ArquivoTxt, Destination:=Range("bl97")).Refresh BackgroundQuery:=False
       'fecha o arquivo texto
    Close #1
    'Cells.EntireColumn.AutoFit ' autoajuste da largura da coluna
    Range("bl97:IV99").Select
    Selection.Delete shift:=xlUp
    Range("bl98:IV98").Select
    Selection.Delete shift:=xlUp
    Columns("bm:bm").Select
    Selection.Copy
    Columns("bj:bj").Select
    Range("bj97").Activate
    ActiveSheet.Paste
    Columns("bp:bp").Select
    Application.CutCopyMode = False
    Selection.Copy
    Columns("bk:bk").Select
    Range("bk97").Activate
    ActiveSheet.Paste
    Columns("bl:IV").Select
    Range("bl97").Activate
    Application.CutCopyMode = False
    Selection.QueryTable.Delete
    Selection.ClearContents
    Range("bj:Bk").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Font.Bold = False
    With Selection.Font
        .Name = "Arial"
        .Size = 12
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    Range("Bg97:Bh97").Select
    Selection.Font.Bold = True
    With Selection.Font
        .Name = "Arial"
        .Size = 16
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
     Range("AQ97").Value = 0
     Rows("97:97").Select
    Selection.RowHeight = 73.5
    Rows("100:123").Select
    Selection.RowHeight = 45.5
    Range("D100").Select
    
    Columns("BJ:BK").Select
    Selection.Replace What:=" 7", Replacement:=" ", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="23 K ", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
    ProtCell
    Prot2
    MsgBox "Importação Concluida Com Sucesso", vbMsgBoxSetForeground
            Else
    MsgBox "VOCÊ JÁ EFETUOU A IMPORTAÇÃO DO ARQUIVO", vbMsgBoxSetForeground
    End If
    ProtCell
    Prot2
   End Sub
 
Postado : 30/04/2012 11:53 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Utilize a instrução On Error como a seguir:

Sub NomedaSuaRotina()

On Error GoTo fim

'seu código aqui

fim:
Exit Sub

End Sub

 
Postado : 01/05/2012 6:37 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Bom dia!!

Tente também...

If ArquivoTxt = False Or ArquivoTxt = "False" Then Exit Sub

'abre um "mini" explorer de arquivos
ArquivoTxt = Application.GetOpenFilename("Arquivos Texto(*.txt), *.txt")
'A linha abaixo talvez possa te ajudar
If ArquivoTxt = False Or ArquivoTxt = "False" Then Exit Sub


If Range("AQ97") = 1 Then
'Contador de linhas
i = 1
'abre um "mini" explorer de arquivos
ArquivoTxt = Application.GetOpenFilename("Arquivos Texto(*.txt), *.txt")
'abre o arquivo texto
Open ArquivoTxt For Input As #1
ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & ArquivoTxt, Destination:=Range("bl97")).Refresh BackgroundQuery:=False
'fecha o arquivo texto
Close #1
'Cells.EntireColumn.AutoFit ' autoajuste da largura da coluna
Range("bl97:IV99").Select
Selection.Delete shift:=xlUp
Range("bl98:IV98").Select
Selection.Delete shift:=xlUp
Columns("bm:bm").Select
Selection.Copy
Columns("bj:bj").Select
Range("bj97").Activate
ActiveSheet.Paste
Columns("bp:bp").Select
Application.CutCopyMode = False
Selection.Copy
Columns("bk:bk").Select
Range("bk97").Activate
ActiveSheet.Paste
Columns("bl:IV").Select
Range("bl97").Activate
Application.CutCopyMode = False
Selection.QueryTable.Delete
Selection.ClearContents
Range("bj:Bk").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Font.Bold = False
With Selection.Font
.Name = "Arial"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Range("Bg97:Bh97").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Arial"
.Size = 16
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Range("AQ97").Value = 0
Rows("97:97").Select
Selection.RowHeight = 73.5
Rows("100:123").Select
Selection.RowHeight = 45.5
Range("D100").Select

Columns("BJ:BK").Select
Selection.Replace What:=" 7", Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="23 K ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

ProtCell
Prot2
MsgBox "Importação Concluida Com Sucesso", vbMsgBoxSetForeground
Else
MsgBox "VOCÊ JÁ EFETUOU A IMPORTAÇÃO DO ARQUIVO", vbMsgBoxSetForeground
End If
ProtCell
Prot2
End Sub
 
Postado : 01/05/2012 7:44 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Withe/Alexandre
muito obrigado .
alexandre ,
até havia tentado uma solução parecida mas não funcionou. era = If ArquivoTxt = "" Then Exit Sub
me explica outra coisa qual o motivo de ter usado False e "False" , qual a diferença entre ambos?

 
Postado : 02/05/2012 8:34 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
Topic starter
 

Boa noite!!

O motivo de forma resumida é que seu código não tem a declaração das variáveis!!!

A diferença é que seu não variável de declarada, e que quando tem cancelamento há um resultado falso para a variável.

MyVal Dim As Integer ... MyVal só pode ser um número inteiro entre-32k (ou quase) e 32k (ou assim).
MyVal Dim como Long ... MyVal só pode ser um número inteiro entre 2.1million-(ou assim) e 2.1million (ou assim).
MyVal As String Dim MyVal ... só pode ser uma cadeia de texto.
MyVal Dim como Boolean ... MyVal só pode ser Verdadeiro ou Falso, é um estado booleano, e não uma string.

Há um padrão chamado Variant VarType que pode realmente ser usado como qualquer coisa, poderoso, mas problemático. Desde que você não declarar variáveis​​, todas as variáveis ​​são variantes sua revelia.

Att...

 
Postado : 02/05/2012 5:22 pm