Page 1 of 2

สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Sat Sep 10, 2016 11:28 pm
by primeval
จาก Sheet Macro

มีการดึงข้อมูลมาจาก Sheet ตารางสรุปประเมินความพึงพอใจ โดยใช้สูตรธรรมดาทั่วไปมาเก็บค่าไว้ใน Sheet Macro ที่นี้ผมอยากกำหนดเงื่อนไข Macro ถ้าหาก Sheet Macro ตรงช่องชื่อวิทยากรหากไม่มีชื่อวิทยากร ไม่ต้องคัดลอกข้อมูลที่ไม่ชื่อวิทยากร ส่งไปยัง Sheet สรุปการประเมินคะแนนข้อมูล ต้องเขียน Vba เพิ่มยังไงมั้งครับ

ผมได้ทำปุ่ม ส่งข้อมูล ไว้ที่หน้าตารางสรุปประเมินความพึงพอใจ มีรายละเอียด Code ดังนี้

Module 2

Code: Select all

Sub Macro_Copy()
MsgBox ("ส่งข้อมูลสำเร็จ")
'
' Macro_Copy Macro
'

'
    Range("MacroCopy").Copy
    Sheets("สรุปการประเมินคะแนน").Select
    Range("B65535").Select
    Selection.End(xlUp).Offset(1, 0).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Sheets("ตารางสรุปประเมินความพึงพอใจ").Select
End Sub
หลังจากกดปุมค่าจะถูกส่งไปต่อท้ายตารางที่มีข้อมูลใน Sheet สรุปการประเมินคะแนน ขอบคุณครับ
ปรับปรุง_ตารางสถิติความพึงพอใจวิทยากร.xlsm

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Sun Sep 11, 2016 12:00 am
by snasui
:D ตัวอย่างการปรับ Code ครับ

Code: Select all

Sub Macro_Copy()
MsgBox ("ส่งข้อมูลสำเร็จ")
'
' Macro_Copy Macro
'

'
    Dim icount As Long
    icount = Application.CountIf(Sheets("Macro").Range("b7:b65536"), ">0")
    Range("MacroCopy").Resize(icount).Copy
    Sheets("สรุปการประเมินคะแนน").Select
    Range("B65535").Select
    Selection.End(xlUp).Offset(1, 0).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Sheets("ตารางสรุปประเมินความพึงพอใจ").Select
End Sub

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Sun Sep 11, 2016 12:43 am
by primeval
snasui wrote::D ตัวอย่างการปรับ Code ครับ

Code: Select all

Sub Macro_Copy()
MsgBox ("ส่งข้อมูลสำเร็จ")
'
' Macro_Copy Macro
'

'
    Dim icount As Long
    icount = Application.CountIf(Sheets("Macro").Range("b7:b65536"), ">0")
    Range("MacroCopy").Resize(icount).Copy
    Sheets("สรุปการประเมินคะแนน").Select
    Range("B65535").Select
    Selection.End(xlUp).Offset(1, 0).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Sheets("ตารางสรุปประเมินความพึงพอใจ").Select
End Sub
ขออนุญาตถามต่ออีกนิดนึงครับ ในตาราง Sheet สรุปการประเมินคะแนน เวลาเราลบข้อมูลออกไปหมด พอเราส่งค่าไป ข้อมูลมันอยู่นอก Table พอผมลองปรับ

Code: Select all

Selection.End(xlUp).Offset(0, 0).Select
เปลี่ยนจาก 1 เป็น 0 สามารถเข้าไปอยู่ใน Table ได้แต่ปรากฏว่า พอส่งข้อมูลไปใหม่มันทับข้อมูลเก่าครับ

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Sun Sep 11, 2016 6:16 am
by snasui
:D ตัวอย่างการปรับ Code ครับ

Code: Select all

'Other code
    Sheets("สรุปการประเมินคะแนน").Select
    Range("B65535").Select
    Selection.End(xlUp).Select
    With Selection
        If .Value <> "" Then
            .Offset(1, 0).Select
        End If
    End With
