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()
Dim x As Double
Dim y As Double
x = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
y = Left(TextBox2.Text, Len(TextBox2.Text) - 1)
Sum = x + y
If Sum < 1 Then
Label2.Caption = 50 & "-" & 60
Label3.Caption = 50 & "-" & 60
Label4.Caption = 50 & "-" & 60
ElseIf Sum > 1.9 Then
Label2.Caption = 40 & "-" & 50
Label3.Caption = 40 & "-" & 50
Label4.Caption = 40 & "-" & 50
End If
If Sum > 3 Then
Label2.Caption = 30 & "-" & 40
Label3.Caption = 30 & "-" & 40
Label4.Caption = 30 & "-" & 40
End If
If Sum > 5.9 Then
Label2.Caption = 20 & "-" & 30
Label3.Caption = 20 & "-" & 30
Label4.Caption = 20 & "-" & 30
End If
If Sum > 8 Then
Label2.Caption = 10 & "-" & 20
Label3.Caption = 10 & "-" & 20
Label4.Caption = 10 & "-" & 20
End If
If TextBox1.Text < 1 And TextBox2.Text < 1 Then
Label2.Caption = 50 & "-" & 60
Label3.Caption = 50 & "-" & 60
Label4.Caption = 50 & "-" & 60
ElseIf TextBox1.Text > 1.9 And TextBox2.Text > 1.9 Then
Label2.Caption = 40 & "-" & 50
Label3.Caption = 40 & "-" & 50
Label4.Caption = 40 & "-" & 50
End If
If TextBox1.Text > 3 And TextBox2.Text > 3 Then
Label2.Caption = 30 & "-" & 40
Label3.Caption = 30 & "-" & 40
Label4.Caption = 30 & "-" & 40
End If
If TextBox1.Text > 5.9 And TextBox2.Text > 5.9 Then
Label2.Caption = 20 & "-" & 30
Label3.Caption = 20 & "-" & 30
Label4.Caption = 20 & "-" & 30
End If
If TextBox1.Text > 8 And TextBox2.Text > 8 Then
Label2.Caption = 10 & "-" & 20
Label3.Caption = 10 & "-" & 20
Label4.Caption = 10 & "-" & 20
End If
End Sub
Code: Select all
If TextBox1.Text < 1 And TextBox2.Text < 1 Then
Label2.Caption = 50 & "-" & 60
Label3.Caption = 50 & "-" & 60
Label4.Caption = 50 & "-" & 60
ElseIf TextBox1.Text > 1.9 And TextBox2.Text > 1.9 Then
Label2.Caption = 40 & "-" & 50
Label3.Caption = 40 & "-" & 50
Label4.Caption = 40 & "-" & 50
End If
Code: Select all
If x < 1 And y < 1 Then
Label2.Caption = 50 & "-" & 60
Label3.Caption = 50 & "-" & 60
Label4.Caption = 50 & "-" & 60
ElseIf x > 1.9 And y > 1.9 Then
Label2.Caption = 40 & "-" & 50
Label3.Caption = 40 & "-" & 50
Label4.Caption = 40 & "-" & 50
End If
อ๋อเหมือนเพียงแค่เราทำเป็นตัวแปร แทนใช่ไหมครับlogic wrote: ↑Tue Feb 20, 2018 11:45 am ลองปรับดูครับ
เอาตัวแปรมาใส่เป็นCode: Select all
If TextBox1.Text < 1 And TextBox2.Text < 1 Then Label2.Caption = 50 & "-" & 60 Label3.Caption = 50 & "-" & 60 Label4.Caption = 50 & "-" & 60 ElseIf TextBox1.Text > 1.9 And TextBox2.Text > 1.9 Then Label2.Caption = 40 & "-" & 50 Label3.Caption = 40 & "-" & 50 Label4.Caption = 40 & "-" & 50 End If
ที่เหลือก็ทำเหมือนกับอันนี้ครับCode: Select all
If x < 1 And y < 1 Then Label2.Caption = 50 & "-" & 60 Label3.Caption = 50 & "-" & 60 Label4.Caption = 50 & "-" & 60 ElseIf x > 1.9 And y > 1.9 Then Label2.Caption = 40 & "-" & 50 Label3.Caption = 40 & "-" & 50 Label4.Caption = 40 & "-" & 50 End If
ปัญหาเกิดครับ
Code: Select all
If x < 2 And y = "" Then
Label2.Caption = 50 & "-" & 60
Label3.Caption = 50 & "-" & 60
Label4.Caption = 50 & "-" & 60
ElseIf x > 1.9 And y = "" Then
Label2.Caption = 40 & "-" & 50
Label3.Caption = 40 & "-" & 50
Label4.Caption = 40 & "-" & 50
End If
Code: Select all
Dim x As Double
Dim y As Double
x = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
y = Left(TextBox2.Text, Len(TextBox2.Text) - 1)
Code: Select all
Dim x As Double
Dim y As Double
if textbox1.text = "" then
x = 0
else
x = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
end if
if textbox2.text = "" then
y = 0
else
y = Left(TextBox2.Text, Len(TextBox2.Text) - 1)
end if
Code: Select all
If x < 2 And y = 0 Then
Label2.Caption = 50 & "-" & 60
Label3.Caption = 50 & "-" & 60
Label4.Caption = 50 & "-" & 60
ElseIf x > 1.9 And y = 0 Then
Label2.Caption = 40 & "-" & 50
Label3.Caption = 40 & "-" & 50
Label4.Caption = 40 & "-" & 50
End If
อ๋อ ขอบคุณมากครับ ได้ความรู้เพิ่มเติม พอดี ผมมือใหม่ครับ พี่ ขอบคุณมากครับlogic wrote: ↑Tue Feb 20, 2018 1:56 pm ตามที่ผมเห็น x กับ y ต้องเป็นตัวเลข เพราะประกาศตัวแปรไว้เป็น double
พอเป็นตัวเลขแล้วจะให้มันมาเทียบกับค่า "" ไม่ได้ มันคนละรูปแบบกัน ค่าว่างที่เป็น "" เป็นตัวอักษร ต้องเทียบกับตัวอักษร ตัวเลขก็ต้องเทียบกับตัวเลข
ผมเห็นว่าให้แปลงเป็นแบบนี้จะง่ายกว่า คือหาก textbox1 เป็นค่าว่าง ให้ x เป็น 0 ถ้า textbox2 เป็นค่าว่างให้ y เป็น 0
จากโค้ดนี้
ถ้าแก้ให้เป็นตามที่ผมว่าไว้ข้างบนก็ใช้ if เข้ามาช่วยCode: Select all
Dim x As Double Dim y As Double x = Left(TextBox1.Text, Len(TextBox1.Text) - 1) y = Left(TextBox2.Text, Len(TextBox2.Text) - 1)
ทีนี้เวลาเทียบก็ให้เทียบ x, y กับตัวเลข ไม่ใช่เทียบกับ "" เช่นแบบนี้ครับCode: Select all
Dim x As Double Dim y As Double if textbox1.text = "" then x = 0 else x = Left(TextBox1.Text, Len(TextBox1.Text) - 1) end if if textbox2.text = "" then y = 0 else y = Left(TextBox2.Text, Len(TextBox2.Text) - 1) end if
Code: Select all
If x < 2 And y = 0 Then Label2.Caption = 50 & "-" & 60 Label3.Caption = 50 & "-" & 60 Label4.Caption = 50 & "-" & 60 ElseIf x > 1.9 And y = 0 Then Label2.Caption = 40 & "-" & 50 Label3.Caption = 40 & "-" & 50 Label4.Caption = 40 & "-" & 50 End If