Page 1 of 1

อยากทราบว่ามีวิธีเขียนที่ง่ายกว่านี้หรือเปล่าครับ

Posted: Thu May 14, 2020 1:14 am
by Singhamanuss
Sub SchedulingTable2()
With Sheets("Mon")
.Range("J2").Cells.Value = T1.Range("I9") & " " & T1.Range("I10")
.Range("J3").Cells.Value = T2.Range("I9") & " " & T2.Range("I10")
.Range("J4").Cells.Value = T3.Range("I9") & " " & T3.Range("I10")
.Range("J5").Cells.Value = T4.Range("I9") & " " & T4.Range("I10")
.Range("J6").Cells.Value = T5.Range("I9") & " " & T5.Range("I10")
.Range("J7").Cells.Value = T6.Range("I9") & " " & T6.Range("I10")
.Range("J8").Cells.Value = T7.Range("I9") & " " & T7.Range("I10")
.Range("J9").Cells.Value = T8.Range("I9") & " " & T8.Range("I10")
.Range("J10").Cells.Value = T9.Range("I9") & " " & T9.Range("I10")
.Range("J11").Cells.Value = T10.Range("I9") & " " & T10.Range("I10")
.Range("J12").Cells.Value = T11.Range("I9") & " " & T11.Range("I10")
.Range("J13").Cells.Value = T12.Range("I9") & " " & T12.Range("I10")
.Range("J14").Cells.Value = T13.Range("I9") & " " & T13.Range("I10")
.Range("J15").Cells.Value = T14.Range("I9") & " " & T14.Range("I10")
.Range("J16").Cells.Value = T15.Range("I9") & " " & T15.Range("I10")
.Range("J17").Cells.Value = T16.Range("I9") & " " & T16.Range("I10")
.Range("J18").Cells.Value = T17.Range("I9") & " " & T17.Range("I10")
.Range("J19").Cells.Value = T18.Range("I9") & " " & T18.Range("I10")
.Range("J20").Cells.Value = T19.Range("I9") & " " & T19.Range("I10")
.Range("J21").Cells.Value = T20.Range("I9") & " " & T20.Range("I10")
.Range("J22").Cells.Value = T21.Range("I9") & " " & T21.Range("I10")
.Range("J23").Cells.Value = T22.Range("I9") & " " & T22.Range("I10")
.Range("J24").Cells.Value = T23.Range("I9") & " " & T23.Range("I10")
.Range("J25").Cells.Value = T24.Range("I9") & " " & T24.Range("I10")
.Range("J26").Cells.Value = T25.Range("I9") & " " & T25.Range("I10")
.Range("J27").Cells.Value = T26.Range("I9") & " " & T26.Range("I10")
.Range("J28").Cells.Value = T27.Range("I9") & " " & T27.Range("I10")
.Range("J29").Cells.Value = T28.Range("I9") & " " & T28.Range("I10")
End With

MsgBox "·Ó¡ÒÃàÃÕ¡¢éÍÁÙŵÒÃÒ§Ê͹àÃÕºÃéÍÂáÅéÇ", vbInformation, "àÊÃç¨ÊÔé¹"


End Sub

Re: อยากทราบว่ามีวิธีเขียนที่ง่ายกว่านี้หรือเปล่าครับ

Posted: Thu May 14, 2020 1:21 am
by Singhamanuss

Code: Select all

