Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Sub Textfile()

Dim FilePath As String
Dim CellData As String
Dim LastCol As Long
Dim LastRow As Long

LastCol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
CellData = ""
DefaultFilePath = "xyz"

FilePath = Application.DefaultFilePath & "\RPA.txt"
Open FilePath For Output As #2
For i = 1 To LastRow
   For j = 1 To LastCol
      If j = LastCol Then
         CellData = CellData + Trim(ActiveCell(i, j).value)
      Else
         CellData = CellData + Trim(ActiveCell(i, j).value) + "  "
      End If
   Next j
   Write #2, CellData
   CellData = ""
Next i
Close #2

MsgBox ("Done")

End Sub


What I have tried:

In this particular line


VB
CellData = CellData + Trim(ActiveCell(i, j).value) + "  "


I am getting data type mismatch error, I have pasted the whole code in the description, what could be the possible error.
Posted
Updated 11-Apr-18 7:12am
v4
Comments
Richard MacCutchan 11-Apr-18 8:50am    
It most likely means that the cell content value is not a string, so you cannot concatenate another string to it.

Try the following :
VB
CellData = CellData + Trim("" + ActiveCell(i, j).value) + "  "
 
Share this answer
 
Comments
Member 13773934 12-Apr-18 6:42am    
I tried the Code but same thing is happened, and actually i have numeric values as well in that line Item so what kind of Data type I can use instead of string.
If you're trying to concatenate the data from the cell try using & operator. The cell may contain a numeric value which would cause an error if a numeric value is trying to be added to a string. You can also use CStr if you like.

In other words
CellData = CellData & CStr(Trim(ActiveCell(i, j).value)) & "  "
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900