Excel VBA宏:直到索引模式正确(从0或1开始)

时间:2013-02-20 05:49:57

标签: excel excel-vba vba

我在iPhone上键入了VBA代码但无法验证,因为我无法访问Excel。

我的指数(sp?)是否正确?

Option Explicit
' label the worksheet
Sub Labels()
    With Worksheets("Sheet1")
        .Range("A1") = "Save This Much Money By Upgrading With Me Right Now"
        .Range("A1").Font.Bold = True
        .Range("A1:F1").Merge
        .Range("A2") = "Basic Phones"
        .Range("B2") = "Smart Phones"
        .Range("C2") = "Cross Segment"
        .Range("D2") = "$36 Activation Fee"
        .Range("E2") = "Basic Phones Credit"
        .Range("F2") = "Smart Phones Credit"
        .Range("G2") = "Total Bill Credit"
        .Range("H2") = "Grand Total Savings"
        .Range("A2:H2").Font.Bold = True
    End With
End Sub

' make # of basic starting w/ A3
Sub MakeBasicPhonesColumn()
    Dim number As Long, basicPhones As Long, counter As Long
    Set number = 3
    Set basicPhones = 0
    Set counter = 3

    Do Until basicPhones > 20
        If counter > 42
            counter = 1
            basicPhones = basicPhones + 1
        End If
        Cells(number, 1).Value = basicPhones
        number = number + 1
        counter = counter + 1
    Loop
End Sub

' make # of smartphones starting with B3
Sub MakeSmartPhonesColumn()
    Dim counter As Long, number As Long, smartPhones As Long, loops As Long
    Set counter = 3
    Set number = 3
    Set smartPhones = 1
    Set loops = 0

    Do Until loops > 21
        If counter > 42
            counter = 1
            smartPhones = 0
            loops = loops + 1
        End If
        Cells(number, 2).Value = smartPhones
        Cells(number + 1, 2).Value = smartPhones
        number = number + 2
        counter = counter + 2
        smartPhones = smartPhones + 1
    Loop
End Sub

' make Cross Segment Yes or No column
Sub MakeCrossSegmentColumn()
    Dim counter As Long, number As Long, bool As Boolean, loops As Long
    Set counter = 3
    Set number = 3
    Set bool = 1
    Set loops = 0

    Do Until loops > 21
        If counter > 42
            counter = 1
            loops = loops + 1
        End If
        Cells(number, 3).Value = bool
        If bool = 1
            bool = 0
        Else: bool = 1
        End If
        counter = counter + 1
        number = number + 1
    Loop
End Sub

' make activation fee savings column
Sub MakeActivationFeeSavingsColumn()
    Dim counter As Long, number As Long, loops As Long, activationFee As Long
    Set counter = 3
    Set number = 3
    Set loops = 0
    Set activationFee = 0

    Do Until loops > 21
        If counter > 42
            counter = 1
            loops = loops + 1
        End If
        activationFee = (Cells(number, 1).Value + Cells(number, 2).Value) * 36
        Cells(number, 4).Value = activationFee
        number = number + 1
        counter = counter + 1
    Loop
End Sub

' make basic, smart, and total cross segment credit columns
Sub MakeCrossSegmentColumns()
    Dim loops As Long, counter As Long, number As Long, basicCrossSegment As Long, smartCrossSegment As Long
    Set loops = 0
    Set counter = 3
    Set number = 3
    Set basicCrossSegment = 0
    Set smartCrossSegment = 0

    Do Until loops > 21
        If counter > 42
            counter = 1
            loops = loops + 1
        End If
        If Cells(number,3).Value = 1
            basicCrossSegment = Cells(number, 1).Value * 25
            smartCrossSegment = Cells(number, 2).Value * 50
            Cells(number, 5).Value = basicCrossSegment
            Cells(number, 6).Value = smartCrossSegment
            Cells(number, 7).Value = basicCrossSegment + smartCrossSegment
        End If
        counter = counter + 1
        number = number + 1
    Loop
End Sub

' make grand total savings column
Sub MakeGrandTotalSavingsColumn()
    Dim counter As Long, loops As Long, number As Long, activationFee As Long, crossSegment As Long
    Set counter = 3
    Set loops = 0
    Set number = 3
    Set activationFee = 0
    Set crossSegment = 0

    Do Until loops > 21
        If counter > 42
            counter = 1
            loops = loops + 1
        End If
        activationFee = Cells(number, 4).Value
        crossSegment = Cells(number, 7).Value
        Cells(number, 8).Value = activationFee + crossSegment
        number = number + 1
        counter = counter + 1
    Loop
