Page 1 of 2
ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่นแล้ว
Posted: Fri Feb 15, 2013 11:19 am
by whatman
มือใหม่มากครับ ลองทำ vba เซฟเป็น xlxm เปิดที่เครื่องตัวเองลองกดก็รันได้ปกติ แต่ copy ที่เครื่องอื่นพอกด run ก็ error code ต้องทำอย่างไรครับ
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Fri Feb 15, 2013 11:40 am
by snasui

ควรแนบ Code แนบไฟล์ตัวอย่างมาด้วย เพื่อจะได้ช่วยดูว่าเขียน Code ไว้อย่างไรครับ
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Mon Feb 18, 2013 12:42 pm
by whatman
โปรดชี้แนะด้วยครับ
ต้องขอโทษด้วยครับ กลัวท่านจะไม่ได้กลับมาอ่านเพราะโพสต์ไว้ตั้งแต่วันศุกร์ครับ
Private Sub CmdConvrt_Click()
Dim Handle As Integer, File_Name As String
Dim WriteRow As Long, TemArr, OneLine As String
Dim price1, price2, price3 As String
Handle = FreeFile
File_Name = "D:\Testrun.txt"
Open File_Name For Input As #Handle
Do While Not EOF(Handle)
Line Input #Handle, OneLine
TemArr = Split(OneLine, "|")
If UBound(TemArr) > 1 Then
WriteRow = WriteRow + 1
Cells(WriteRow, 1) = TemArr(0)
Cells(WriteRow, 2) = TemArr(1)
Cells(WriteRow, 3) = TemArr(2)
Cells(WriteRow, 4) = TemArr(4)
Cells(WriteRow, 5) = TemArr(5)
Cells(WriteRow, 6) = TemArr(5)
Cells(WriteRow, 7) = TemArr(7)
Cells(WriteRow, 8) = TemArr(8)
Cells(WriteRow, 9) = TemArr(9)
If TemArr(29) = "S225-1-1011-001" Then
Cells(WriteRow, 10) = Txtprc1.Value
Cells(WriteRow, 11) = TemArr(8) * Txtprc1
ElseIf TemArr(29) = "S225-1-1211-002" Then
Cells(WriteRow, 10) = Txtprc2.Value
Cells(WriteRow, 11) = TemArr(8) * Txtprc2
ElseIf TemArr(29) = "S225-1-DUMM-001" Then
Cells(WriteRow, 10) = Txtprc3.Value
Cells(WriteRow, 11) = TemArr(8) * Txtprc3
End If
'Cells(WriteRow, 11) = TemArr(8) * DC
Cells(WriteRow, 12) = TemArr(9)
Cells(WriteRow, 13) = TemArr(24)
Cells(WriteRow, 14) = TemArr(25)
Cells(WriteRow, 15) = TemArr(26)
End If
Loop
Close #Handle
End Sub
Private Sub CmdSavefiles_Click()
ActiveWorkbook.SaveAs Filename:= _
"D:\testrun.txt", FileFormat:= _
xlTextMSDOS, CreateBackup:=False
End Sub
Private Sub RefEdit1_BeforeDragOver(Cancel As Boolean, ByVal Data As MSForms.DataObject, ByVal x As stdole.OLE_XPOS_CONTAINER, ByVal y As stdole.OLE_YPOS_CONTAINER, ByVal DragState As MSForms.fmDragState, Effect As MSForms.fmDropEffect, ByVal Shift As Integer)
End Sub
Private Sub Txtprc1_Change()
If Me.Txtprc1.Value = blank Then Exit Sub
With Me.Txtprc1
chkval = Application.WorksheetFunction.IsNumber(Evaluate(Me.Txtprc1.Value))
If chkval = False Then
MsgBox (" Input only number.")
.Value = Left(.Value, Len(.Value) - 1)
End If
End With
End Sub
Private Sub Txtprc2_Change()
If Me.Txtprc2.Value = blank Then Exit Sub
With Me.Txtprc2
chkval = Application.WorksheetFunction.IsNumber(Evaluate(Me.Txtprc2.Value))
If chkval = False Then
MsgBox (" Input only number.")
.Value = Left(.Value, Len(.Value) - 1)
End If
End With
End Sub
Private Sub Txtprc3_Change()
If Me.Txtprc3.Value = blank Then Exit Sub
With Me.Txtprc3
chkval = Application.WorksheetFunction.IsNumber(Evaluate(Me.Txtprc3.Value))
If chkval = False Then
MsgBox (" Input only number.")
.Value = Left(.Value, Len(.Value) - 1)
End If
End With
End Sub
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Mon Feb 18, 2013 12:45 pm
by whatman
โปรดช่วยดูด้วยครับ ขอบคุณมากครับ
แล้วต้องการที่จะดึงอีกไฟล์ด้วยครับ
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Mon Feb 18, 2013 6:01 pm
by snasui