'Other code
'Other code คือ Code เดิมที่ไม่ต้องปรับครับ

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Sun Sep 11, 2016 7:43 am
by primeval
snasui wrote::D ตัวอย่างการปรับ Code ครับ

Code: Select all

'Other code
    Sheets("สรุปการประเมินคะแนน").Select
    Range("B65535").Select
    Selection.End(xlUp).Select
    With Selection
        If .Value <> "" Then
            .Offset(1, 0).Select
        End If
    End With
'Other code
'Other code คือ Code เดิมที่ไม่ต้องปรับครับ
Range("MacroCopy").Resize(icount).Copy

ติด Debug ครับ

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Sun Sep 11, 2016 7:48 am
by snasui
:D แนบไฟล์ที่ติดปัญหาพร้อม Code ล่าสุดที่ได้ปรับปรุงมาเองแล้วและต้องทำเช่นนี้เสมอ จะได้ช่วยตรวจสอบให้ได้ครับ

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Sun Sep 11, 2016 7:50 am
by primeval
primeval wrote:
snasui wrote::D ตัวอย่างการปรับ Code ครับ

Code: Select all

'Other code
    Sheets("สรุปการประเมินคะแนน").Select
    Range("B65535").Select
    Selection.End(xlUp).Select
    With Selection
        If .Value <> "" Then
            .Offset(1, 0).Select
        End If
    End With
'Other code
'Other code คือ Code เดิมที่ไม่ต้องปรับครับ
Range("MacroCopy").Resize(icount).Copy

ติด Debug ครับ
อ้อได้แล้วครับ ลืมใส่ข้อมูลก็เลยติด Debug ได้แล้วครับขอบคุณครับ

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Sun Sep 18, 2016 8:17 pm
by primeval
พอดีผมไปเห็นวิธี save file pdf โดยไม่ต้องระบุตำแหน่งที่ตั้ง File จากเว็บนี้ครับ
http://www.contextures.com/excelvbapdf.html จะเอามาปรับใช้แล้วพบปัญหาดังนี้ครับ

1.ตัวข้อมูลชื่อวิทยากร ตารางสรุปประเมินความพึงพอใจ สามารถ save ออก PDF ได้ตามจำนวนชื่อวิทยากร แต่ข้อมูลออกมาแค่ชื่อคนเดี่ยว คนแรก ตัวอย่าง File ครับ
Sheet1_25590918_2017.pdf
รบกวนขอวิธีให้สามารถออกมาทั้งหมดครับ
2.จากข้อ 1 ข้อมูล PDF ยังไม่ได้มีการปรับ Format กระดาษ

Code: Select all

ActiveSheet.VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1
โด้ดตัวนี้มาจากการบันทึก Macro ไม่ทราบว่าจะเอาเพิ่มไว้ตรงไหน ทดลองแทรกอยู่หลายครั้งแต่ติด Error ครับ จะได้ช่วยปรับหน้ากระดาษ PDF ให้ ตารางหรือตัวอักษรที่ตกไปอยู่หน้าอื่น ดันขึ้นมาอยู่ในหน้าเดี่ยวกันครับ

จากตัวอย่างครับ Sheet ตารางสรุปประเมินความพึงพอใจ ชื่อปุ่ม Form_PDF Module 6 Code ดังกล่าว

Code: Select all

Sub Button10_PDF()
    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
    Dim allV As Range
    Dim r As Range
    Dim ns As Worksheet
    Dim s As Range
    Dim myValue As Variant
    Set ns = Sheets.Add(After:=ActiveSheet)
    Set s = Sheets("สรุปวิทยากรตามหลักสูตร").Range("b7")
    With Sheets("ตารางสรุปประเมินความพึงพอใจ")
        Set allV = .Range("c11", .Range("c" & .Rows.Count).End(xlUp))
        
        
    End With
    
    For Each r In allV
        s.Value = r.Value
        Sheets("สรุปวิทยากรตามหลักสูตร").UsedRange.Copy
        With ns.Range("c" & Rows.Count).End(xlUp).Offset(1, -2)
            .PasteSpecial Paste:=xlPasteValues
            .PasteSpecial Paste:=xlPasteFormats
            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 savng file
