snasui.com ยินดีต้อนรับ
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
ฟอรัมถาม-ตอบปัญหาการใช้งาน Macro และ 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] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
xoathx
Member
Posts: 3 Joined: Tue Aug 06, 2019 12:13 pm
#1
Post
by xoathx » Tue Aug 06, 2019 1:27 pm
สวัสดีครับ พอดีผมมีปัญหาคือพยายามทำสูตร vlookup ด้วย macro ครับ แต่จะแบ่งเป็นเฉพาะแต่ละชีทไป เช่น sheet นี้จะต้อง vlookup ข้อมูลจากช่อง 3 อีก sheet ต้อง vlookup ข้อมูลจากช่อง 4 เป็นต้นครับ.
ทีนี้ผมลอง Record macro แล้วนำมาแก้ แต่พบว่าไม่สามารถใช้งานได้ครับ จึงได้ปรับเปลี่ยนไปมาจนได้เป็นสูตรตามนี้ครับ แต่ยังติดปัญหา vlookup แล้วข้อมูลไม่มาครับ และ sheet ที่เลือก ข้อมูลไม่เดิมลงไปในช่องครับ จึงเรียนขอความช่วยเหลืออาจารย์ครับ.
Code: Select all
Sub Test2()
'
' Test Macro
'
'
Dim lastrow As Long
Sheets(Array("M880(C)", "M553(C)", "M551(C)", "M577(C)")). _
Select
Sheets("M880(C)").Activate
Range("BY4") = "=VLOOKUP(BY4,'Z:\Toner Tacking report 2018\BUMRUNGRAD (BIH)\[WJA_BIH.csv]WJA_BIH'!C:I,4,0)"
Range("BZ4") = "=VLOOKUP(BY4,'Z:\Toner Tacking report 2018\BUMRUNGRAD (BIH)\[WJA_BIH.csv]WJA_BIH'!C:I,7,0)"
Range("BY4:BZ4").Select
Selection.AutoFill Destination:=Range("BY4:BZ4" & lastrow)
Range("BY4:BZ4" & lastrow).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("BW1:BX3").Select
Selection.Copy
Range("BY1").Select
ActiveSheet.Paste
Range("BY1:BZ1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "6/8/2019"
Range("BY1:BZ1").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("BY1:BZ1").Select
End Sub
snasui
Site Admin
Posts: 31214 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#2
Post
by snasui » Tue Aug 06, 2019 8:22 pm
กรุณาแนบไฟล์ตัวอย่างมาด้วยและควรอธิบายมาด้วยว่าหากทำงานถูกต้องคำตอบจะเป็นอย่างไรครับ
xoathx
Member
Posts: 3 Joined: Tue Aug 06, 2019 12:13 pm
#3
Post
by xoathx » Wed Aug 07, 2019 12:34 pm
ผลที่ออกมาผิดครับ.สูตรจะออกมาเป็น 0 และใน sheet ที่เหลือไว้ ก็ไม่แสดงค่าครับ แสดงเพียงชีทแรกครับ.
แบบที่ต้องการครับ ทุกชีทที่เลือกจะแสดงผลลัพธ์ออกมาครับ.พร้อมทั้งจะแสดงผลลัพธ์ถึงแค่ cell สุดท้ายครับ.
Macro ที่ใช้ตอนนี้คือชื่อ Test ครับ.
snasui
Site Admin
Posts: 31214 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#4
Post
by snasui » Wed Aug 07, 2019 7:52 pm
ตัวอย่าง Code การ Loop เพื่อทำงานกับแต่ละชีตครับ
Code: Select all
Sub Test()
'
' Test Macro
'
'
Dim lastrow As Long
Dim s As Worksheet
For Each s In Sheets(Array("M880(C)", "M553(C)", "M551(C)", "M577(C)"))
lastrow = s.Range("BY" & s.Rows.Count).End(xlUp).Row
s.Activate
s.Range("BY4") = "=VLOOKUP(BY4,'Z:\Toner Tacking report 2018\BUMRUNGRAD (BIH)\[WJA_BIH.csv]WJA_BIH'!C:I,4,0)"
s.Range("BZ4") = "=VLOOKUP(BY4,'Z:\Toner Tacking report 2018\BUMRUNGRAD (BIH)\[WJA_BIH.csv]WJA_BIH'!C:I,7,0)"
s.Range("BY4:BZ4").Select
Selection.AutoFill Destination:=Range("BY4:BZ" & lastrow)
s.Range("BY4:BZ" & lastrow).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
s.Range("BW1:BX3").Select
Selection.Copy
s.Range("BY1").Select
ActiveSheet.Paste
s.Range("BY1:BZ1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "6/8/2019"
s.Range("BY1:BZ1").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
s.Range("BY1:BZ1").Select
Next s
End Sub
xoathx
Member
Posts: 3 Joined: Tue Aug 06, 2019 12:13 pm
#5
Post
by xoathx » Thu Aug 08, 2019 11:48 am
ขอบพระคุณอาจารย์ครับ ตอนนี้นำไปปรับใช้ได้เรียบร้อยแล้วครับ.