Page 1 of 2

สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 8:00 pm
by godman
อาจารย์ครับ จาก code ข้างล่างนี้ทำเพื่อใช้งานในการแปลงจาก excel เป็น text file โมดูลนี้ชื่อButton1 ครับอยากทราบว่า

ตรงนี้ที่เลข 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



Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 8:23 pm
by snasui
:D
วันวานยังหวานอยู่ wrote:ถ้าผมจะให้มันไปดึงรหัสสินค้าที่ sheet QPM_DFD ช่อง F2 ต้องแก้ไขโค้ดตรงใหนครับ 999 คือรหัสสินค้า ไม่สะดวกเป็นอย่างมากเพราะบางคนเปิดมาโครไม่เป็น
Format(Time(), "hhmmss") & "999" & ".txt" อยู่บรรทุดที่ 23 ครับรหัสอาจจะมีหลากหลาย เช่น 895 เป็นต้น
สามารถเปลี่ยนเป็นตามด้านล่างครับ

Code: Select all

Format(Time(), "hhmmss") & Sheets("QPM_DFD").Range("F2").Value & ".txt"

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 8:31 pm
by godman
มันยังไม่เปลี่ยนเลยครับ อาจารย์ เพราะไฟล์ .txt ที่ไปสร้างใหม่จะมีรูปแบบไฟล์ว่า 098533326_659498369_TOLRINGDFD_25550328202757999
สามตัวสุดท้ายคือ รหัสสินค้า ในที่นี่คือ 999 ผมลองเปลี่ยนค่าเซลล์ใน f2 ชี้ัตดังกล่าวแล้วยังคงเดิมครับ รูปแบบนี้คือ พ.ศ เดือน วันที่ เวลา อยู่ในรูปชั่วโมง นาที วินาทีและตัดสุดท้ายคือรหัสสินค้าครับ 25550328202757999
นี่คือส่วนที่สี่นะครับ ที่ผมอยากให้เปลี่ยนรหัสสินค้าซึ่งเป็นส่วนประกอบชื่อไฟล์ และสุดทัายไฟลชื่อนี้จะไปอยู่ที่ Drive D ตามที่เขียนมาโครไว้อะครับ

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 8:57 pm
by snasui
:D ลองดู Code ด้านบนใหม่ครับ :roll: เนื่องจากเครื่องหมาย " ด้านท้ายหายไป 1 ตัวผมกลับไปแก้ไขให้แล้ว หากยังไม่ได้ช่วยแจ้งด้วยครับว่าโปรแกรมฟ้องว่าอย่างไร

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 9:02 pm
by godman
มันขึ้นว่า can not open file name ครับ

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 9:03 pm
by godman
นี่คือ โค้ดที่เปลี่ยนแล้วครับ ให้เลือกจากช่อง F2 ผมกดปุ่มมาโครแล้วครับ

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") & Sheets("QPM_DFDQPM_DFD").Range("F2").Value & ".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




Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 9:14 pm
by snasui
:tt: ผม Copy ชื่อชีทมาวางซ้ำ ได้กลับไปแก้ใน Code ด้านบนให้แล้ว :roll: หรือสามารถจะ Copy code ด้านล่างไปใช้ได้เลยครับ

Code: Select all