strFile = strName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile

'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strPathFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")

'export to PDF if a folder was selected
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile

       End If

        End With
        Application.CutCopyMode = False
        
       


        
        

        
        
        
    Next r
exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
    
    Sheets("ตารางสรุปประเมินความพึงพอใจ").Select
End Sub
ปรับปรุง_ตารางสถิติความพึงพอใจวิทยากร.xlsm
รบกวนด้วยครับขอบคุณครับ

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Sun Sep 18, 2016 8:19 pm
by snasui
:D ตัวอย่างการปรับ Code ครับ

Code: Select all

            'Other code
            'create default name for savng file
            strFile = strName & "_" & strTime & ".pdf"
            strPathFile = strPath & strFile
            
            With ActiveSheet.PageSetup
                .FitToPagesWide = 1
                .FitToPagesTall = False
            End With
            'Other code
สำหรับ 'Other code คือ Code เดิมครับ

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Sun Sep 18, 2016 8:41 pm
by primeval
snasui wrote::D ตัวอย่างการปรับ Code ครับ

Code: Select all

            'Other code
            'create default name for savng file
            strFile = strName & "_" & strTime & ".pdf"
            strPathFile = strPath & strFile
            
            With ActiveSheet.PageSetup
                .FitToPagesWide = 1
                .FitToPagesTall = False
            End With
            'Other code
สำหรับ 'Other code คือ Code เดิมครับ
Code เดิมผมเห็น Copy ต่อลงมาครับ แต่ทีนี้ ก่อน Copy ลงมาผมอยากให้ Save ออก PDF ที่ละคนครับ แล้วก็ปรับ Page Break Preview ให้พอดีหน้าครับ

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Sun Sep 18, 2016 11:06 pm
by snasui
:D ลองปรับ Code มาเองก่อนตามลำดับที่ต้องการ ติดตรงไหนค่อยนำมาถามกันต่อ

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

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Fri Sep 23, 2016 2:15 pm
by primeval

Code: Select all

Sub Button9_PDF()

 Dim ws As Worksheet
    Dim newSheetName As String


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
    Dim allV As Range
    Dim r As Range
    Dim ns As Worksheet
    Dim s As Range
    Dim myValue As Variant
    Set ns = Sheets.Add(After:=ActiveSheet)
    Set s = Sheets("สรุปวิทยากรตามหลักสูตร").Range("b7")
    With Sheets("ตารางสรุปประเมินความพึงพอใจ")
        Set allV = .Range("c11", .Range("c" & .Rows.Count).End(xlUp))
        
        
End With

Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")
For Each r In allV
        s.Value = r.Value
        Sheets("สรุปวิทยากรตามหลักสูตร").UsedRange.Copy
        With ns.Range("c" & Rows.Count).End(xlUp).Offset(1, -2)
        
            .PasteSpecial Paste:=xlPasteValues
            .PasteSpecial Paste:=xlPasteFormats
ActiveSheet.PageSetup.PrintArea = Sheets("สรุปวิทยากรตามหลักสูตร").UsedRange.Copy

'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
newSheetName = Sheets("สรุปวิทยากรตามหลักสูตร").Range("b7")
'strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")



'create default name for savng file
strFile = newSheetName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile

With ActiveSheet.PageSetup
                .FitToPagesWide = 1
                .FitToPagesTall = False
            End With


'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strPathFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")

'export to PDF if a folder was selected
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile

       End If

        
        End With
Application.CutCopyMode = False

       


        
        

        
        
        
    Next r
exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
    
    Sheets("ตารางสรุปประเมินความพึงพอใจ").Select
End Sub
ลองปรับดูแล้วไม่สามารถใช้ PageBreak ได้ครับ ผมลองไล่ที่ละบรรทัดตามที่อาจารย์บอกแล้ว แต่ข้อมูลที่ออก PDF ออกแค่ชื่อแรกครับ
ปรับปรุง_ตารางสถิติความพึงพอใจวิทยากร.xlsm

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Fri Sep 23, 2016 7:43 pm
by snasui
:D ที่ปรับมายังไม่พบว่ามีการย้ายตำแหน่ง Code ที่ผมเขียนไว้ในโพสต์ก่อนหน้าแต่อย่างใดครับ

