สร้างชื่อไฟล์เป็นประเภทอื่น
Posted: Wed Mar 28, 2012 8:00 pm
อาจารย์ครับ จาก code ข้างล่างนี้ทำเพื่อใช้งานในการแปลงจาก excel เป็น text file โมดูลนี้ชื่อButton1 ครับอยากทราบว่า
ตรงนี้ที่เลข 999 ต้องเปิดมาโครทุกครั้งเพื่อมาเปลี่ยนเป็นรหัสสินค้าทุกครั้ง อยากทราบว่าถ้าผมจะให้มันไปดึงรหัสสินค้าที่ sheet QPM_DFD ช่อง F2 ต้องแก้ไขโค้ดตรงใหนครับ 999 คือรหัสสินค้า ไม่สะดวกเป็นอย่างมากเพราะบางคนเปิดมาโครไม่เป็น
Format(Time(), "hhmmss") & "999" & ".txt" อยู่บรรทุดที่ 23 ครับรหัสอาจจะมีหลากหลาย เช่น 895 เป็นต้น
ตรงนี้ที่เลข 999 ต้องเปิดมาโครทุกครั้งเพื่อมาเปลี่ยนเป็นรหัสสินค้าทุกครั้ง อยากทราบว่าถ้าผมจะให้มันไปดึงรหัสสินค้าที่ sheet QPM_DFD ช่อง F2 ต้องแก้ไขโค้ดตรงใหนครับ 999 คือรหัสสินค้า ไม่สะดวกเป็นอย่างมากเพราะบางคนเปิดมาโครไม่เป็น
Format(Time(), "hhmmss") & "999" & ".txt" อยู่บรรทุดที่ 23 ครับรหัสอาจจะมีหลากหลาย เช่น 895 เป็นต้น
Code: Select all
Sub Button1_Click()
'ActiveSheet.Range("B7").CurrentRegion.Select
ActiveSheet.Range("BA7", _
ActiveSheet.Range("a7").End(xlDown).End(xlToLeft)).Select
Dim FileNum As Integer
Dim ColumnCount As Integer
Dim RowCount As Integer
Dim data_line As String
Dim aa As String
' Prompt user for destination file name.
'DestFile = InputBox("Enter the destination filename" & _
' Chr(10) & "(with complete path and extension):", _
' "Quote-Comma Exporter")
' Obtain next free file handle number.
FileNum = FreeFile()
' Turn error checking off.
On Error Resume Next
'Fix output file path to D:\xxxx.txt
DestFile = "D:\098533326_659498369_TOLRINGDFD_" & _
Format(Now(), "YYYYMMDD") & _
Format(Time(), "hhmmss") & "999" & ".txt"
' Attempt to open destination file for output.
Open DestFile For Output As #FileNum
' If an error occurs report it and end.
If Err <> 0 Then
MsgBox "Cannot open filename " & DestFile
End
End If
' Turn error checking on.
On Error GoTo 0
Print #FileNum, "SOF"
Print #FileNum, "# 65-949-8369"
Print #FileNum, "# TOLRINGCCP"
Print #FileNum, "# DFD 1.1"
' Loop for each row in selection.
For RowCount = 1 To Selection.Rows.Count
data_line = ""
' Loop for each column in selection.
' column 12-103 is number
For ColumnCount = 1 To Selection.Columns.Count
' Write current cell's text to file with quotation marks.
'column number
If (ColumnCount >= 12) And (ColumnCount <= 103) Then
'And ColumnCount
'If ColumnCount = 11 _
'Or ColumnCount = 14 _
'Or (ColumnCount >= 16 _
And ColumnCount <= 67) Then
If Selection.Cells(RowCount, ColumnCount).Text <> "" Then 'not blank
' Print #FileNum, Selection.Cells(RowCount, _
' ColumnCount).Text;
data_line = data_line & Selection.Cells(RowCount, _
ColumnCount).Text
End If
Else ' column not number
If Selection.Cells(RowCount, ColumnCount).Text <> "" Then 'not blank
' Print #FileNum, """" & Selection.Cells(RowCount, _
' ColumnCount).Text & """";
data_line = data_line & """" & Selection.Cells(RowCount, _
ColumnCount).Text & """"
Else
data_line = data_line & """" & """"
' Print #FileNum, """" & """", 'write "" to file
End If
End If
If ColumnCount = Selection.Columns.Count Then
' If so, then write a blank line.
' Print #FileNum,
Else
data_line = data_line & ","
' Print #FileNum, ",",
End If
' Start next iteration of ColumnCount loop.
Next ColumnCount
Print #FileNum, data_line
' Print #FileNum,
' Start next iteration of RowCount loop.
Next RowCount
' Close destination file.
Print #FileNum, "EOF"
Close #FileNum
MsgBox "Already created to file path " & DestFile
End Sub