ไม่ทราบว่า Code Error ที่บรรทัดใดครับ
ควรแนบไฟล์ Text ตัวอย่างให้ทดสอบด้วยครับ
สำหรับ Code ด้านล่างและ Code ที่คล้ายกัน
Code: Select all
If Me.Txtprc1.Value = blank Then Exit Sub
หากตัวแปร blank หมายถึงไม่กรอกค่าใดสามารถเขียนได้เป็น
Code: Select all
If Me.Txtprc1.Value = "" Then Exit Sub
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Tue Feb 19, 2013 8:53 am
by whatman
ขอบคุณครับ ตัวอย่างไฟล์ที่ดึงมาครับ
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Tue Feb 19, 2013 12:04 pm
by snasui

ผมทดสอบแล้วไม่ติดปัญหาใด สำหรับการโพสต์ Code ให้เป็น Code เพื่อสะดวกในการอ่านและ Copy ไปใช้ ดูตัวอย่างที่นี่ครับ
viewtopic.php?f=3&t=1187
ไม่ทราบว่าเครื่องของ User ใช้ Excel Version ใด และติดที่ Procedure ใด บรรทัดใดครับ
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Tue Feb 19, 2013 1:45 pm
by whatman
เครื่องผมใช้ excel 2007 เครื่องอื่นก็ใช้ 2007 เช่นเดียวกันครับ
ที่เครื่องผมเองรันผ่านแต่พอรันที่เครื่องอื่นขึ้นแบบนี้ครับ
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Tue Feb 19, 2013 2:11 pm
by snasui

กรณีต้องการให้กรอกได้เฉพาะตัวเลขและเป็น 1 หลักเท่านั้นลองดูตัวอย่างการปรับ Code ตามด้านล่างครับ
Code: Select all
Private Sub Txtprc1_Change()
If Me.Txtprc1.Value = "" Then Exit Sub
With Me.Txtprc1
If Not .Text Like "#" Then
MsgBox (" Input only number.")
.Value = ""
End If
End With
End Sub
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Tue Feb 19, 2013 3:19 pm
by whatman
ขอบคุณมากครับ กรณีต้องการกรอกตัวเลขหลายหลักและมีทศนิยมด้วยทำอย่างไรครับ ลองใส่เป็น ###.## เครื่องไม่รับครับ
Code: Select all
Private Sub CmdConvrt_Click()
Dim Handle As Integer, File_Name As String
Dim WriteRow As Long, TemArr, OneLine As String
Dim price1, price2, price3 As String
Handle = FreeFile
File_Name = "D:\Testrun.txt"
Open File_Name For Input As #Handle
Do While Not EOF(Handle)
Line Input #Handle, OneLine
TemArr = Split(OneLine, "|")
If UBound(TemArr) > 1 Then
WriteRow = WriteRow + 1
Cells(WriteRow, 1) = TemArr(0)
Cells(WriteRow, 2) = TemArr(1)
Cells(WriteRow, 3) = TemArr(2)
Cells(WriteRow, 4) = TemArr(4)
Cells(WriteRow, 5) = TemArr(5)
Cells(WriteRow, 6) = TemArr(5)
Cells(WriteRow, 7) = TemArr(7)
Cells(WriteRow, 8) = TemArr(8)
Cells(WriteRow, 9) = TemArr(9)
If TemArr(29) = "S225-1-1011-001" Then
Cells(WriteRow, 10) = Txtprc1.Value
Cells(WriteRow, 11) = TemArr(8) * Txtprc1
ElseIf TemArr(29) = "S225-1-1211-002" Then
Cells(WriteRow, 10) = Txtprc2.Value
Cells(WriteRow, 11) = TemArr(8) * Txtprc2
ElseIf TemArr(29) = "S225-1-DUMM-001" Then
Cells(WriteRow, 10) = Txtprc3.Value
Cells(WriteRow, 11) = TemArr(8) * Txtprc3
End If
'Cells(WriteRow, 11) = TemArr(8) * DC
Cells(WriteRow, 12) = TemArr(9)
Cells(WriteRow, 13) = TemArr(24)
Cells(WriteRow, 14) = TemArr(25)
Cells(WriteRow, 15) = TemArr(26)
End If
Loop
Close #Handle
End Sub
จาก code ผมดึงข้อมูลออกมาเป็นตัวๆโดยใช้ตัว | ในการแบ่งข้อมูล ทีนี้ในไฟล์นี้ขาดข้อมูลที่จะต้องการตัดไป โดยต้องนำข้อมูลตัวที่4ในการไปเปรียบเทียบแล้วนำข้อมูลนั้นมาใช้ในบรรทัดเดียวกัน
อย่างเช่นข้อมูล 21M0008243 21M0008244 21M0008245 21M0008249 ให้เป็น MS1302001
ข้อมูล 21M0008246 21M0008247 21M0008258 ให้เป็น MS1302002
อยากให้ดึงข้อมูลของแถวที่4มาแสดงที่หน้าจอแล้วเรากรอกเลขลงไป
หรือถ้าไม่ได้ก็อยากได้แบบที่เรากรอกด้านซ้ายเป็น 21M0008243 และด้านขวาเป็น MS1302001 แล้วนำข้อมูลที่เรากรอกลงไปเก็บในช่องครับ
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Tue Feb 19, 2013 4:59 pm
by snasui

