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
Private Sub CommandButton1_Click()
ThisWorkbook.Worksheets("Storage information page 1").Unprotect Password:="1"
ThisWorkbook.Worksheets("Storage information page 2").Unprotect Password:="1"
If Worksheets("Input page").Range("B2").Value = "00:00" Then
Worksheets("Input page").Range("C2:D2").Select
Application.CutCopyMode = False
Selection.Copy
Worksheets("Storage information page 1").Range("C2:D2").Select
Selection.PasteSpecial Paste:=xlPasteValues
Else
Worksheets("Storage information page 1").Range("C2:D2").Value = 0
MsgBox ("Incomplete information, Please check and save again...")
End If
ThisWorkbook.Worksheets("Input page").Range("B2:D2").ClearContents
ThisWorkbook.Worksheets("Input page").Range("C5:D5").ClearContents
ThisWorkbook.Worksheets("Storage information page 1").Protect Password:="1"
ThisWorkbook.Worksheets("Storage information page 2").Protect Password:="1"
ActiveWorkbook.Save
End Sub
คือผมต้องการดั่งที่ผมอธิบายอยู่ในรูป ครับ อาจารย์snasui wrote: กรอกข้อมูลด้วยมือและแสดงค่าที่ถูกต้องของชีตและเซลล์ปลายทางมาด้วยจะได้เข้าใจตรงกันครับ
Code: Select all
Private Sub CommandButton1_Click()
Dim i As Integer, timeRng As Range
Dim timeInput As Range, rng1 As Range, rng2 As Range
With Sheets("Storage information page 1")
Set timeRng = .Range("b2:b25")
End With
With Sheets("Input page")
Set timeInput = .Range("b2")
Set rng1 = .Range("c2:d2")
Set rng2 = .Range("c5:d5")
End With
If Not IsError(Application.Match(timeInput.Value, timeRng, 0)) Then
i = Application.Match(timeInput.Value, timeRng, 0) - 1
ThisWorkbook.Worksheets("Storage information page 1").Unprotect Password:="1"
ThisWorkbook.Worksheets("Storage information page 2").Unprotect Password:="1"
With Sheets("Storage information page 1")
.Range("c2").Offset(i, 0).Resize(1, 2).Value = rng1.Value
End With
With Sheets("Storage information page 2")
.Range("c2").Offset(i, 0).Resize(1, 2).Value = rng2.Value
End With
Else
MsgBox "Please check your input time.", vbInformation
Exit Sub
End If
ThisWorkbook.Worksheets("Input page").Range("B2:D2").ClearContents
ThisWorkbook.Worksheets("Input page").Range("C5:D5").ClearContents
ThisWorkbook.Worksheets("Storage information page 1").Protect Password:="1"
ThisWorkbook.Worksheets("Storage information page 2").Protect Password:="1"
ActiveWorkbook.Save
End Sub
snasui wrote: ตัวอย่าการปรับ Code ครับ
Code: Select all
Private Sub CommandButton1_Click() Dim i As Integer, timeRng As Range Dim timeInput As Range, rng1 As Range, rng2 As Range With Sheets("Storage information page 1") Set timeRng = .Range("b2:b25") End With With Sheets("Input page") Set timeInput = .Range("b2") Set rng1 = .Range("c2:d2") Set rng2 = .Range("c5:d5") End With If Not IsError(Application.Match(timeInput.Value, timeRng, 0)) Then i = Application.Match(timeInput.Value, timeRng, 0) - 1 ThisWorkbook.Worksheets("Storage information page 1").Unprotect Password:="1" ThisWorkbook.Worksheets("Storage information page 2").Unprotect Password:="1" With Sheets("Storage information page 1") .Range("c2").Offset(i, 0).Resize(1, 2).Value = rng1.Value End With With Sheets("Storage information page 2") .Range("c2").Offset(i, 0).Resize(1, 2).Value = rng2.Value End With Else MsgBox "Please check your input time.", vbInformation Exit Sub End If ThisWorkbook.Worksheets("Input page").Range("B2:D2").ClearContents ThisWorkbook.Worksheets("Input page").Range("C5:D5").ClearContents ThisWorkbook.Worksheets("Storage information page 1").Protect Password:="1" ThisWorkbook.Worksheets("Storage information page 2").Protect Password:="1" ActiveWorkbook.Save End Sub
Xengsue wrote:snasui wrote: ตัวอย่าการปรับ Code ครับ
Code: Select all
Private Sub CommandButton1_Click() Dim i As Integer, timeRng As Range Dim timeInput As Range, rng1 As Range, rng2 As Range With Sheets("Storage information page 1") Set timeRng = .Range("b2:b25") End With With Sheets("Input page") Set timeInput = .Range("b2") Set rng1 = .Range("c2:d2") Set rng2 = .Range("c5:d5") End With If Not IsError(Application.Match(timeInput.Value, timeRng, 0)) Then i = Application.Match(timeInput.Value, timeRng, 0) - 1 ThisWorkbook.Worksheets("Storage information page 1").Unprotect Password:="1" ThisWorkbook.Worksheets("Storage information page 2").Unprotect Password:="1" With Sheets("Storage information page 1") .Range("c2").Offset(i, 0).Resize(1, 2).Value = rng1.Value End With With Sheets("Storage information page 2") .Range("c2").Offset(i, 0).Resize(1, 2).Value = rng2.Value End With Else MsgBox "Please check your input time.", vbInformation Exit Sub End If ThisWorkbook.Worksheets("Input page").Range("B2:D2").ClearContents ThisWorkbook.Worksheets("Input page").Range("C5:D5").ClearContents ThisWorkbook.Worksheets("Storage information page 1").Protect Password:="1" ThisWorkbook.Worksheets("Storage information page 2").Protect Password:="1" ActiveWorkbook.Save End Sub
ขอบคุณมาก ครับ อาจารย์
ใช้ได้ตามที่ต้องการ ครับ
แต่ว่ายังมีปัญหาอยู่ที่ว่า
1.ถ้าเราป้อนข้อมูลหมดแล้วเหลือแค่ยังไม่ได้ป้อนเวลาลงไปแล้วเราไปกดปุ่ม save เลย มันก็จะไปเขียนทับข้อมูลที่เซลล์ปลายทางเลย ครับ(ข้อนี้คืออยากให้เงื่อนไขตัวนี้ไปจัดเข้ากับเงื่อนไขของ Else ได้ไหมครับ เหมือนที่เราป้อนเวลาไม่ตรงหรือยังไม่ได้ป้อนเวลาลงไปแล้ว มันจะให้เราป้อนเวลาใหม่ ครับ)
Code: Select all
If timeInput.Value = "" Then
MsgBox "Please check your input time.", vbInformation
Exit Sub
Else
Code: Select all
Option Explicit
Private Sub CommandButton1_Click()
Dim i As Integer, timeRng As Range
Dim timeInput As Range, rng1 As Range, rng2 As Range
With Sheets("Storage information page 1")
Set timeRng = .Range("b2:b25")
End With
With Sheets("Input page")
Set timeInput = .Range("b2")
Set rng1 = .Range("c2:d2")
Set rng2 = .Range("c5:d5")
End With
If timeInput.Value = "" Then
MsgBox "Please check your input time.", vbInformation
Exit Sub
Else
If Not IsError(Application.Match(timeInput.Value, timeRng, 0)) Then
i = Application.Match(timeInput.Value, timeRng, 0) - 1
ThisWorkbook.Worksheets("Storage information page 1").Unprotect Password:="1"
ThisWorkbook.Worksheets("Storage information page 2").Unprotect Password:="1"
ThisWorkbook.Unprotect Password:="1"
With Sheets("Storage information page 1")
.Range("c2").Offset(i, 0).Resize(1, 2).Value = rng1.Value
End With
With Sheets("Storage information page 2")
.Range("c2").Offset(i, 0).Resize(1, 2).Value = rng2.Value
End With
Else
MsgBox "Please check your input time.", vbInformation
Exit Sub
End If
End If
ThisWorkbook.Worksheets("Input page").Range("B2:D2").ClearContents
ThisWorkbook.Worksheets("Input page").Range("C5:D5").ClearContents
ThisWorkbook.Worksheets("Storage information page 1").Protect Password:="1"
ThisWorkbook.Worksheets("Storage information page 2").Protect Password:="1"
ThisWorkbook.Protect Password:="1"
ActiveWorkbook.Save
End Sub
Code: Select all
If Not IsError(Application.Match(timeInput.Value, timeRng, 0)) Then
i = Application.Match(timeInput.Value, timeRng, 0) - 1
Code: Select all
With Sheets("Storage information page 1")
.Range("c2").Offset(i, 0).Resize(1, 2).Value = rng1.Value
End With
จาก Code นี้Xengsue wrote:อาจารย์ ครับ รบกวนขอให้อาจารย์ช่วยให้คำอธิบายเกี่ยวกับ 2 ตัวนี้ให้ด้วยครับ
ค่า -1 คือค่าอะไร มันมีความหมายถึงอะไร ครับCode: Select all
If Not IsError(Application.Match(timeInput.Value, timeRng, 0)) Then i = Application.Match(timeInput.Value, timeRng, 0) - 1
Resize(1, 2).Value มันหมายความว่ายังไงครับCode: Select all
With Sheets("Storage information page 1") .Range("c2").Offset(i, 0).Resize(1, 2).Value = rng1.Value End With
Code: Select all
If Not IsError(Application.Match(timeInput.Value, timeRng, 0)) Then
i = Application.Match(timeInput.Value, timeRng, 0) - 1
Resize(1, 2)
แปลว่าให้ขยายข้อมูลเป็น 1 บรรทัดและ 2 คอลัมน์ ส่วน .Value
เป็น Property ของข้อมูล หมายถึง ค่าของข้อมูลนั้น เช่น Range("A1").Value
คือค่าของ A1 นั่นเองsnasui wrote: Code ที่เขียนมากำหนดค่า i ไม่ตรงกับที่ผมแจ้งไป ขอให้ใช้ตามที่ผมแนะนำไปทุกอักขระเสียก่อนแล้วค่อยแจ้งสิ่งที่ผิดพลาด ช่วยทดสอบด้วย Code ที่ผมแจ้งไปอีกรอบ หากยังไม่ตรงให้แนบไฟล์มาใหม่ ระบุเวลาที่ผิดพลาดมาสัก 2-3 ค่าจะได้สะดวกในการทดสอบ โดย Code ในไฟล์ที่แนบมานั้นจะต้องเป็นไปตามที่ผมปรับปรุงไปล่าสุดแล้วยังเป็นปัญหา
แต่หากเป็นคำถามใหม่ที่จะถามต่อเนื่องกันไป จะต้องปรับปรุง Code เพื่องานนั้น ๆ เสียก่อน ติดแล้วค่อยถามกันครับครับ
Code: Select all
If timeInput.Value = "" Then
MsgBox "Please check your input time.", vbInformation
Exit Sub
Else
End If
Code: Select all
Private Sub CommandButton1_Click()
Dim i As Integer, timeRng As Range, rng As Range
Dim timeInput As Range, rng1 As Range, rng2 As Range
With Sheets("Storage information page 1")
Set timeRng = .Range("b2:b25")
End With
With Sheets("Input page")
Set timeInput = .Range("b2")
Set rng1 = .Range("c2:d2")
Set rng2 = .Range("c5:d5")
End With
If timeInput.Value = "" Then
MsgBox "Please check your input time.", vbInformation
Exit Sub
Else
If Application.CountIf(timeRng, timeInput.Value) Then
For Each rng In timeRng
If Round(rng.Value, 15) = Round(timeInput.Value, 15) Then
i = rng.Row
Exit For
End If
Next rng
ThisWorkbook.Worksheets("Storage information page 1").Unprotect Password:="1"
ThisWorkbook.Worksheets("Storage information page 2").Unprotect Password:="1"
ThisWorkbook.Unprotect Password:="1"
With Sheets("Storage information page 1")
.Range("c" & i).Resize(1, 2).Value = rng1.Value
End With
With Sheets("Storage information page 2")
.Range("c" & i).Resize(1, 2).Value = rng2.Value
End With
Else
MsgBox "Please check your input time.", vbInformation
Exit Sub
End If
End If
ThisWorkbook.Worksheets("Input page").Range("B2:D2").ClearContents
ThisWorkbook.Worksheets("Input page").Range("C5:D5").ClearContents
ThisWorkbook.Worksheets("Storage information page 1").Protect Password:="1"
ThisWorkbook.Worksheets("Storage information page 2").Protect Password:="1"
ThisWorkbook.Protect Password:="1"
ActiveWorkbook.Save
End Sub
จาก code ตัวนี้ผมลอง test ดูแล้วเจออยู่ 2 ปัญหาsnasui wrote: ตัวอย่างการปรับ Code ครับ
Code: Select all
Private Sub CommandButton1_Click() Dim i As Integer, timeRng As Range, rng As Range Dim timeInput As Range, rng1 As Range, rng2 As Range With Sheets("Storage information page 1") Set timeRng = .Range("b2:b25") End With With Sheets("Input page") Set timeInput = .Range("b2") Set rng1 = .Range("c2:d2") Set rng2 = .Range("c5:d5") End With If timeInput.Value = "" Then MsgBox "Please check your input time.", vbInformation Exit Sub Else If Application.CountIf(timeRng, timeInput.Value) Then For Each rng In timeRng If Round(rng.Value, 15) = Round(timeInput.Value, 15) Then i = rng.Row Exit For End If Next rng ThisWorkbook.Worksheets("Storage information page 1").Unprotect Password:="1" ThisWorkbook.Worksheets("Storage information page 2").Unprotect Password:="1" ThisWorkbook.Unprotect Password:="1" With Sheets("Storage information page 1") .Range("c" & i).Resize(1, 2).Value = rng1.Value End With With Sheets("Storage information page 2") .Range("c" & i).Resize(1, 2).Value = rng2.Value End With Else MsgBox "Please check your input time.", vbInformation Exit Sub End If End If ThisWorkbook.Worksheets("Input page").Range("B2:D2").ClearContents ThisWorkbook.Worksheets("Input page").Range("C5:D5").ClearContents ThisWorkbook.Worksheets("Storage information page 1").Protect Password:="1" ThisWorkbook.Worksheets("Storage information page 2").Protect Password:="1" ThisWorkbook.Protect Password:="1" ActiveWorkbook.Save End Sub
Code: Select all
If Round(rng.Value, 15) = Round(timeInput.Value, 15) Then
Code: Select all
'Other code
If timeInput.Value = "" Then
MsgBox "Please check your input time.", vbInformation
Exit Sub
Else
For Each rng In timeRng
If Application.Text(rng.Value, "h:mm") = Application.Text(timeInput.Value, "h:mm") Then
i = rng.Row
Exit For
End If
Next rng
If i > 0 Then
ThisWorkbook.Worksheets("Storage information page 1").Unprotect Password:="1"
ThisWorkbook.Worksheets("Storage information page 2").Unprotect Password:="1"
ThisWorkbook.Unprotect Password:="1"
With Sheets("Storage information page 1")
.Range("c" & i).Resize(1, 2).Value = rng1.Value
End With
With Sheets("Storage information page 2")
.Range("c" & i).Resize(1, 2).Value = rng2.Value
End With
Else
MsgBox "Please check your input time.", vbInformation
Exit Sub
End If
End If
'Other code
คำว่า "อาไร" ต้องเขียนว่า "อะไร" ไม่เช่นนั้นผิดกฎการใช้บอร์ดข้อ 1 ด้านบนXengsue wrote:เพราะอาไรถึงต้องใส่ 15 ครับ
แสดงว่าการกำหนดลักษณะนั้นยังเกิดความแตกต่างกันระหว่างค่าที่คีย์ลงไปเองในเซลล์กับค่าที่ได้จากการคำนวณ ถ้าจำได้จะเห็นว่าชีตแรก เป็นการคีย์เข้าไปเองโดยผู้ใช้เพื่อกำหนดเวลา แต่ชีตที่เหลือเกิดจากการบวกต่อเนื่องกันไปเพื่อให้เป็นเวลาในชม.ถัด ๆ ไป และการกำหนด Format ด้วย Code ล่าสุดสามารถที่จะจัดการปัญหาความแตกต่างตรงนี้ได้Xengsue wrote:ปัญหาที่ มัน error จาก code เมื่อกี้ มัน error ได้ยังไง ดูสูตรก็ถูกแล้ว run ก็ผ่านไปหลายตัวแล้ว แต่ทำไมมันถึง error ที่ตัวเดียวนั่น
ขอโทษด้วย ครับsnasui wrote:คำว่า "อาไร" ต้องเขียนว่า "อะไร" ไม่เช่นนั้นผิดกฎการใช้บอร์ดข้อ 1 ด้านบน
ฉะนั้น อาจารย์ จึ่งต้องใช้คำสั่ง text มาแทนคำสั่ง round ใช่ไหมครับsnasui wrote:แสดงว่าการกำหนดลักษณะนั้นยังเกิดความแตกต่างกันระหว่างค่าที่คีย์ลงไปเองในเซลล์กับค่าที่ได้จากการคำนวณ ถ้าจำได้จะเห็นว่าชีตแรก เป็นการคีย์เข้าไปเองโดยผู้ใช้เพื่อกำหนดเวลา แต่ชีตที่เหลือเกิดจากการบวกต่อเนื่องกันไปเพื่อให้เป็นเวลาในชม.ถัด ๆ ไป และการกำหนด Format ด้วย Code ล่าสุดสามารถที่จะจัดการปัญหาความแตกต่างตรงนี้ได้
ครับผม รับทราบครับXengsue wrote:ขอโทษด้วย ครับ
คือผมเป็นคนลาว แล้วไม่ค่อยเก่งหลักภาษาไทยเท่าไร ครับ ขอโทษมากฯ ครับ
คราวหน้าจะไม่ให้พลาดอีก ครับ
ถูกต้องครับXengsue wrote:ฉะนั้น อาจารย์ จึ่งต้องใช้คำสั่ง text มาแทนคำสั่ง round ใช่ไหมครับ