Format(Time(), "hhmmss") & Sheets("QPM_DFD").Range("F2").Value & ".txt"

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 9:23 pm
by godman
ขอบคุณครับ ใช้ได้ผลดีเลยครับ

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 9:40 pm
by bank9597
:D :D :D :D :tt: :tt: :tt: :tt:

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 9:42 pm
by godman
อาจารย์ครับ จาก โมดูลนี้ Button3 อยู่ในไฟล์เดียวกัน ผมอยากลดขึ้นตอน เพราะตามข้อกำหนดของลูกค้าจะต้องส่งเป็นไฟล์ 2 ไฟล์คือไฟล์ชนิด MF ซึ่งใช้ code ข้างล่างนี้ในการสร้าง text file แต่ปัญหาคือขั้นตอนมากเกินไป ถ้าผมต้องการให้มันพ่วงกัน คือกดแค่ครั้งเดียว ได้ไหมครับ เพราะข้อมูลใช้ชุดเดียวกัน แต่ว่าไฟล์นี้ไปเอาชือและเน้อหาจากชี้ตที่ชื่อว่า QPM-MF ครับในการสร้าง txt file โดยผู้ใช้ต้องกด 2 ครั้งจึงจะถือว่าทำงานสำเร็จ แต่ว่าปัญหาคือ ผู้ใช้อาจลืมกดครั้งที่สอง เพื่อสร้างไฟล์ MF หลังจากสร้างไฟล์ DFD (ที่อาจารย์ตอบไปแล้วคือ Button 1 ครับ มีวิธีที่จะรวมมาโครทั้งสองให้อยู่ในครั้งเดียว แต่สร้างสองไฟล์คือ MF กับ DFD สังเกตุได้อย่างไรว่าไฟล์ใหนชื่อ MF หรือ DFD
098533326_659498369_TOLRINGMF_25550328213825968 จริงๆต้องจุด txt ต่อท้ายครับ
ที่ตรงคำว่า TOLRINGMF นี่แหละครับคือ MF ส่วน
098533326_659498369_TOLRINGDFD_25550328212725123
อันนี้คือ DFD สังเกตุจาก TOLRINGDFD ครับนี่คือ DFD นะครับ จริงๆข้อมูลของสินค้าชนิดเดียวกันครับ

Code: Select all

Sub Button3_Click()
'Range("A4" & LastRow).Select
'Range("A6").End(xlDown).Select
'Range("A4").End(xlRight).Select
  ActiveSheet.Range("c5", _
   ActiveSheet.Range("a4").End(xlDown).End(xlToLeft)).Select
Dim DestFile As String
    Dim FileNum As Integer
    Dim ColumnCount As Integer
    Dim RowCount As Integer
    Dim data_line As String
    Dim RowCo As Integer
    Dim ColumnCo As Integer
    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()
    
    
    On Error Resume Next
    'Fix output file path to D:\xxxx.txt
     DestFile = "D:\098533326_659498369_TOLRINGMF_" & _
              Format(Now(), "YYYYMMDD") & _
              Format(Time(), "hhmmss") & "968" & ".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

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 9:50 pm
by snasui
:D ก่อนบรรทัด End Sub ของ Button1_Click ให้ใส่ Button3_Click ซึ่งจะได้เป็น

Code: Select all

Sub Button1_Click()
   ...
   Button3_Click
End Sub
เครื่องหมาย ... คือ Code อื่น ๆ

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 10:09 pm
by godman
ขอบคุณครับ แต่พอผมไปเปิดใน Dirve D ที่เป็นปลายทาง ไฟล์ MF เหลือแค่นี้ครับ
SOF
# 65-949-8369
# TOLRINGCCP
"1","2",3
EOF

จริงๆ แล้วมันต้องเป็นแบบนี้อะครับ
SOF
# 65-949-8369
# TOLRINGCCP
"FILE_NAME","FILE_CREATION_DATE","NUMBER_OF_RECORDS"
"098533326_659498369_TOLRINGDFD_20120229162214123.txt","20120229162214123",5
EOF
เกิดจากอะไรหรือครับ

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 10:13 pm
by godman
ผมลืมแนบไฟล์ไป ผมคลิก แล้วมันโอเคเรื่องชื่อไฟล์ ครับ แต่ว่า เน้อหาที่เป็น txt มันไม่เหมือน Button 3 ครับ

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 10:15 pm
by godman
ผมสังเกตเห็นว่า ชื่อไฟล์ มันเอาชื่อ 3 ตัวท้าย รหัสสินค้า 123 มาตั้งแทน ผมว่ามันต้องเกี่ยวกันนะครับ

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 10:28 pm
by snasui
วันวานยังหวานอยู่ wrote:ขอบคุณครับ แต่พอผมไปเปิดใน Dirve D ที่เป็นปลายทาง ไฟล์ MF เหลือแค่นี้ครับ
SOF
# 65-949-8369
# TOLRINGCCP
"1","2",3
EOF

จริงๆ แล้วมันต้องเป็นแบบนี้อะครับ
SOF
# 65-949-8369
# TOLRINGCCP
"FILE_NAME","FILE_CREATION_DATE","NUMBER_OF_RECORDS"
"098533326_659498369_TOLRINGDFD_20120229162214123.txt","20120229162214123",5
EOF
เกิดจากอะไรหรือครับ
:lol: ลองตรวจสอบโดยการกดแป้น F8 เพื่อ Run ทีละ Step ดูว่าผิดพลาดตรงไหน แล้วแจ้งมา จะได้ช่วยกันดูต่อครับ

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 10:42 pm
by godman
ผมลองเปิด view macro ดูแล้ว และเปิดหน้าที่มีโค้ด อยู่ แล้วกด F8 มันขึ้นสีเหลืองทั้งหมดเลย ไม่พบความผิดปกติครับ

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 10:58 pm
by snasui
:D ความผิดปกติหมายถึง ค่าไหนที่ Code ควรจะนำมาบันทึกแต่ข้ามไปไม่บันทึก ไม่ใช่ว่า Run แล้วมีการฟ้องให้ทำการ Debug ครับ

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 11:05 pm
by godman
อันนี้คือผมกดปุ่มมาโครในหน้า QPM-MF นะครับ มันก็ไปที่ drive D เนื้อหาที่เป็น text file คือ
SOF
# 65-949-8369
# TOLRINGCCP
"FILE_NAME","FILE_CREATION_DATE","NUMBER_OF_RECORDS"
"098533326_659498369_TOLRINGDFD_25550328193058968","25550328193058968",5
EOF
แต่เมื่อผมกด Macro ที่หน้าที่สอง QPM-DFD ที่เป็นแบบต่อเนื่องกดครั้งที่ 1 DFD จะไปพอกดครั้งที่สอง mf จะไป แต่เนื้อหาที่ไปมีแค่นี้
SOF
# 65-949-8369
# TOLRINGCCP
"1","2",3
EOF

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 11:07 pm
by godman
ดังนั้นค่าที่ข้ามไปคือ ประโยคนี้ครับ
"FILE_NAME","FILE_CREATION_DATE","NUMBER_OF_RECORDS"
"098533326_659498369_TOLRINGDFD_25550328193058968","25550328193058968",5

Re: สร้างชื่อไฟล์เป็นประเภทอื่น

Posted: Wed Mar 28, 2012 11:10 pm
by snasui
:lol: คงเข้าใจคลาดเคลื่อนครับ

ผมให้กด F8 เพื่อตรวจสอบว่าเมื่อ Run ทีละ Step แล้ว ค่าไหนที่ Code ควรนำไปบันทึกแแต่ไม่บันทึก โดยเกิดจากบรรทัดใดใน Code ให้ยกบรรทัดนั้นมาถาม ไม่ใช่ยกผลลัพธ์มาถาม จำเป็นจะต้องตรวจมาให้ผมก่อน ผมเน้นที่ติดปัญหาเท่านั้นครับ