ตัวอย่างการวางตำแหน่ง Code ครับ

Code: Select all

'Other code
For Each r In allV
    s.Value = r.Value
    Sheets("สรุปวิทยากรตามหลักสูตร").UsedRange.Copy
    With ns.Range("c" & Rows.Count).End(xlUp).Offset(1, -2)
    
        .PasteSpecial Paste:=xlPasteValues
        .PasteSpecial Paste:=xlPasteFormats
        
        With ActiveSheet.PageSetup
            .PrintArea = Selection.Address
            .FitToPagesWide = 1
        End With
'Other code

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Fri Sep 23, 2016 9:37 pm
by primeval
snasui wrote::D ที่ปรับมายังไม่พบว่ามีการย้ายตำแหน่ง Code ที่ผมเขียนไว้ในโพสต์ก่อนหน้าแต่อย่างใดครับ

ตัวอย่างการวางตำแหน่ง Code ครับ

Code: Select all

'Other code
For Each r In allV
    s.Value = r.Value
    Sheets("สรุปวิทยากรตามหลักสูตร").UsedRange.Copy
    With ns.Range("c" & Rows.Count).End(xlUp).Offset(1, -2)
    
        .PasteSpecial Paste:=xlPasteValues
        .PasteSpecial Paste:=xlPasteFormats
        
        With ActiveSheet.PageSetup
            .PrintArea = Selection.Address
            .FitToPagesWide = 1
        End With
'Other code
อยู่ใน Module6 ครับ ฃื่อปุ่ม Sub Button9_PDF() ครับ

Code: Select all

'create default name for savng file
strFile = newSheetName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile

With ActiveSheet.PageSetup
                .FitToPagesWide = 1
                .FitToPagesTall = False
            End With
แต่ Code ล่าสุดได้ผลกว่าครับออก PDF ทีละรายการแล้วครับ แต่ FitPage ไม่ได้ครับ

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Fri Sep 23, 2016 9:40 pm
by snasui
:D ช่วยอธิบายคำว่า FitPage ไม่ได้มาอย่างละเอียด หรือแนบไฟล์ที่ถูกต้องตามที่ต้องการมาด้วย

ในเครื่องผมสามารถ Run ได้ตามไฟล์ PDF ที่แนบมาตาม Link นี้ครับ download/file.php?id=21181

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Fri Sep 23, 2016 9:54 pm
by primeval
snasui wrote::D ช่วยอธิบายคำว่า FitPage ไม่ได้มาอย่างละเอียด หรือแนบไฟล์ที่ถูกต้องตามที่ต้องการมาด้วย

ในเครื่องผมสามารถ Run ได้ตามไฟล์ PDF ที่แนบมาตาม Link นี้ครับ download/file.php?id=21181
จาก File ตัวอย่าง ข้อมูลทั้งหมดจะต้องอยู่ในหน้ากระดาษเดี่ยวกันไม่ตกลงไปหน้าอื่นครับ เหมือนกับเวลาเรา Print Excel แล้วเลยหน้า Limit กระดาษ 1 หน้า ผมก็ต้องไปที่ View > Page Break Preview เพื่อเลื่อนให้ข้อมูลให้อยู่ในแค่1หน้ากระดาษหรือตามที่เราต้องการครับ
แต่เมื่อสักครู่ผมขออนุญาตลองเพิ่ม Code จากหน้าดูครับ

อ้างอิงจากเว็บนี้ครับ
http://www.mrexcel.com/forum/excel-ques ... 1-1-a.html

Code: Select all

With ActiveSheet.PageSetup
            .PrintArea = Selection.Address
            .PrintTitleRows = "$42:$42"
            .PrintTitleColumns = "$N:$N"
            .Orientation = xlPortrait
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = 1
        End With
