Code VBA วางข้อมูลพร้อมระบายสีและวางเส้นตารางค่ะ
Posted: Wed Apr 29, 2015 1:28 pm
เรียนอาจารย์และท่านผู้รู้ช่วยเรื่องปรับโค๊ดค่ะ
ตัวอย่างไฟล์แนบให้นำข้อมูลที่ชีท Sheet1 ไปวางที่ชีท Sheet2 พร้อมระบายสีและตีเส้นตาราง ตามตัวอย่างที่ชีท Sheet2 ค่ะ
ความต้องการคือ วางข้อมูล+ระบายสี+ตีเส้นตาราง สลับกับ วางข้อมูล+ตีเส้นตาราง สลับกันตามตัวอย่างไฟล์แนบชีท Sheet2 ค่ะ
โค๊ดด้านล่างทั้งสามโค๊ด จะปรับอย่างไรเพื่อใช้ร่วมกันได้ โดยให้เหลือเป็นโค๊ดเดียวค่ะ
ตัวอย่างไฟล์แนบให้นำข้อมูลที่ชีท Sheet1 ไปวางที่ชีท Sheet2 พร้อมระบายสีและตีเส้นตาราง ตามตัวอย่างที่ชีท Sheet2 ค่ะ
ความต้องการคือ วางข้อมูล+ระบายสี+ตีเส้นตาราง สลับกับ วางข้อมูล+ตีเส้นตาราง สลับกันตามตัวอย่างไฟล์แนบชีท Sheet2 ค่ะ
โค๊ดด้านล่างทั้งสามโค๊ด จะปรับอย่างไรเพื่อใช้ร่วมกันได้ โดยให้เหลือเป็นโค๊ดเดียวค่ะ
Code: Select all
Sub PasteData()
Dim rSource As Range
Dim rTarget As Range
With Worksheets("Sheet1")
Set rSource = .Range("A2:F2").Resize(.Range("G1"))
End With
Set rTarget = Worksheets("Sheet2").Range("A1000").End(xlUp).Offset(1, 0)
rSource.Copy
rTarget.PasteSpecial xlPasteValues
Application.CutCopyMode = False
End SubCode: Select all
Sub Macro1()
Dim rSource As Range
Dim rTarget As Range
With Worksheets("Sheet1")
Set rSource = .Range("A2:F2").Resize(.Range("G1"))
End With
Set rTarget = Worksheets("Sheet2").Range("A1000").End(xlUp).Offset(1, 0)
rSource.Copy
rTarget.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDash
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDash
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDash
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDash
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlDash
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlDash
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
End SubCode: Select all
Sub Macro2()
Dim rSource As Range
Dim rTarget As Range
With Worksheets("Sheet1")
Set rSource = .Range("A2:F2").Resize(.Range("G1"))
End With
Set rTarget = Worksheets("Sheet2").Range("A1000").End(xlUp).Offset(1, 0)
rSource.Copy
rTarget.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDash
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDash
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDash
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDash
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlDash
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlDash
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
End Sub