snasui.com ยินดีต้อนรับ
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
คำแสดงเจตนา
ขอผลแห่งการให้ความรู้นี้ จงกลับไปยังผู้ที่เป็นเจ้าของเดิม แม้ข้าพเจ้าจะไม่รู้จักท่านก็ตาม ขอให้แสงแห่งปัญญาที่ท่านเคยจุดไว้ ได้กลับไปเติมเต็มชีวิตของท่านอีกครั้ง และขอให้เจตนาของข้าพเจ้าเป็นการคืนความดีอย่างสงบ
ฟอรัมถาม-ตอบปัญหาการใช้งาน MS Excel and VBA
Forum rules
ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
Godtheking
Member
Posts: 34 Joined: Thu Feb 23, 2023 7:02 am
Excel Ver: 2010,365
#1
Post
by Godtheking » Tue Mar 05, 2024 11:37 am
ผมอยากจะแทนค่าใน Set sh = ThisWorkbook.Sheets("Trim")
ให้ Trim เก็บค่าเอา Sheets จาก Option Button ถ้าเลือก PBP ให้ Trim เปลี่ยนเป็น PBP
ต้องเขียนโค้ดอย่างไงครับ
Code: Select all
Dim sh As Worksheet
Dim iRow As Long
Set sh = ThisWorkbook.Sheets("Trim")
If frmForm.txtRowNumber.Value = "" Then
iRow = Sheets("Trim").Range("A" & Rows.Count).End(xlUp).Row + 1
Else
iRow = frmForm.txtRowNumber.Value
End If
You do not have the required permissions to view the files attached to this post.
snasui
Site Admin
Posts: 31175 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#2
Post
by snasui » Tue Mar 05, 2024 12:26 pm
ตัวอย่าง การปรับ Code ลองประยุกต์ใช้ดูครับ
Code: Select all
'Other code'
Dim x As Object
Dim str As String
For Each x In frmForm.Frame5.Controls
If x.Value = True Then
str = x.Name
Exit For
End If
Next
Set sh = ThisWorkbook.Sheets(x)
'Other code
ในโอกาสถัดไปกรุณาระบุรายละเอียดให้ครบว่า Code อยู่ที่ Module ไหน ชื่อว่าอะไร อ่านกฎการใช้บอร์ดข้อ 5 ด้านบนประกอบ
จะได้เข้าถึงปัญหาโดยไวครับ
Godtheking
Member
Posts: 34 Joined: Thu Feb 23, 2023 7:02 am
Excel Ver: 2010,365
#3
Post
by Godtheking » Wed Mar 06, 2024 9:40 am
ผมลองปรับใช้ได้แร้วครับแต่ตอนนี้ผมติดปัญหาเรื่องของการ UPDATE
ผม Edit ข้อมูลขึ้นมาแก้ไข แร้วกด Update ข้อมูลของผมที่ Update ไม่ได้ทับตัวที่ Edit
ขั้นมาแต่ข้อมูลลงไปอยู่ล่าสุด
ผมต้องปรับแก้ Code ตรงไหนบ้างครับ
Code: Select all
Sub Update_Weight() ''' *** Update *** '''
Dim sh As Worksheet
Dim iRow As Long
Dim x As Object
Dim str As String
For Each x In frmForm.Frame5.Controls
If x.Value = True Then
str = x.Name
Exit For
End If
Next
Set sh = ThisWorkbook.Sheets(str)
If frmForm.txtRowNumber.Value = "" Then
iRow = sh.Range("A" & Rows.Count).End(xlUp).Row + 1
Else
iRow = frmForm.txtRowNumber.Value
End If
If frmForm.txtPart.Value = "" Then
MsgBox "Please specify Part No."
ElseIf frmForm.txtName.Value = "" Then
MsgBox "Please specify PART Name."
ElseIf Len(frmForm.txtModel.Value) <> 3 Then
MsgBox "Please specify Model"
ElseIf Len(frmForm.txtControl.Value) <> 4 Then
MsgBox "Please specify CONTROL"
ElseIf frmForm.txtQty.Value = "" Then
MsgBox "Please specify Q'TY"
ElseIf frmForm.txtPiece.Value = "" Then
MsgBox "Please specify By Piece"
ElseIf Len(frmForm.txtDate.Value) <> 8 Then
MsgBox "Please specify Date."
ElseIf Len(frmForm.txtID.Value) <> 4 Then
frmForm.txtPart.SetFocus
Else
With sh
.Cells(iRow, 1) = iRow - 2
.Cells(iRow, 2) = frmForm.txtPart.Value
.Cells(iRow, 3) = frmForm.txtName.Value
.Cells(iRow, 4) = frmForm.txtModel.Value
.Cells(iRow, 5) = frmForm.txtControl.Value
.Cells(iRow, 6) = frmForm.txtQty.Value
.Cells(iRow, 7) = frmForm.txtBox1.Value
.Cells(iRow, 8) = frmForm.txtBox2.Value
.Cells(iRow, 9) = frmForm.txtBox3.Value
.Cells(iRow, 10) = frmForm.txtBox4.Value
.Cells(iRow, 11) = frmForm.txtBox5.Value
.Cells(iRow, 12) = frmForm.txtBox6.Value
.Cells(iRow, 13) = frmForm.txtBox7.Value
.Cells(iRow, 14) = frmForm.txtBox8.Value
.Cells(iRow, 15) = frmForm.txtBox9.Value
.Cells(iRow, 16) = frmForm.txtBox10.Value
.Cells(iRow, 17) = frmForm.txtPiece.Value
.Cells(iRow, 18) = frmForm.txtStandard.Value
.Cells(iRow, 19) = frmForm.txtLower.Value
.Cells(iRow, 20) = frmForm.txtUpper.Value
.Cells(iRow, 21) = frmForm.txtPercent.Value
.Cells(iRow, 22) = frmForm.txtPercent1.Value
.Cells(iRow, 23) = frmForm.txtID.Value
.Cells(iRow, 24) = frmForm.txtDate.Value
.Cells(iRow, 25) = frmForm.txtIDName.Value
.Cells(iRow, 26) = Left(frmForm.txtPart.Value, 8)
.Cells(iRow, 27) = frmForm.txtCheck.Value
End With
End If
'Call SaveData
End Sub
snasui
Site Admin
Posts: 31175 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#4
Post
by snasui » Wed Mar 06, 2024 10:32 am
ช่วยโพสต์ให้เป็นไปตามกฎการใช้บอร์ดข้อ 1 ด้านบน
แล้วแจ้งมาใหม่ครับ