格式化选择循环中的单元格

时间:2018-11-14 01:43:45

标签: excel vba

我有一个循环宏,它可以根据我选择的单元格来更改一行日期。有没有一种简单的方法来格式化这些单元格而又不使代码复杂化。我公司的政策是保持Macros尽可能简单。

我在代码顶部尝试了以下内容,但这会格式化整个工作表,而不是相关单元格。

cells.HorizontalAlignment = xlRight
cells.NumberFormat = "mmm-yy"

我可以通过下面的方法执行此操作,但是它将100行添加到代码中

cell.Offset(0, 0) = "1-Jul-19"
cell.Offset(0, 0).HorizontalAlignment = xlRight
cell.Offset(0, 0).NumberFormat = "mmm-yy"

是否可以在一行上完成此操作

cell.Offset(0, 0) = "1-Jul-19".HorizontalAlignment = xlRight.NumberFormat = "mmm-yy"

我们将不胜感激。

斯科特


**Sub CHANGE_MONTH_LOOP()
Dim rngMyRange As range
Dim cell As range
Set rngMyRange = Selection
For Each cell In rngMyRange.Cells
cell.Offset(0, 0) = "1-Jul-19"

cell.Offset(0, 1) = "1-Aug-19"

cell.Offset(0, 2) = "1-Sep-19"

Next cell

End Sub**

3 个答案:

答案 0 :(得分:0)

Sub test()


    Dim LastRow As Long
    Dim irow As Long
    Dim jrow As Long
    Dim StartCol As Long
    Dim StartRow As Long



    For Each ws In ActiveWorkbook.Worksheets
        With ws.Select
            LastRow = ws.Range("G" & Rows.Count).End(xlUp).Row

            Set StartDate = Cells.Find(What:="Jul-17", After:=ws.Range("A1"), LookIn:=xlValues _
                        , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
            StartCol = StartDate.Column
            StartRow = StartDate.Row

                For irow = StartRow To LastRow
                    Set Rng = ws.Range(Cells(irow, StartCol), Cells(irow, StartCol + 11))
                        For Each Cell In Rng
                            Cell.Value = DateAdd("yyyy", 1, Cell)
                        Next Cell

                    Rng.HorizontalAlignment = xlRight
                    Rng.NumberFormat = "mmm-yy"

                    irow = irow + 2
                Next irow

                For jrow = StartRow To LastRow
                    Set Rng = ws.Range(Cells(jrow, StartCol + 12), Cells(jrow, StartCol + 12))
                        For Each Cell In Rng
                            Cell.Value = "FY19 Total"
                        Next Cell
                    jrow = jrow + 2
                Next jrow
        End With
    Next ws


End Sub

答案 1 :(得分:0)

PICTURE LINK

这是工作表的外观。基本上,我使用查找/选择,然后在突出显示的单元格上运行宏。我特意一次做一张纸,因为有一些例外。该宏使用cell.offset函数填充相邻的单元格。

理想情况下,我要做的就是将这三个命令放在一行上

cell.Offset(0, 0).HorizontalAlignment = xlRight
cell.Offset(0, 0).NumberFormat = "mmm-yy"
cell.Offset(0, 0) = "1-Jul-19"

即     cell.Offset(0,0).Horizo​​ntalAlignment = xlRight.NumberFormat =“ mmm-yy” .value =“ 19年7月1日”

有人知道这是否可以做到吗?

完整代码:

Sub DATE_MONTHLY_LOOP()

Dim rngMyRange As range
Dim cell As range

Set rngMyRange = Selection

For Each cell In rngMyRange.Cells

cell.Offset(0, 0) = "1-Jul-19"
cell.Offset(0, 0).HorizontalAlignment = xlRight
cell.Offset(0, 0).NumberFormat = "mmm-yy"


cell.Offset(0, 1) = "1-Aug-19"
cell.Offset(0, 1).HorizontalAlignment = xlRight
cell.Offset(0, 1).NumberFormat = "mmm-yy"


cell.Offset(0, 2) = "1-Sep-19"
cell.Offset(0, 2).HorizontalAlignment = xlRight
cell.Offset(0, 2).NumberFormat = "mmm-yy"

cell.Offset(0, 3) = "1-Oct-19"
cell.Offset(0, 3).HorizontalAlignment = xlRight
cell.Offset(0, 3).NumberFormat = "mmm-yy"

cell.Offset(0, 4) = "1-Nov-19"
cell.Offset(0, 4).HorizontalAlignment = xlRight
cell.Offset(0, 4).NumberFormat = "mmm-yy"

cell.Offset(0, 5) = "1-Dec-19"
cell.Offset(0, 5).HorizontalAlignment = xlRight
cell.Offset(0, 5).NumberFormat = "mmm-yy"

cell.Offset(0, 6) = "1-Jan-20"
cell.Offset(0, 6).HorizontalAlignment = xlRight
cell.Offset(0, 6).NumberFormat = "mmm-yy"

cell.Offset(0, 7) = "1-Feb-20"
cell.Offset(0, 7).HorizontalAlignment = xlRight
cell.Offset(0, 7).NumberFormat = "mmm-yy"

cell.Offset(0, 8) = "1-Mar-20"
cell.Offset(0, 8).HorizontalAlignment = xlRight
cell.Offset(0, 8).NumberFormat = "mmm-yy"

cell.Offset(0, 9) = "1-Apr-20"
cell.Offset(0, 9).HorizontalAlignment = xlRight
cell.Offset(0, 9).NumberFormat = "mmm-yy"

cell.Offset(0, 10) = "1-May-20"
cell.Offset(0, 10).HorizontalAlignment = xlRight
cell.Offset(0, 10).NumberFormat = "mmm-yy"

cell.Offset(0, 11) = "1-Jun-20"
cell.Offset(0, 11).HorizontalAlignment = xlRight
cell.Offset(0, 11).NumberFormat = "mmm-yy"

cell.Offset(0, 12) = "FY20 TOTAL"
cell.Offset(0, 12).ColumnWidth = 11.3
cell.Offset(0, 12).HorizontalAlignment = xlRight
cell.Offset(0, 12).NumberFormat = "mmm-yy" 

Next cell

End sub

答案 2 :(得分:0)

再次返回,因此,如果在@alowflyingpig和老板的世界中,更少的行必然意味着更简单,请在下面查看与您的“完整代码”完全相同的代码...但是在更少的行中...

Sub DATE_MONTHLY_LOOP()
Dim cell As Range, i As Integer
For Each cell In Selection.Cells: For i = 0 To 11: With cell.Offset(0, i): .Value = DateAdd("m", 1 + i, 43617): .HorizontalAlignment = xlRight: .NumberFormat = "mmm-yy": End With: Next i:    With cell.Offset(0, 12): .Value = "FY20 TOTAL": .ColumnWidth = 11.3: .HorizontalAlignment = xlRight: .NumberFormat = "mmm-yy": End With: Next cell
End Sub