:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser

code vba Import text file to excel แล้วติดปัญหา

Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่

Post a reply


This question is a means of preventing automated form submissions by spambots.
Smilies
:D :thup: :cp: :flw: :rz: :sg: :tt: :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :ard: :arl: :aru: :| :mrgreen: :geek: :ugeek:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: code vba Import text file to excel แล้วติดปัญหา

Re: code vba Import text file to excel แล้วติดปัญหา

#9

by benzdekhad » Sat Oct 27, 2012 9:53 am

ขอบคุณมากเลยนะครับอาจารย์ ตอนนี้ใช้ได้ดีมากเลยครับ ไม่ติดปัญหาใดๆแล้วครับ สามารถ import text file ได้ทุก file แล้ว

Re: code vba Import text file to excel แล้วติดปัญหา

#8

by snasui » Fri Oct 26, 2012 10:32 pm

:D ติดปัญหาเมื่อเกิด Error แล้วจะวน Loop ไม่รู้จบ ผมจึง Mark เป็น Comment ไว้ตามที่เห็นครับ

Re: code vba Import text file to excel แล้วติดปัญหา

#7

by benzdekhad » Fri Oct 26, 2012 10:28 pm

import text file มาได้ปกติแล้วครับอาจารย์ขอบคุณมากเลยนะครับ ไม่ทราบว่า code เดิมติดปัญหาอะไรเหรอครับอาจารย์

Re: code vba Import text file to excel แล้วติดปัญหา

#6

by snasui » Fri Oct 26, 2012 10:20 pm

:D ลองปรับ Code เป็นตามด้านล่างครับ

Code: Select all

Sub Import2NextCol()

      Dim Filt$, Title$, FileText$
      Dim FileName$, N&, FirstEmpty&

      '//show dialog to import file
      '{Note: Office 2000 requires that
      '(*.bas; *.frm; *.cls;*.txt;*.log;*.frx)
      'be written twice, for later versions you
      'can delete the second instance}
      Filt = "VB Files (*.bas; *.frm; *.cls;*.txt;*.log;*.frx) " & _
             "(*.bas; *.frm; *.cls;*.txt;*.log;*.frx)," & _
             "*.bas;*.frm;*.cls;*.txt;*.log;*.frx"
      Title = "SELECT A FOLDER - DOUBLE-CLICK OR CLICK " & _
              "OPEN TO IMPORT - CANCEL TO QUIT"
      FileName = Application.GetOpenFilename _
                 (FileFilter:=Filt, FilterIndex:=5, Title:=Title)

      '//find first empty column
'      On Error GoTo IsBlankSheet    '< Error = nothing to find
      On Error Resume Next
      FirstEmpty = Cells.Find("*", SearchOrder:=xlByColumns, _
                              LookIn:=xlValues, SearchDirection:=xlPrevious). _
                   Column
      If FirstEmpty = 0 Then FirstEmpty = 1
      '//all columns contain text
      If FirstEmpty = 257 Then
            MsgBox "Sorry, no more columns on this sheet"
            Exit Sub
      End If

TextEntry:
      '//check there is a file to import
      If Dir(FileName) <> Empty Then
            '//import the text
            Application.ScreenUpdating = False
            Open (FileName) For Input As #1
            N = 1
            Do While Not EOF(1)
                 Line Input #1, FileText
                  Rows(N).Columns(FirstEmpty) = FileText
                  N = N + 1
            Loop      '< Loop until end of file
            Close #1

            '//tart up the spreadsheet
            ActiveWindow.DisplayGridlines = False
            With Cells
                  .Font.Size = 9
                  Columns.AutoFit
                  Rows.AutoFit
            End With

            '//goto the start of the entered text & exit sub
            Application.Goto Rows(1).Columns(FirstEmpty), scroll:=True
      End If
'      Exit Sub
'
'IsBlankSheet:
'      '//start in column 1
'      FirstEmpty = 1
'      '//clear the error & continue import
'      Resume TextEntry
End Sub

Re: code vba Import text file to excel แล้วติดปัญหา

#5

by benzdekhad » Fri Oct 26, 2012 9:17 pm

ไม่ฟ้อง error อะไรขึ้นมาเลยครับ มันจะค้างซักพักหนึ่งแล้ว โปรแกรม excel มัน not responding ไปเลยครับ

Re: code vba Import text file to excel แล้วติดปัญหา

#4

by snasui » Fri Oct 26, 2012 9:11 pm

:D ขอถามเพิ่มเติมว่าโปรแกรมฟ้องว่าอย่างไรหรือไม่ครับ หากมีการฟ้องช่วยจับภาพมาดูกันครับ

Re: code vba Import text file to excel แล้วติดปัญหา

#3

by benzdekhad » Fri Oct 26, 2012 9:02 pm

เรียน อาจารย์ snasui

code ที่อาจารย์แก้ให้ นั้นผลลัพธ์การ import ข้อมูลมาได้ตามที่ต้องการเลยครับ แต่ทางผมติดอยู่คือ การ import ไฟล์ source 1 ในไฟล์แนบ ผมลอง import มาแล้วโปรแกรมค้างตลอดเลย แต่ ไฟล์ source 2 กลับ import ได้ปกติ ไม่ทราบว่า โค๊ด vba ที่ผมใช้ผิดพลาดต้องส่วนไหนเหรอครับ หรือว่าเป็นข้อจำกัดของโปรแกรม excel
Attachments
source data.rar
(94.33 KiB) Downloaded 34 times

Re: code vba Import text file to excel แล้วติดปัญหา

#2

by snasui » Fri Oct 26, 2012 4:19 pm

:D ลองตามนี้ครับ

ปรับ Code จาก

Code: Select all

Do While Not EOF(1)
      Input #1, FileText
      Rows(N).Columns(FirstEmpty) = FileText
      N = N + 1
Loop      '< Loop until end of file
เป็น

Code: Select all

Do While Not EOF(1)
      Line Input #1, FileText '<== Change this line
      Rows(N).Columns(FirstEmpty) = FileText
      N = N + 1
Loop      '< Loop until end of file

code vba Import text file to excel แล้วติดปัญหา

#1

by benzdekhad » Fri Oct 26, 2012 2:34 pm

ผมมีปัญหาการ import text file มาลง excel file โดยตัวอย่างที่ติดปัญหาคือ ข้อมูลจาก text file จะเป็น row เดียวกันเช่น
ข้อมูล "TCA1-046AS01 uptime is 1 year, 19 weeks, 1 day, 19 hours, 38 minutes"
แต่พอผม import ด้วย vba code ลง excel file ข้อมูลจะได้เป็น

TCA1-046AS01 uptime is 1 year
19 weeks
1 day
19 hours
38 minutes

ซึ่งเป็นข้อมูลที่มีหลายบรรทัดขึ้นมา ผมได้แนบ excel file และ text file ที่ผมใช้ เข้ามาโดยใน excel file column A จะเป็นผลลัพธ์ที่ได้จากการ import โดย vba code ส่วน column C คือผลลัพธ์ที่อยากให้ออกมาในรูปแบบนั้น รบกวนช่วยดู code vba ว่าผิดพลาดต้องส่วนใดด้วยครับ

...ขอบคุณครับ...
Attachments
Txt to Excel.zip
(87.09 KiB) Downloaded 44 times

Top