EXCEL TOOLS
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
[code]
และปิดด้วย [/code]
ตัวอย่างเช่น [code]dim r as range[/code]
เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)Code: Select all
Sub ExportSelectedRangeToCSV()
' ให้ผู้ใช้งานเลือกที่อยู่จัดเก็บไฟล์ CSV
Dim filePath As Variant
filePath = Application.GetSaveAsFilename(InitialFileName:="MyFile.csv", fileFilter:="CSV Files (*.csv), *.csv")
' ตรวจสอบว่าผู้ใช้งานได้เลือกที่อยู่จัดเก็บไฟล์หรือไม่
If filePath = False Then
Exit Sub
End If
' เตรียมตัวแปรสำหรับบันทึกข้อมูล
Dim selectedRange As Range
Set selectedRange = Selection
' สร้างไฟล์ CSV โดยใช้ข้อมูลจากช่วงของเซลล์ที่เลือก
selectedRange.Copy
Workbooks.Add
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.SaveAs filename:=filePath, FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close
End Sub
ดำเนินการและปฏิบัติตามเรียบร้อยแล้วครับsnasui wrote: Sun Apr 23, 2023 1:13 pmกรุณาแนบภาพ แนบไฟล์ที่เขียน Code เอาไว้แล้วมาที่ฟอรัมนี้โดยตรง ระบุ Version ของ Excel ที่ใช้ตาม Link นี้ viewtopic.php?p=103177#p103177 นอกจากนี้กรุณาอ่านกฎการใช้บอร์ดทุกข้อด้านบนด้วยครับ
![]()
Code: Select all
Sub WriteCSVFile()
Dim My_filenumber As Integer
Dim logStr As String
Dim l As Long
Dim rall As Range
Dim r As Range, ra As Range
My_filenumber = FreeFile
Open "C:\Downloads\Test2.csv" For Output As #My_filenumber
Print #My_filenumber, logStr
With ActiveSheet
Set rall = .Range("a1", .Range("a" & .Rows.Count).End(xlUp))
For Each r In rall
If r.Value <> "" Then
Set ra = .Range(r, r.End(xlToRight))
logStr = VBA.Join(Application.Transpose( _
Application.Transpose(Application.Index(ra, 0))), ",")
Print #My_filenumber, logStr
l = l + 1
End If
Next r
Close #My_filenumber
End With
End Sub
Code: Select all
logStr = VBA.Join(Application.Transpose( _
Application.Transpose(Application.Index(ra, 0))), ",") & ","