Page 2 of 2

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

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

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

=C32

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

จากนั้นเพิ่มความกว้างให้กับคอลัมน์ Z ให้เท่า ๆ กับคอลัมน์ C:N และจัดรูปแบบเป็น Wrap Text ครับ
ถ้างั้นเวลาผมรัน macro ก็ไม่สามารถจัดข้อมูลอื่นให้สามารถขยายออกได้อัตโนมัติ เพราะว่าจาก Code ที่พี่ให้ผมมาเรื่อยๆ จะกดเพียงปุ่มเดี่ยวแล้วข้อมูลก็จะต่อต่อกันครับ

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

Posted: Tue Nov 01, 2016 8:38 pm
by snasui
:D ด้วย Code แล้วไม่มีอะไรที่ทำไม่ได้ครับ

การต่อข้อมูลก็สามารถหาได้ว่าบรรทัดใดที่เราจะขยายความสูง ไม่ทราบว่ามีความกังวลตรงไหน อย่างไรครับ

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

Posted: Tue Nov 01, 2016 8:53 pm
by primeval147
ถ้าดูข้อมูลในช่องเสนอความเห็น เช่น sheet NOOB
ข้อ 1-3 ข้อความสั้น
แต่พอข้อ 4 มันยาว ผมอยากให้เวลาผมกดปุ่ม macro แล้วสามารถปรับขนาดช่องตามความยาวของข้อมูลได้ครับ

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

Posted: Tue Nov 01, 2016 9:03 pm
by snasui
:D ให้ทำตามที่ผมแจ้งไปแล้ว ทำแล้วติดตรงไหนค่อยมาถามกันต่อครับ

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

Posted: Tue Nov 01, 2016 9:46 pm
by primeval147
primeval wrote:ขออนุญาตสอบถาม เพิ่มเติมครับ จาก 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
ใน Code ตัวนี้ที่ผมแนบมา

Other Code

Code: Select all

    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
Other Code

จาก Code ตัวนี้สามารทำให้ Form สรุปวิทยากรตามหลักสูตร สามารถ AutoFit Row Height ขณะทำการ Merge Cell ได้
โดยเฉพาะข้อ 4
ความเป็นกันเองของวิทยากรชอบพูดเฮ้ย ต้องหัดพูด ร,ล. เสียใหม่ ไม่ชอบพูดกิน-ให้พูดรับประทาน บอกระดับการศึกษา ไม่ดูถูกคนฟังควรเดินมาเชื้อเชิญพูดเพราะๆดีกว่า เราไม่ใช่เด็ก เธอไปสอนที่อื่นที่ระดับต่ำกว่าคนที่นี่ อย่าเอามาใช้ที่นี่ หัดคน STAFF ของตนเองด้วย
จะเห็นว่าความสูงขยายออกแต่ก็ยังแสดงข้อความตกหล่นบางส่วน ผมอยากรู้ว่า Code ดังกล่าวตัวนี้พอมีวิธีปรับให้กว้างกว่านี้ไหมครับ

ส่วนของที่อาจารย์แนะนำมา ไม่สามารถนำไปรับใช่ได้ครับ เพราะว่ามีการ Merge Cell เอาไว้เท่าที่ผมศึกษาโดยไม่รู้อะไรการ Merge Cell แล้ว Warp Text ลงมาแล้วกดคำสั่ง Auto Fit Height ไม่สามารถทำได้ครับ อยากจะให้อาจารย์ช่วยรบกวนตรวจสอบ Code ให้หน่อยครับ

ผมกลัวว่าถามไปตรงๆ เกรงว่าอาจารย์จะตำหนิว่า ผมไม่ศึกษาอะไรมาก่อนเลยครับ

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

Posted: Tue Nov 01, 2016 9:52 pm
by snasui
:lol: ผมตอบไปแล้วและดูเหมือนว่าจะไม่เข้าใจและปรับใช้ไม่เป็น

ตัวอย่างการปรับ Code คือด้านล่าง ใช้คอลัมน์ Z เป็นตัวช่วยในการปรับความสูงให้พอดีข้อความครับ

Code: Select all

