摆脱重复的代码(过程太大错误)

时间:2019-06-24 17:39:44

标签: excel vba

我想使用“ For Each”或任何其他有用的方法来消除这些重复代码的大块

我只重复执行约70次代码块,但是由于它包含多个动态值,因此我很难使用“ For Each”

Public Sub ToTextBox()
Dim wsA As Worksheet
Set wsA = Sheets("Compute")

'1
With Sheets("Slide Data").Shapes("TextBox 1")
    .TextFrame.Characters.Text = wsA.Range("b" & Range("L2")).Value & Chr(10) & wsA.Range("c" & Range("L2")).Value
    .TextFrame.Characters(1, 7).Font.Bold = True
    .TextFrame.Characters(1, 7).Font.Size = 7
    .TextFrame.Characters(1, 7).Font.Name = "Verdana"
    .TextFrame.HorizontalAlignment = xlHAlignCenter
End With

    Sheets("Slide Data").Shapes("TextBox 1A").TextFrame.Characters.Text = Sheet3.Range("a" & Range("L2")).Value
    Sheets("Slide Data").Shapes("TextBox 1B").TextFrame.Characters.Text = Sheet3.Range("k" & Range("L2")).Value
    Sheets("Slide Data").Shapes("TextBox 1C").TextFrame.Characters.Text = Sheet3.Range("j" & Range("L2")).Value

'2   
With Sheets("Slide Data").Shapes("TextBox 2")
    .TextFrame.Characters.Text = wsA.Range("b" & Range("L2") + 1).Value & Chr(10) & wsA.Range("c" & Range("L2") + 1).Value
    .TextFrame.Characters(1, 7).Font.Bold = True
    .TextFrame.Characters(1, 7).Font.Size = 7
    .TextFrame.Characters(1, 7).Font.Name = "Verdana"
    .TextFrame.HorizontalAlignment = xlHAlignCenter
End With

    Sheets("Slide Data").Shapes("TextBox 2A").TextFrame.Characters.Text = Sheet3.Range("a" & Range("L2") + 1).Value
    Sheets("Slide Data").Shapes("TextBox 2B").TextFrame.Characters.Text = Sheet3.Range("k" & Range("L2") + 1).Value
    Sheets("Slide Data").Shapes("TextBox 2C").TextFrame.Characters.Text = Sheet3.Range("j" & Range("L2") + 1).Value

'3    
With Sheets("Slide Data").Shapes("TextBox 3")
    .TextFrame.Characters.Text = wsA.Range("b" & Range("L2") + 2).Value & Chr(10) & wsA.Range("c" & Range("L2") + 2).Value
    .TextFrame.Characters(1, 7).Font.Bold = True
    .TextFrame.Characters(1, 7).Font.Size = 7
    .TextFrame.Characters(1, 7).Font.Name = "Verdana"
    .TextFrame.HorizontalAlignment = xlHAlignCenter
End With

    Sheets("Slide Data").Shapes("TextBox 3A").TextFrame.Characters.Text = Sheet3.Range("a" & Range("L2") + 2).Value
    Sheets("Slide Data").Shapes("TextBox 3B").TextFrame.Characters.Text = Sheet3.Range("k" & Range("L2") + 2).Value
    Sheets("Slide Data").Shapes("TextBox 3C").TextFrame.Characters.Text = Sheet3.Range("j" & Range("L2") + 2).Value

'and 72 others


End Sub

寻找一种循环方法来压缩此代码

1 个答案:

答案 0 :(得分:-1)

打开它:

With Sheets("Slide Data").Shapes("TextBox 1")
    .TextFrame.Characters.Text = wsA.Range("b" & Range("L2")).Value & Chr(10) & wsA.Range("c" & Range("L2")).Value
    .TextFrame.Characters(1, 7).Font.Bold = True
    .TextFrame.Characters(1, 7).Font.Size = 7
    .TextFrame.Characters(1, 7).Font.Name = "Verdana"
    .TextFrame.HorizontalAlignment = xlHAlignCenter
End With

对此:

Sub ShapeStuff(number as Long, sourceSheet as Worksheet)
    With Sheets("Slide Data").Shapes("TextBox " & cstr(number))
        .TextFrame.Characters.Text = sourceSheet.Range("b" & sourceSheet.Range("L2") + number - 1).Value & Chr(10) & sourceSheet.Range("c" & Range("L2") + number - 1).Value
        .TextFrame.Characters(1, 7).Font.Bold = True
        .TextFrame.Characters(1, 7).Font.Size = 7
        .TextFrame.Characters(1, 7).Font.Name = "Verdana"
        .TextFrame.HorizontalAlignment = xlHAlignCenter
    End With
End Sub

然后分别调用ShapeStuff(1, wsA)ShapeStuff(2, wsA)ShapeStuff(3, wsA)