ตัวอย่างการปรับ Code สำหรับการกอกตัวเลขได้หลายตัวพร้อมมี . คั่นตามด้านล่างครับ
ในโอกาสต่อไปควรปรับปรุง Code ที่ให้ไปมาเองก่อน ติดตรงไหนค่อยมาดูกันต่อครับ
Code: Select all
Private Sub Txtprc1_Change()
If Me.Txtprc1.Value = "" Then Exit Sub
With Me.Txtprc1
If Not Right(.Text, 1) Like "#" And Right(.Text, 1) <> "." Then
MsgBox (" Input only number.")
.Value = Left(.Value, Len(.Value) - 1)
End If
End With
End Sub
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Tue Feb 19, 2013 5:20 pm
by whatman
นำโค๊ดมารันแล้วเป็นดังรูปครับ รบกวนด้วยครับท่าน
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Tue Feb 19, 2013 8:00 pm
by snasui

ในเครื่องผม Code สามารถทำงานได้ แต่หากคีย์ . ซ้ำจะต้องปรับ Code ใหม่ ผมเลยปรับเป็นตามด้านล่าง ลองทดสอบดูครับ
Code: Select all
Private Sub Txtprc1_Change()
If Me.Txtprc1.Value = "" Then Exit Sub
On Error Resume Next
With Me.Txtprc1
chkval = Evaluate(.Value)
If Left(chkval, 5) = "Error" Then
MsgBox (" Input only number.")
.Value = Left(.Value, Len(.Value) - 1)
End If
End With
End Sub
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Wed Feb 20, 2013 11:15 am
by whatman
ลองใช้โค๊ดของท่านแล้วเมื่อเปิดกับเครื่องอื่นก็รันไม่ได้ครับ โปรดชี้แนะด้วยครับท่าน
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Wed Feb 20, 2013 11:29 am
by snasui