'Other code
With Sheets("µÒÃÒ§ÊÃØ»»ÃÐàÁÔ¹¤ÇÒÁ¾Ö§¾Íã¨")
    Set allV = .Range("c11", .Range("c" & .Rows.Count).End(xlUp))
    myValue = InputBox("Enter Sheet Name")
    ActiveSheet.Name = myValue
    ActiveSheet.Range("z2:z10000").Formula = "=D2"
    ActiveSheet.Range("z2:z10000").WrapText = True
    ActiveSheet.Range("z1").EntireColumn.ColumnWidth = 106
    ActiveSheet.Range("z2:z10000").Font.Name = "FreesiaUPC"
    ActiveSheet.Range("z2:z10000").Font.Size = 16
End With
'Other code

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

Posted: Tue Nov 01, 2016 10:21 pm
by primeval147
snasui wrote::lol: ผมตอบไปแล้วและดูเหมือนว่าจะไม่เข้าใจและปรับใช้ไม่เป็น

ตัวอย่างการปรับ Code คือด้านล่าง ใช้คอลัมน์ Z เป็นตัวช่วยในการปรับความสูงให้พอดีข้อความครับ

Code: Select all

'Other code
With Sheets("µÒÃÒ§ÊÃØ»»ÃÐàÁÔ¹¤ÇÒÁ¾Ö§¾Íã¨")
    Set allV = .Range("c11", .Range("c" & .Rows.Count).End(xlUp))
    myValue = InputBox("Enter Sheet Name")
    ActiveSheet.Name = myValue
    ActiveSheet.Range("z2:z10000").Formula = "=D2"
    ActiveSheet.Range("z2:z10000").WrapText = True
    ActiveSheet.Range("z1").EntireColumn.ColumnWidth = 106
    ActiveSheet.Range("z2:z10000").Font.Name = "FreesiaUPC"
    ActiveSheet.Range("z2:z10000").Font.Size = 16
End With
'Other code

ปรับแล้วครับ
ปรับปรุง_ตารางสถิติความพึงพอใจวิทยากร.xlsm

ยัง warp text ไม่ได้

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

Posted: Tue Nov 01, 2016 10:35 pm
by snasui
:D ผมให้ Code ไปอย่างไรขอให้ใช้ตามนั้นครับ ที่นำไปใช้นั้นไม่ตรงกับที่ผมให้ไปครับ

คอลัมน์ Z ในชีต สรุปวิทยากรตามหลักสูตร ไม่จำเป็นต้องมีครับ

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

Posted: Tue Nov 01, 2016 11:07 pm
by primeval147
snasui wrote::D ผมให้ Code ไปอย่างไรขอให้ใช้ตามนั้นครับ ที่นำไปใช้นั้นไม่ตรงกับที่ผมให้ไปครับ

คอลัมน์ Z ในชีต สรุปวิทยากรตามหลักสูตร ไม่จำเป็นต้องมีครับ

แก้ให้ตรงตามที่อาจารย์ให้แล้วครับ
ปรับปรุง_ตารางสถิติความพึงพอใจวิทยากร.xlsm
แต่ข้อความยังแสดงไม่หมดครับ
คุณธิดา ธัญ_20161101_2304.pdf

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

Posted: Tue Nov 01, 2016 11:15 pm
by snasui
:D เนื่องจาก UsedRange ในชีต สรุปวิทยากรตามหลักสูตร มีเกินไปจากข้อมูลที่มี เมื่อ Copy มาใช้จึงทับสูตรที่ให้ไว้

ให้ปรับ Code เฉพาะบรรทัดนี้เสียใหม่ครับ Sheets("สรุปวิทยากรตามหลักสูตร").UsedRange.Copy เป็น Sheets("สรุปวิทยากรตามหลักสูตร").Range("a1:n40").Copy เป็นต้น

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

Posted: Tue Nov 01, 2016 11:21 pm
by primeval147
snasui wrote::D เนื่องจาก UsedRange ในชีต สรุปวิทยากรตามหลักสูตร มีเกินไปจากข้อมูลที่มี เมื่อ Copy มาใช้จึงทับสูตรที่ให้ไว้

ให้ปรับ Code เฉพาะบรรทัดนี้เสียใหม่ครับ Sheets("สรุปวิทยากรตามหลักสูตร").UsedRange.Copy เป็น Sheets("สรุปวิทยากรตามหลักสูตร").Range("a1:n40").Copy เป็นต้น
ขอบคุณมากๆครับ อาจารย์