ผลลัพธ์ออกมาดังไฟล์นี้ครับ
คุณดุษฎี นามสกุล04_25590923_2146.pdf
ผมยังไม่ค่อยเข้าใจ Code เท่าไรครับ อย่าง

Code: Select all

Zoom = False
นี้หมายถึงอะไรครับ

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Fri Sep 23, 2016 10:12 pm
by snasui
:D ควรแนบผลลัพธ์ที่ต้องการหรืออธิบายมาเช่นนี้ตั้งแต่แรกจะได้เข้าใจตรงกัน

ไฟล์แนบที่โพสต์มาเป็นการจัดให้ "พอดี 1 หน้า" Code สำหรับการจัดให้พอดีกับความสูงเพียง 1 หน้าคือ .FitToPagesTall = 1 โดยนำไปต่อจาก Code เดิมที่ผมตอบไปแล้วครับ

.Zoom=False เป็นการกำหนดค่าการ Zoom ให้เป็นค่า False หรือไม่ต้อง Zoom นั่นเอง บรรทัดนี้ไม่จำเป็นต้องใส่เข้าไปก็ย่อมได้หากไม่กำหนดไปเป็นอย่างอื่นครับ

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Fri Sep 23, 2016 10:16 pm
by primeval
snasui wrote::D ควรแนบผลลัพธ์ที่ต้องการหรืออธิบายมาเช่นนี้ตั้งแต่แรกจะได้เข้าใจตรงกัน

ไฟล์แนบที่โพสต์มาเป็นการจัดให้ "พอดี 1 หน้า" Code สำหรับการจัดให้พอดีกับความสูงเพียง 1 หน้าคือ .FitToPagesTall = 1 โดยนำไปต่อจาก Code เดิมที่ผมตอบไปแล้วครับ

.Zoom=False เป็นการกำหนดค่าการ Zoom ให้เป็นค่า False หรือไม่ต้อง Zoom นั่นเอง บรรทัดนี้ไม่จำเป็นต้องใส่เข้าไปก็ย่อมได้หากไม่กำหนดไปเป็นอย่างอื่นครับ
คำนี้แหละครับผมนึกไม่ออกเข้าเรียกอะไร ก็เลยหาคำอื่นที่ตัวเองเข้าใจมาอธิบายครับ ขอบคุณครับ ไม่ได้ Sansui Excel ผมไม่สามารถทำได้แน่ๆครับ

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Tue Nov 01, 2016 1:28 pm
by primeval
ขออนุญาตสอบถาม เพิ่มเติมครับ จาก Sheet สรุปวิทยากรตามหลักสูตร ช่อง Cell D31 ถึง D40 ได้มีการ Merge Cell ไว้เพื่อให้สามารถแสดงข้อมูล Sheet ตารางสรุปประเมินความพึงพอใจได้ ที่นี้ผมอยากให้ช่อง D31 ถึง D40 สามารถขยายช่องความสูงอัติโนมัติของ Cell เมื่อมีการพิมพ์ข้อความยาวๆ ได้ไหมครับ

Code: Select all

Sub Button9_PDF()
 Dim ws As Worksheet
    Dim newSheetName As String
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
    Dim allV As Range
    Dim r As Range
    Dim ns As Worksheet
    Dim s As Range
    Dim myValue As Variant
    Set ns = Sheets.Add(After:=ActiveSheet)
    Set s = Sheets("สรุปวิทยากรตามหลักสูตร").Range("b7")
    With Sheets("ตารางสรุปประเมินความพึงพอใจ")
        Set allV = .Range("c11", .Range("c" & .Rows.Count).End(xlUp))
        myValue = InputBox("Enter Sheet Name")
        ActiveSheet.Name = myValue