End Sub

2 个答案:

答案 0 :(得分:0)

Option Explicit
' label the worksheet
Sub Labels()
    With Worksheets("Sheet1")
        .Range("A1") = "Save This Much Money By Upgrading With Me Right Now"
        .Range("A1").Font.Bold = True
        .Range("A1:F1").Merge
        .Range("A2") = "Basic Phones"
        .Range("B2") = "Smart Phones"
        .Range("C2") = "Cross Segment"
        .Range("D2") = "$36 Activation Fee"
        .Range("E2") = "Basic Phones Credit"
        .Range("F2") = "Smart Phones Credit"
        .Range("G2") = "Total Bill Credit"
        .Range("H2") = "Grand Total Savings"
        .Range("A2:H2").Font.Bold = True
    End With
End Sub

' make # of basic starting w/ A3
Sub MakeBasicPhonesColumn()
    Dim number As Long, basicPhones As Long, counter As Long
    number = 3
    basicPhones = 0
    counter = 3

    Do Until basicPhones > 20
        If counter > 42 Then
            counter = 1
            basicPhones = basicPhones + 1
        End If
        Cells(number, 1).Value = basicPhones
        number = number + 1
        counter = counter + 1
    Loop
End Sub

' make # of smartphones starting with B3
Sub MakeSmartPhonesColumn()
    Dim counter As Long, number As Long, smartPhones As Long, loops As Long
    counter = 3
    number = 3
    smartPhones = 1
    loops = 0

    Do Until loops > 21
        If counter > 42 Then
            counter = 1
            smartPhones = 0
            loops = loops + 1
        End If
        Cells(number, 2).Value = smartPhones
        Cells(number + 1, 2).Value = smartPhones
        number = number + 2
        counter = counter + 2
        smartPhones = smartPhones + 1
    Loop
End Sub

' make Cross Segment Yes or No column
Sub MakeCrossSegmentColumn()
    Dim counter As Long, number As Long, bool As Boolean, loops As Long
    counter = 3
    number = 3
    bool = 1
    loops = 0

    Do Until loops > 21
        If counter > 42 Then
            counter = 1
            loops = loops + 1
        End If
        Cells(number, 3).Value = bool
        If bool = 1 Then
            bool = 0
        Else: bool = 1
        End If
        counter = counter + 1
        number = number + 1
    Loop
End Sub

' make activation fee savings column
Sub MakeActivationFeeSavingsColumn()
    Dim counter As Long, number As Long, loops As Long, activationFee As Long
     counter = 3
     number = 3
     loops = 0
     activationFee = 0

    Do Until loops > 21
        If counter > 42 Then
            counter = 1
            loops = loops + 1
        End If
        activationFee = (Cells(number, 1).Value + Cells(number, 2).Value) * 36
        Cells(number, 4).Value = activationFee
        number = number + 1
        counter = counter + 1
    Loop
End Sub

' make basic, smart, and total cross segment credit columns
Sub MakeCrossSegmentColumns()
    Dim loops As Long, counter As Long, number As Long, basicCrossSegment As Long, smartCrossSegment As Long
     loops = 0
     counter = 3
     number = 3
     basicCrossSegment = 0
     smartCrossSegment = 0

    Do Until loops > 21
        If counter > 42 Then
            counter = 1
            loops = loops + 1
        End If
        If Cells(number, 3).Value = 1 Then
            basicCrossSegment = Cells(number, 1).Value * 25
            smartCrossSegment = Cells(number, 2).Value * 50
            Cells(number, 5).Value = basicCrossSegment
            Cells(number, 6).Value = smartCrossSegment
            Cells(number, 7).Value = basicCrossSegment + smartCrossSegment
        End If
        counter = counter + 1
        number = number + 1
    Loop
End Sub

' make grand total savings column
Sub MakeGrandTotalSavingsColumn()
    Dim counter As Long, loops As Long, number As Long, activationFee As Long, crossSegment As Long
     counter = 3
     loops = 0
     number = 3
     activationFee = 0
     crossSegment = 0

    Do Until loops > 21
        If counter > 42 Then
            counter = 1
            loops = loops + 1
        End If
        activationFee = Cells(number, 4).Value
        crossSegment = Cells(number, 7).Value
        Cells(number, 8).Value = activationFee + crossSegment
        number = number + 1
        counter = counter + 1
    Loop
End Sub

答案 1 :(得分:0)

好的,我刚检查了你的代码。

