如何在excel中为许多单元格添加循环

时间:2016-10-11 03:34:52

标签: excel vba excel-vba macros

Sub table()

Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets

    ws.Cells(1, "C") = Format(ws.Cells(1, "A"), "yyyymmdd") & Format(ws.Cells(1, "B"), "hhmmss")
    ws.Cells(1, "C").NumberFormat = "0"
    Next ws

End Sub

这是我的单细胞代码(第一个细胞)。我想通过为i设置循环来为多个单元格运行它。

2 个答案:

答案 0 :(得分:1)

Sub table()
Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        For i = 1 To 500 ' <-- Edit these numbers to modify the size of the iteration
            ws.Cells(i, "C") = Format(ws.Cells(i, "A"), "yyyymmdd") & Format(ws.Cells(i, "B"), "hhmmss")
            ws.Cells(i, "C").NumberFormat = "0"
        Next i
    Next ws
End Sub

答案 1 :(得分:0)

做类似的事情:

For Each ws In ActiveWorkbook.Worksheets
   for each cell in ws.range("C1:C1000")
      cell = Format(ws.Cells(1, "A"), "yyyymmdd") & Format(ws.Cells(1, "B"), "hhmmss")
      cell.NumberFormat = "0"
   next cell
Next ws