End With
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")
For Each r In allV
        s.Value = r.Value
        Sheets("สรุปวิทยากรตามหลักสูตร").UsedRange.Copy
        With ns.Range("c" & Rows.Count).End(xlUp).Offset(1, -2)
            .PasteSpecial Paste:=xlPasteValues
            .PasteSpecial Paste:=xlPasteFormats
            
    Dim j As Long
    Dim n As Long
    Dim i As Long
    Dim MW As Double 'merge width
    Dim RH As Double 'row height
    Dim MaxRH As Double
    Dim rngMArea As Range
    Dim rng As Range
     
    Const SpareCol  As Long = 26
    Set rng = Range("C10:O" & Range("C" & Rows.Count).End(xlUp).Row)
    
    With rng
        For j = 1 To .Rows.Count
             'if the row is not hidden
            If Not .Parent.Rows(.Cells(j, 1).Row).Hidden Then
                 'if the cells have data
                If Application.WorksheetFunction.CountA(.Rows(j)) Then
                    MaxRH = 0
                    For n = .Columns.Count To 1 Step -1
                        If Len(.Cells(j, n).Value) Then
                             'mergecells
                            If .Cells(j, n).MergeCells Then
                                Set rngMArea = .Cells(j, n).MergeArea
                                With rngMArea
                                    MW = 0
                                    If .WrapText Then
                                         'get the total width
                                        For i = 1 To .Cells.Count
                                            MW = MW + .Columns(i).ColumnWidth
                                        Next
                                        MW = MW + .Cells.Count * 0.66
                                         'use the spare column and put the value, make autofit, get the row height
                                        With .Parent.Cells(.Row, SpareCol)
                                            .Value = rngMArea.Value
                                            .ColumnWidth = MW
                                            .WrapText = True
                                            .EntireRow.AutoFit
                                            RH = .RowHeight
                                            MaxRH = Application.Max(RH, MaxRH)
                                            .Value = vbNullString
                                            .WrapText = False
                                            .ColumnWidth = 8.43
                                        End With
                                        .RowHeight = MaxRH
                                    End If
                                End With
                            ElseIf .Cells(j, n).WrapText Then
                                RH = .Cells(j, n).RowHeight
                                .Cells(j, n).EntireRow.AutoFit
                                If .Cells(j, n).RowHeight < RH Then .Cells(j, n).RowHeight = RH
                            End If
                        End If
                    Next
                End If
            End If
        Next
        .Parent.Parent.Worksheets(.Parent.Name).UsedRange
    End With
            
            
    Columns("A:A").ColumnWidth = 13
    Columns("E:E").ColumnWidth = 24
    Columns("N:N").ColumnWidth = 10
With ActiveSheet.PageSetup
            .PrintArea = Selection.Address
            .Orientation = xlPortrait
            .Zoom = 55
            .FitToPagesWide = 1
            .FitToPagesTall = 1
        End With
'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
newSheetName = Sheets("สรุปวิทยากรตามหลักสูตร").Range("b7")
'strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")
'create default name for savng file
strFile = newSheetName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile
'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strPathFile, _
        filefilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")
'export to PDF if a folder was selected
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
       End If
        End With
Application.CutCopyMode = False
    Next r
exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
    Sheets("ตารางสรุปประเมินความพึงพอใจ").Select
End Sub
ซึ่งจาก Code ที่ผมเพิ่มเข้าไป ช่อง Cell ยังมีปัญหาครับ
ตัวอย่างที่มีปัญหา
คุณธิดา ธัญ_25591101_1306.pdf
ตัวอย่างที่ไม่มีปัญหา
คุณแอฟ จุดจุด01_25591101_1306.pdf
File Excel ครับ
ปรับปรุง_ตารางสถิติความพึงพอใจวิทยากร.xlsm

Re: สอบถามการ Copy ต่อท้ายตารางแบบมีเงื่อนไข

Posted: Tue Nov 01, 2016 7:58 pm
by snasui
:D วิธีการแก้ไขแบบง่ายโดยไม่ต้องเขียน Code คือให้ไปยังคอลัมน์ใด ๆ เช่นคอลัมน์ Z เพื่อใช้เป็นคอลัมน์ช่วย

จากนั้นเซลล์ Z32 คีย์สูตร

=C32

Enter > Copy ไปด้านล่าง

จากนั้นเพิ่มความกว้างให้กับคอลัมน์ Z ให้เท่า ๆ กับคอลัมน์ C:N และจัดรูปแบบเป็น Wrap Text ครับ