2013-10-18 48 views
1

嗨,我想将7个单元格合并到第1行的所有方式。而不是Clickng A1按Shift键,然后右箭头键六次我想我可以通过宏。我写了以下内容,但不确定如何将7添加到字母引用中,特别是一旦我进入AA1的最后七个结尾,然后它开始AA2并继续。下面的代码实现了我想要的,但是我怎样才能引入变量和循环来将字母存储为int,然后添加六个字符,然后再转换回字符串并继续到下一个集合?我会怎么写While (End of row has not been reached)添加到列字母excel

Range("A1:G1").Select 
With Selection 
    .HorizontalAlignment = xlGeneral 
    .VerticalAlignment = xlBottom 
    .WrapText = False 
    .Orientation = 0 
    .AddIndent = False 
    .IndentLevel = 0 
    .ShrinkToFit = False 
    .ReadingOrder = xlContext 
    .MergeCells = True 
End With 

Range("H1:N1").Select 
    With Selection 
    .HorizontalAlignment = xlGeneral 
    .VerticalAlignment = xlBottom 
    .WrapText = False 
    .Orientation = 0 
    .AddIndent = False 
    .IndentLevel = 0 
    .ShrinkToFit = False 
    .ReadingOrder = xlContext 
    .MergeCells = True 
End With 
+2

东南合并的细胞上的某处被描述为“魔鬼的发明,试图超越耐力”。 – pnuts

+0

跨所选中心可能是一种避免合并的方法。 – pnuts

回答

1

这是你正在尝试?

'R1 is the row of the first cell 
'C1 is the column of the first cell 
'R2 is the row of the second cell 
'C2 is the column of the second cell 

Sub Sample() 
    Dim Rng As Range 
    Dim ws As Worksheet 
    Dim R1 As Long, C1 As Long 
    Dim R2 As Long, C2 As Long 

    Set ws = ThisWorkbook.Sheets("Sheet1") 

    R1 = 1: C1 = 1 
    R2 = 1: C2 = C1 + 6 

    With ws 
     Set Rng = .Range(.Cells(R1, C1), .Cells(R2, C2)) 
     Application.DisplayAlerts = False 
     Rng.Merge 
     Application.DisplayAlerts = True 
    End With 
End Sub 

编辑:您可能还会发现THIS链接有趣。

+0

你能解决我问题的循环方面吗? –

+0

当然:)得到该列中的最后一行,并使用“For Next”循环:)要获取最后一行,请参阅此链接http://stackoverflow.com/questions/11169445/error-finding-last-used-细胞在-VBA –