How to replicate a value depending on what is inside the cell?

Asked

Viewed 60 times

0

I have a code that always puts 264 empty characters after the last word typed inside the cell; then puts more "00000000000000" after 264 empty characters and export to txt, as shown below:

inserir a descrição da imagem aqui

My question is I have to put at the end of the line after the 264 characters always "99999999999" that in the cell is with the following field filled : 5497558138889999999999998

Follows my code:

Sub Macro14()

Application.ScreenUpdating = False

Dim arq1 As Long
Dim name As String
Dim op As Variant
Dim linha As Long
Dim res As String

On Error GoTo fim:

linha = 1

While ThisWorkbook.Sheets("TesteConcluido").Cells(linha, 1) <> ""

name = ThisWorkbook.Sheets("TesteConcluido").Cells(linha, 1)

While Len(name) < 265

name = name + " "

Wend


If linha = 42 And 43 Then

name = name & "999999999990000"

Else

name = name & "000000000000000"

End If

arq1 = FreeFile

Open fileTeste For Append As arq1

Print #arq1, name

Close #arq1
linha = linha + 1
Wend

fim:
If Err.Number = "70" Then

Resume

End If

MsgBox "O arquivo foi exportado com sucesso! ", vbInformation, "Exportar arquivos"

res = MsgBox("Você gostaria voltar ao menu?", vbYesNo + vbQuestion, "QNIS - CAIXA")

If res = vbYes Then

Else

ThisWorkbook.Close savechanges:=True
Unload FormQNIS
End If

Application.ScreenUpdating = True


End Sub
  • Could you mark some answers to the questions you asked as accepted and answered. This way more people will be willing to help you. For more information about Sopt, read the Community FAQ

  • 1

    All right I answered the other questions, thank you

1 answer

0


Follows solution of the problem:

Option Explicit

Sub Macro14()

Application.ScreenUpdating = False

Dim arq1 As Long
Dim name As String
Dim op As Variant
Dim linha As Long
Dim res As String
Dim texto As String

texto = "54975581388899999999999"


On Error GoTo fim:
    
linha = 1

While ThisWorkbook.Sheets("TesteConcluido").Cells(linha, 1) <> ""
     
name = CStr(ThisWorkbook.Sheets("TesteConcluido").Cells(linha, 1).Value)
    
While Len(name) < 265
            
name = name + " "
            
Wend

If InStr(1, name, texto, vbTextCompare) > 0 Then

name = name & "999999999990000"

Else

name = name & "000000000000000"

End If
       
arq1 = FreeFile

Open fileTeste For Append As arq1

Print #arq1, name

Close #arq1
linha = linha + 1
Wend

fim:
If Err.Number = "70" Then

Resume

End If

MsgBox "O arquivo foi exportado com sucesso! ", vbInformation, "Exportar arquivos"

res = MsgBox("Você gostaria voltar ao menu?", vbYesNo + vbQuestion, "QNIS - CAIXA")
    
If res = vbYes Then
  
Else

ThisWorkbook.Close savechanges:=True
Unload FormQNIS
End If

Application.ScreenUpdating = True


End Sub

Browser other questions tagged

You are not signed in. Login or sign up in order to post.