首先,您忘记将Then关键字与If语句一起使用。

标签()工作正常。 其他潜艇有错误:您将Long变量指定为Set number = 3Long不是引用类型,因此Set关键字是多余的。

我删除了错误,它对我来说效果很好。这是代码:

Option Explicit
' label the worksheet
Sub Labels()
    With Worksheets("Sheet1")
        .Range("A1") = "Save This Much Money By Upgrading With Me Right Now"
        .Range("A1").Font.Bold = True
        .Range("A1:F1").Merge
        .Range("A2") = "Basic Phones"
        .Range("B2") = "Smart Phones"
        .Range("C2") = "Cross Segment"
        .Range("D2") = "$36 Activation Fee"
        .Range("E2") = "Basic Phones Credit"
        .Range("F2") = "Smart Phones Credit"
        .Range("G2") = "Total Bill Credit"
        .Range("H2") = "Grand Total Savings"
        .Range("A2:H2").Font.Bold = True
    End With
End Sub

' make # of basic starting w/ A3
Sub MakeBasicPhonesColumn()
 Dim number As Long, basicPhones As Long, counter As Long
 number = 3
 basicPhones = 0
 counter = 3

Do Until basicPhones > 20
 If counter > 42 Then
  counter = 1
  basicPhones = basicPhones + 1
 End If
 Cells(number, 1).Value = basicPhones
 number = number + 1
 counter = counter + 1
Loop
End Sub

' make # of smartphones starting with B3
Sub MakeSmartPhonesColumn()
Dim counter As Long, number As Long, smartPhones As Long, loops As Long
counter = 3
number = 3
smartPhones = 1
loops = 0

Do Until loops > 21
 If counter > 42 Then
  counter = 1
  smartPhones = 0
  loops = loops + 1
 End If
 Cells(number, 2).Value = smartPhones
 Cells(number + 1, 2).Value = smartPhones
 number = number + 2
 counter = counter + 2
 smartPhones = smartPhones + 1
Loop
End Sub

' make Cross Segment Yes or No column
Sub MakeCrossSegmentColumn()
Dim counter As Long, number As Long, bool As Boolean, loops As Long
counter = 3
number = 3
bool = 1
loops = 0

Do Until loops > 21
 If counter > 42 Then
  counter = 1
  loops = loops + 1
 End If
 Cells(number, 3).Value = bool
 If bool = 1 Then
  bool = 0
 Else: bool = 1
 End If
 counter = counter + 1
 number = number + 1
Loop
End Sub

' make activation fee savings column
Sub MakeActivationFeeSavingsColumn()
Dim counter As Long, number As Long, loops As Long, activationFee As Long
counter = 3
number = 3
loops = 0
activationFee = 0

Do Until loops > 21
 If counter > 42 Then
  counter = 1
  loops = loops + 1
 End If
 activationFee = (Cells(number, 1).Value + Cells(number, 2).Value) * 36
 Cells(number, 4).Value = activationFee
 number = number + 1
 counter = counter + 1
Loop
End Sub

' make basic, smart, and total cross segment credit columns
Sub MakeCrossSegmentColumns()
Dim loops As Long, counter As Long, number As Long, basicCrossSegment As Long, smartCrossSegment As Long
loops = 0
counter = 3
number = 3
basicCrossSegment = 0
smartCrossSegment = 0

Do Until loops > 21
 If counter > 42 Then
  counter = 1
  loops = loops + 1
 End If
 If Cells(number, 3).Value = 1 Then
  basicCrossSegment = Cells(number, 1).Value * 25
  smartCrossSegment = Cells(number, 2).Value * 50
  Cells(number, 5).Value = basicCrossSegment
  Cells(number, 6).Value = smartCrossSegment
  Cells(number, 7).Value = basicCrossSegment + smartCrossSegment
 End If
 counter = counter + 1
 number = number + 1
Loop
End Sub

' make grand total savings column
Sub MakeGrandTotalSavingsColumn()
Dim counter As Long, loops As Long, number As Long, activationFee As Long, crossSegment As Long
counter = 3
loops = 0
number = 3
activationFee = 0
crossSegment = 0

Do Until loops > 21
 If counter > 42 Then
  counter = 1
  loops = loops + 1
 End If
 activationFee = Cells(number, 4).Value
 crossSegment = Cells(number, 7).Value
 Cells(number, 8).Value = activationFee + crossSegment
 number = number + 1
 counter = counter + 1
Loop
End Sub

虽然我不能保证程序中没有逻辑错误。