ช่วย Post ภาพการฟ้อง Error ของโปรแกรมมาด้วยครับ
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Fri Feb 22, 2013 3:59 pm
by whatman
ต้องการให้บรรทัดสุดท้ายรวมราคาทั้งหมดแต่ error ครับ
ลองใส่ผลรวมได้แค่ผมรวมของรอบสุดท้าย ไม่แน่ใจว่าใส่ผิดตรงไหนคือลองใส่ใน loop ก็ได้ตัวสุดท้ายตัวเดียว
ใส่นอก loop ก็ไม่คำนวณเลยครับ
Code: Select all
Private Sub CmdConvrt_Click()
Dim Handle As Integer, File_Name As String
Dim WriteRow As Long, TemArr, OneLine As String
Dim Price1, Price2, Price3 As String
Dim Pono() As Variant
Dim Invno() As String
Dim Chkval As Integer
Dim Sump1 As Integer
Dim Sump2 As Integer
Dim Sump3 As Integer
Dim Sumpr As Integer
Dim Sumprice As Integer
Handle = FreeFile
File_Name = "D:\testrun.txt"
Open File_Name For Input As #Handle
Do While Not EOF(Handle)
Line Input #Handle, OneLine
TemArr = Split(OneLine, "|")
If UBound(TemArr) > 1 Then
WriteRow = WriteRow + 1
Cells(WriteRow, 1) = "VMI050"
Cells(WriteRow, 2) = TemArr(1)
Cells(WriteRow, 3) = TemArr(2)
'Pono = TemArr(4)
Cells(WriteRow, 4) = TemArr(4)
Cells(WriteRow, 5) = TemArr(5)
Cells(WriteRow, 6) = TemArr(5)
Cells(WriteRow, 7) = TemArr(7)
Cells(WriteRow, 8) = TemArr(8)
Cells(WriteRow, 9) = " PC"
If TemArr(4) = 9999999999# Then
Cells(WriteRow, 9) = " "
Cells(WriteRow, 11) = Sumprice
End If
If TemArr(29) = "S225-1-1011-001" Then
Cells(WriteRow, 10) = Txtprc1.Value
Cells(WriteRow, 11) = TemArr(8) * Txtprc1
Sump1 = Cells(WriteRow, 11)
ElseIf TemArr(29) = "S225-1-1211-002" Then
Cells(WriteRow, 10) = Txtprc2.Value
Cells(WriteRow, 11) = TemArr(8) * Txtprc2
Sump2 = Cells(WriteRow, 11)
ElseIf TemArr(29) = "S225-1-DUMM-001" Then
Cells(WriteRow, 10) = Txtprc3.Value
Cells(WriteRow, 11) = TemArr(8) * Txtprc3
Sump3 = Cells(WriteRow, 11)
End If
'Cells(WriteRow, 11) = TemArr(8) * DC
Cells(WriteRow, 12) = TemArr(9)
Cells(WriteRow, 13) = TemArr(24)
Cells(WriteRow, 14) = TemArr(25)
Cells(WriteRow, 15) = TemArr(26)
Sumpr = Sump1 + Sump2 + Sump3
'Sumprice = Sumpr + Sumprice
'Sumprice = Sumprice + Sumpr
End If
'Sumprice = Sumprice + Sumpr
Loop
Close #Handle
End Sub
Private Sub CmdSavefiles_Click()
ActiveWorkbook.SaveAs Filename:= _
"D:\testfile.txt", FileFormat:= _
xlTextMSDOS, CreateBackup:=False
End Sub
Private Sub RefEdit1_BeforeDragOver(Cancel As Boolean, ByVal Data As MSForms.DataObject, ByVal x As stdole.OLE_XPOS_CONTAINER, ByVal y As stdole.OLE_YPOS_CONTAINER, ByVal DragState As MSForms.fmDragState, Effect As MSForms.fmDropEffect, ByVal Shift As Integer)
End Sub
Private Sub Txtprc1_Change()
If Me.Txtprc1.Value = "" Then Exit Sub
On Error Resume Next
With Me.Txtprc1
Chkval = Evaluate(.Value)
If Left(Chkval, 5) = "Error" Then
MsgBox (" Input only number.")
.Value = Left(.Value, Len(.Value) - 1)
End If
End With
End Sub
Private Sub Txtprc2_Change()
If Me.Txtprc2.Value = "" Then Exit Sub
On Error Resume Next
With Me.Txtprc2
Chkval = Evaluate(.Value)
If Left(Chkval, 5) = "Error" Then
MsgBox (" Input only number.")
.Value = Left(.Value, Len(.Value) - 1)
End If
End With
End Sub
Private Sub Txtprc3_Change()
If Me.Txtprc3.Value = "" Then Exit Sub
On Error Resume Next
With Me.Txtprc3
Chkval = Evaluate(.Value)
If Left(Chkval, 5) = "Error" Then
MsgBox (" Input only number.")
.Value = Left(.Value, Len(.Value) - 1)
End If
End With
End Sub
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Fri Feb 22, 2013 10:34 pm
by snasui

ลองแนบไฟล์ตัวอย่างคำตอบที่ต้องการมาด้วยครับว่าบรรทัดสุดท้ายที่ว่านั้นค่าเป็นเท่าใด รวมคอลัมน์ใด จะได้เข้าใจตรงกันครับ
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Tue Feb 26, 2013 11:42 am
by whatman
ไฟล์ที่ต้องการจะเป็นแบบนี้ครับ
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Tue Feb 26, 2013 12:42 pm
by snasui

แนบมาเป็น Excel และแสดงให้เห็นว่าบรรทัดรวมอยู่คอลัมน์ไหน เซลล์ไหนครับ
Re: ทำ vba ไว้ที่เครื่องตัวเองเปิดได้แต่ไปเปิดกับเครื่องอื่น
Posted: Thu Feb 28, 2013 9:35 am
by whatman
ตรงส่วนของผลรวมครับ ไฮไลสีเหลืองไว้ครับ