Sub SchedulingTable2()
With Sheets("Mon")
.Range("J2").Cells.Value = T1.Range("I9") & " " & T1.Range("I10")
.Range("J3").Cells.Value = T2.Range("I9") & " " & T2.Range("I10")
.Range("J4").Cells.Value = T3.Range("I9") & " " & T3.Range("I10")
.Range("J5").Cells.Value = T4.Range("I9") & " " & T4.Range("I10")
.Range("J6").Cells.Value = T5.Range("I9") & " " & T5.Range("I10")
.Range("J7").Cells.Value = T6.Range("I9") & " " & T6.Range("I10")
.Range("J8").Cells.Value = T7.Range("I9") & " " & T7.Range("I10")
.Range("J9").Cells.Value = T8.Range("I9") & " " & T8.Range("I10")
.Range("J10").Cells.Value = T9.Range("I9") & " " & T9.Range("I10")
.Range("J11").Cells.Value = T10.Range("I9") & " " & T10.Range("I10")
.Range("J12").Cells.Value = T11.Range("I9") & " " & T11.Range("I10")
.Range("J13").Cells.Value = T12.Range("I9") & " " & T12.Range("I10")
.Range("J14").Cells.Value = T13.Range("I9") & " " & T13.Range("I10")
.Range("J15").Cells.Value = T14.Range("I9") & " " & T14.Range("I10")
.Range("J16").Cells.Value = T15.Range("I9") & " " & T15.Range("I10")
.Range("J17").Cells.Value = T16.Range("I9") & " " & T16.Range("I10")
.Range("J18").Cells.Value = T17.Range("I9") & " " & T17.Range("I10")
.Range("J19").Cells.Value = T18.Range("I9") & " " & T18.Range("I10")
.Range("J20").Cells.Value = T19.Range("I9") & " " & T19.Range("I10")
.Range("J21").Cells.Value = T20.Range("I9") & " " & T20.Range("I10")
.Range("J22").Cells.Value = T21.Range("I9") & " " & T21.Range("I10")
.Range("J23").Cells.Value = T22.Range("I9") & " " & T22.Range("I10")
.Range("J24").Cells.Value = T23.Range("I9") & " " & T23.Range("I10")
.Range("J25").Cells.Value = T24.Range("I9") & " " & T24.Range("I10")
.Range("J26").Cells.Value = T25.Range("I9") & " " & T25.Range("I10")
.Range("J27").Cells.Value = T26.Range("I9") & " " & T26.Range("I10")
.Range("J28").Cells.Value = T27.Range("I9") & " " & T27.Range("I10")
.Range("J29").Cells.Value = T28.Range("I9") & " " & T28.Range("I10")
End With

MsgBox "·Ó¡ÒÃàÃÕ¡¢éÍÁÙŵÒÃÒ§Ê͹àÃÕºÃéÍÂáÅéÇ", vbInformation, "àÊÃç¨ÊÔé¹"


End Sub

Re: อยากทราบว่ามีวิธีเขียนที่ง่ายกว่านี้หรือเปล่าครับ

Posted: Thu May 14, 2020 8:07 am
by snasui
:D กรุณาแนบไฟล์ตัวอย่างมาด้วยจะได้สะดวกในการตอบครับ

Re: อยากทราบว่ามีวิธีเขียนที่ง่ายกว่านี้หรือเปล่าครับ

Posted: Thu May 14, 2020 11:00 pm
by Singhamanuss
ครับ ไฟล์ครับ
ครูวิทย์ - Copy.xlsm

Re: อยากทราบว่ามีวิธีเขียนที่ง่ายกว่านี้หรือเปล่าครับ

Posted: Fri May 15, 2020 12:52 am
by Bo_ry
แบบนี้ไหม

Code: Select all

Sub SchedulingTable()
Dim lr As Long
With Sheets("Mon")
        For lr = 1 To 28
            .Range("B" & lr + 1).Cells.Value = Sheets(lr + 10).Range("I7")
        Next
End With

MsgBox "·Ó¡ÒÃàÃÕ¡¢éÍÁÙŵÒÃÒ§Ê͹àÃÕºÃéÍÂáÅéÇ", vbInformation, "àÊÃç¨ÊÔé¹"

End Sub

Re: อยากทราบว่ามีวิธีเขียนที่ง่ายกว่านี้หรือเปล่าครับ

Posted: Fri May 15, 2020 7:48 am
by snasui
:D อีกตัวอย่าง Code ที่รองรับการใช้ CodeName และยืดหยุ่นต่อการเรียงชีตสลับกันไปมาครับ

Code: Select all

Dim sh As Worksheet
With Sheets("Mon")
    For Each sh In Worksheets
        If VBA.Left(sh.CodeName, 1) = "T" Then
            .Range("j1").Offset(VBA.Replace(sh.CodeName, "T", ""), 0).Value = _
                sh.Range("i9").Value & " " & sh.Range("i10").Value
        End If
    Next sh
End With

Re: อยากทราบว่ามีวิธีเขียนที่ง่ายกว่านี้หรือเปล่าครับ

Posted: Fri May 15, 2020 4:35 pm
by Singhamanuss
ขอคุณครับ ทำงานง่ายขึ้นมากเลยครับ