Macro : Export file to Excel แล้วไม่มี File
Posted: Thu Feb 20, 2020 4:13 pm
เรียน สอบถามอาจารย์ครับ
1. File นี้คือตัวอย่างนะครับ File จริงมีขนาดใหญ่ เลยยกตัวอย่างไฟล์นี้ครับ
ผมต้องการ Export file to Excel แล้วไม่มี File ไม่แน่ใจว่าต้องปรับ Code ตรงไหนหรือไม่ครับ ขอบคุณครับ
2. ถ้า Sheet "Forms" ที่ต้องการ Export มีการดึงสูตรจาก Sheet "Input" หลังจาก Export แล้ว สูตรจะไปด้วยหรือไม่ครับ
1. File นี้คือตัวอย่างนะครับ File จริงมีขนาดใหญ่ เลยยกตัวอย่างไฟล์นี้ครับ
ผมต้องการ Export file to Excel แล้วไม่มี File ไม่แน่ใจว่าต้องปรับ Code ตรงไหนหรือไม่ครับ ขอบคุณครับ
2. ถ้า Sheet "Forms" ที่ต้องการ Export มีการดึงสูตรจาก Sheet "Input" หลังจาก Export แล้ว สูตรจะไปด้วยหรือไม่ครับ
Code: Select all
Private Sub CommandButton1_Click()
Worksheets("Data").Activate
'www.contextures.com
'for Excel 2010 and later
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")
'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")
'create default name for saving file
strFile = Sheets("Input").Range("E6").Value & "_" & Sheets("Input").Range("E7").Value & "_" & Sheets("Input").Range("E12").Value & ".xls"
strPathFile = strPath & strFile
'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="Excel Files (*.xls), *.xls", _
Title:="Select Folder and FileName to save")
'export to PDF if a folder was selected
If myFile <> "False" Then
'wsA.ExportAsFixedFormat
'Copy the ActiveSheet to new workbook
For Each wsA In ActiveWorkbook.Worksheets
wsA.UsedRange.Copy
wsA.UsedRange.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Next
'confirmation message with file info
MsgBox "Excel file has been created: " _
& vbCrLf _
& myFile
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create Excel file"
Resume exitHandler
End Sub