我有一张excel表格:
2015年2月的月度薪酬
Sr No. Account No. Employee Name Amount
1 45264 Ranapratap Chandra 6159
2 45370 Darshan Doshi 21116
3 47378 KamalJeet Duhra 18100
4 45316 Prasad Belvalkar 26624
为了与我需要生成的系统之一集成 包含所有数据的文本文件, 1.最后一栏,记录在记事本的第171栏。
我已经尝试了一切,现在我很累。
以下是代码段:
Option Explicit
Sub ToText()
Dim pth As String
pth = ThisWorkbook.Path
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Dim a As Object
Dim OutPutFile As String
'Creating a output file manually
OutPutFile = CStr(Trim(InputBox("Enter File Name - Do Not Use Special Characters or Spaces", "User Alert - Enter File Name")))
Set a = fs.CreateTextFile(pth & "\" & OutPutFile & ".txt", True)
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets(CStr(Trim(InputBox("Enter Sheet Name / copy - Paste the Sheet Name as in the Excel Workbook", "User Alert - Enter Sheet Name"))))
Dim rng As Range
Set rng = sh.UsedRange
Dim sRange As String
sRange = GetTextFromRangeText(rng)
Call a.WriteLine(sRange)
a.Close
End Sub
Function GetTextFromRangeText(ByVal poRange As Range) As String
Dim vRange As Variant
Dim sRet As String
Dim i As Integer
Dim j As Integer
If Not poRange Is Nothing Then
vRange = poRange
For i = LBound(vRange) To UBound(vRange)
For j = LBound(vRange, 2) To UBound(vRange, 2)
sRet = sRet & vRange(i, j)
Next j
sRet = sRet & vbCrLf
Next i
End If
GetTextFromRangeText = sRet
End Function
这会产生: 2015年2月的月度薪酬
Sr No.Account No.Employee NameAmount
145264Ranapratap Chandra6159
245370Darshan Doshi21116
347378KamalJeet Duhra18100
445316Prasad Belvalkar26624