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

我在iPhone上input了VBA代码,无法validation,因为我没有访问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 

 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 

好的,我只是检查你的代码。

首先,你忘记用If关键字和If语句。

标签()工作正常。 其他潜艇有一个错误:你分配一个Longvariables作为Set number = 3Long不是引用types,所以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 

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