PDA

View Full Version : ADD-IN MACRO



girisbisht
12-09-2012, 11:57 PM
Dear Sir, I do not know basics of VBA. I know only Excel. I have added a module in an existing add-in, which have to create text file of a sheet named as "ECR" Sheet. I am getting problem. When I copy this module in inside the file, it works well. But in Add-in module this does not works. I get Vba Error Code 13 - Data Type Mismatch. The date in Row Number. I want to use this module as in a Add-in module. Kindly help me.

girisbisht
12-10-2012, 01:44 AM
Sub TEXTXFILE()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
On Error GoTo ErrHandler:

Dim txtfile As TextStream
Dim fso As New Scripting.FileSystemObject
Dim sFile As String
sFile = Application.GetSaveAsFilename(InitialFileName:=txtfilename(), FileFilter:="Text Files (*.txt), *.txt")
If sFile = "False" Then Exit Sub
If fso.FileExists(sFile) Then
Kill sFile
Set txtfile = fso.CreateTextFile(sFile, True)
Else
Set txtfile = fso.CreateTextFile(sFile, True)
End If

ActiveWorkbook.Sheets("ECR").Select
For Decount = 1 To ActiveSheet.UsedRange.Rows.Count - 1
d = "#~#"
DD1 = Left(Cells(Decount + 1, 1), 7)
DD2 = UCase(Cells(Decount + 1, 2))
DD3 = Left(Cells(Decount + 1, 3), 10)
DD4 = Left(Cells(Decount + 1, 4), 10)
DD5 = Left(Cells(Decount + 1, 5), 10)
DD6 = Left(Cells(Decount + 1, 6), 10)
DD7 = Left(Cells(Decount + 1, 7), 10)
DD8 = Left(Cells(Decount + 1, 8), 10)
DD9 = Left(Cells(Decount + 1, 9), 10)
DD10 = Left(Cells(Decount + 1, 10), 10)
If Cells(Decount + 1, 11) > 0 Then
DD11 = Left(Cells(Decount + 1, 11), 2)
Else
DD11 = 0
End If
If Cells(Decount + 1, 12) > 0 Then
DD12 = Left(Cells(Decount + 1, 12), 10)
Else
DD12 = 0
End If
If Cells(Decount + 1, 13) > 0 Then
DD13 = Left(Cells(Decount + 1, 13), 10)
Else
DD13 = 0
End If
If Cells(Decount + 1, 14) > 0 Then
DD14 = Left(Cells(Decount + 1, 14), 10)
Else
DD14 = 0
End If
If Cells(Decount + 1, 15) > 0 Then
DD15 = Left(Cells(Decount + 1, 15), 10)
Else
DD15 = 0
End If
If Cells(Decount + 1, 16) > 0 Then
DD16 = Left(Cells(Decount + 1, 16), 10)
Else
DD16 = 0
End If
DD17 = UCase(Cells(Decount + 1, 17))
DD18 = UCase(Left(Cells(Decount + 1, 18), 1))
If Cells(Decount + 1, 19) > 0 Then
DD19 = Right("00" & Day(Cells(Decount + 1, 19)), 2) & "/" & _
Right("00" & Month(Cells(Decount + 1, 19)), 2) & "/" & _
Year(Cells(Decount + 1, 19))
Else
DD19 = ""
End If
DD20 = UCase(Left(Cells(Decount + 1, 20), 1))
If Cells(Decount + 1, 21) > 0 Then
DD21 = Right("00" & Day(Cells(Decount + 1, 21)), 2) & "/" & _
Right("00" & Month(Cells(Decount + 1, 21)), 2) & "/" & _
Year(Cells(Decount + 1, 21))
Else
DD21 = ""
End If
If Cells(Decount + 1, 22) > 0 Then
DD22 = Right("00" & Day(Cells(Decount + 1, 22)), 2) & "/" & _
Right("00" & Month(Cells(Decount + 1, 22)), 2) & "/" & _
Year(Cells(Decount + 1, 22))
Else
DD22 = ""
End If
If Cells(Decount + 1, 23) > 0 Then
DD23 = Right("00" & Day(Cells(Decount + 1, 23)), 2) & "/" & _
Right("00" & Month(Cells(Decount + 1, 23)), 2) & "/" & _
Year(Cells(Decount + 1, 23))
Else
DD23 = ""
End If
If Cells(Decount + 1, 24) > 0 Then
DD24 = Right("00" & Day(Cells(Decount + 1, 24)), 2) & "/" & _
Right("00" & Month(Cells(Decount + 1, 24)), 2) & "/" & _
Year(Cells(Decount + 1, 24))
Else
DD24 = ""
End If
DD25 = Left(Cells(Decount + 1, 25), 1)
'-------------------------
txtfile.WriteLine (DD1 & d & DD2 & d & DD3 & d & DD4 & d & DD5 & d & DD6 & d & DD7 & _
d & DD8 & d & DD9 & d & DD10 & d & DD11 & d & DD12 & d & DD13 & d & DD14 & d _
& DD15 & d & DD16 & d & DD17 & d & DD18 & d & DD19 & d & DD20 & d & DD21 & d _
& DD22 & d & DD23 & d & DD24 & d & DD25)
'-------------------------
Next Decount
MsgBox "The text file is saved as " & sFile

Exit Sub
ErrHandler:
If Err.Number = 13 Then
MsgBox "Vba Error Code 13 - Data Type Mismatch. The date in Row Number " _
& Decount + 1 & " is not in the date format"
Else
MsgBox "Vba Error Code " & Err.Number & " - " & Err.Description & _
". Textfile is not created successfully."
GoTo ExitFunction:
End If
ExitFunction:
On Error GoTo 0

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub