snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
Sub MergeColumnsInAB()
Dim wsAB As Worksheet
Dim wsAA As Worksheet
Dim lastColumnAA As Long
Dim colStart As Long
Dim colEnd As Long
Dim i As Long
Set wsAB = ThisWorkbook.Sheets("AB")
Set wsAA = ThisWorkbook.Sheets("AA")
lastColumnAA = wsAA.Cells(1, wsAA.Columns.Count).End(xlToLeft).Column
colStart = 2
colEnd = 5
For i = colStart To lastColumnAA Step 4
If colEnd > wsAB.Columns.Count Then colEnd = wsAB.Columns.Count
With wsAB.Range(wsAB.Cells(2, colStart), wsAB.Cells(2, colEnd))
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
colStart = colStart + 4
colEnd = colEnd + 4
Next i
End Sub
You do not have the required permissions to view the files attached to this post.
Sub MergeColumnsInAB()
Dim wsAB As Worksheet
Dim wsAA As Worksheet
Dim lastColumnAA As Long
Dim colStart As Long
Dim colEnd As Long
Dim i As Long
Set wsAB = ThisWorkbook.Sheets("AB")
Set wsAA = ThisWorkbook.Sheets("AA")
lastColumnAA = wsAA.Cells(1, wsAA.Columns.Count).End(xlToLeft).Column - 1
colStart = 2
colEnd = 5
For i = colStart To lastColumnAA * 4 Step 4
If colEnd > wsAB.Columns.Count Then colEnd = wsAB.Columns.Count
With wsAB.Range(wsAB.Cells(2, colStart), wsAB.Cells(2, colEnd))
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
colStart = colStart + 4
colEnd = colEnd + 4
Next i
End Sub
Sub MergeColumnsInAB()
Dim wsAB As Worksheet
Dim wsAA As Worksheet
Dim lastColumnAA As Long
Dim colStart As Long
Dim colEnd As Long
Dim i As Long
Set wsAB = ThisWorkbook.Sheets("AB")
Set wsAA = ThisWorkbook.Sheets("AA")
lastColumnAA = wsAA.Cells(1, wsAA.Columns.Count).End(xlToLeft).Column - 1
colStart = 2
colEnd = 5
For i = colStart To lastColumnAA * 4 Step 4
If colEnd > wsAB.Columns.Count Then colEnd = wsAB.Columns.Count
With wsAB.Range(wsAB.Cells(2, colStart), wsAB.Cells(2, colEnd))
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
colStart = colStart + 4
colEnd = colEnd + 4
Next i
End Sub