如何用循环调用第二个代码?

时间:2018-02-13 20:00:04

标签: vba excel-vba excel

我是VBA初学者,我的代码显然有问题。

  

错误1004 - 运行时错误

当代码通过cells(i,1).value= formatcell

请劝告? 谢谢

Sub firstcode()
Dim formatcell As Integer
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
formatcell = Cells(1, 1).Value
For i = 0 To lastrow

If formatcell < 20 Then

Call SecondCode(True, "Arial", 22)
Else
Call SecondCode(False, "Times", 30)

End If

Cells(i, 1).Value = formatcell
Next i
End Sub

Private Sub SecondCode(BoldValue As Boolean, NameValue As String, SizeValue)
With ActiveCell.Font
.Bold = BoldValue
.name = NameValue
.Size = SizeValue
End With
End Sub

3 个答案:

答案 0 :(得分:2)

更简单的版本?

Sub FormatCells()
    Dim formatcell As Range, lastRow As Long, cl As Range

    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Set formatcell = Range("A1")

    For Each cl In Range("A2:A" & lastRow)
        If formatcell < 20 Then
            cl.Font.Bold = True
            cl.Font.Name = "Arial"
            cl.Font.Size = 22
        Else
            cl.Font.Bold = False
            cl.Font.Name = "Times"
            cl.Font.Size = 30
        End If

        cl.Value = formatcell
    Next cl
End Sub

修改后的代码

Sub Test()
    Dim switchVal As Range, lastRow As Long, cl As Range

    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Set switchVal = Range("A1")

    For Each cl In Range("A2:A" & lastRow)
        If cl < switchVal Then
            FormatCell cl, True, "Arial", 22
        Else
            FormatCell cl, False, "Times", 30
        End If
    Next cl
End Sub

Sub FormatCell(cl As Range, bold As Boolean, font As String, size As Integer)
    cl.font.bold = bold
    cl.font.Name = font
    cl.font.size = size
End Sub

答案 1 :(得分:0)

我已经重构了下面的代码,以避免在保留所有内容时遇到的许多陷阱。我将整体结构保持不变,但使其更清晰,并评论了为什么我改变了我改变的内容。

Option Explicit

Sub firstcode()

    Dim formatcell As Integer
    Dim lastrow As Long
    Dim ws As Worksheet

    'always declare and work with parent object (otherwise Cells may refer to a worksheet you don't expect it to
    Set ws = ThisWorkbook.Worksheets("Sheet1") 'change as needed

    With ws

        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        formatcell = .Cells(1, 1).Value

        Dim i as Long
        For i = 1 To lastrow 'there is no row 0 in excel so start with 1

            Select Case formatcell < 20 'select case easier to read for me
                Case True: SecondCode .Cells(i, 1), True, "Arial", 22 'writing call is not needed
                Case False: SecondCode .Cells(i, 1), True, "Times", 30
            End Select

            .Cells(i, 1).Value = formatcell

        Next i

    End With

End Sub

Private Sub SecondCode(rng As Range, BoldValue As Boolean, NameValue As String, SizeValue As Integer)

    'again work directly with the object. using `ActiveCell can cause undesireable errors
    With rng.Font
        .Bold = BoldValue
        .Name = NameValue
        .Size = SizeValue
    End With

End Sub

答案 2 :(得分:0)

也许你是在这之后

Sub firstcode()
    Dim formatcell As Integer
    Dim lastrow As Long

    With WorkSheets("mySheetName") ‘ change mySheetName to your actual sheet name
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For i = 1 To lastrow
            formatcell = .Cells(i, 1).Value
            If formatcell < 20 Then
                SecondCode  .Cells(i, 1), True, "Arial", 22
            Else
                SecondCode  .Cells(i, 1), False, "Times", 30
            End If
        Next i
    End With
End Sub

Private Sub SecondCode(cell As Range, BoldValue As Boolean, NameValue As String, SizeValue)
    With cell.Font
        .Bold = BoldValue
        .name = NameValue
        .Size = SizeValue
    End With
End Sub