excel中vba用for语句对多个单元格赋值的问题
新手求大神指教将表sheet1的C2至C32单元格依次赋值:C2的值为表sheet2的i4到i35之和,C3的值为表sheet3的i4到i35之和,依次类推。Sheet1...
新手求大神指教
将表sheet1的C2至C32单元格依次赋值:C2的值为表sheet2的i4到i35之和,C3的值为表sheet3的i4到i35之和,依次类推。
Sheet1.Range("c2") = Application.WorksheetFunction.Sum(Sheet2.Range("i4:i35"))
Sheet1.Range("c3") = Application.WorksheetFunction.Sum(Sheet3.Range("i4:i35"))
Sheet1.Range("c4") = Application.WorksheetFunction.Sum(Sheet4.Range("i4:i35"))
......
Sheet1.Range("c32") = Application.WorksheetFunction.Sum(Sheet32.Range("i4:i35"))
上述语句如何用for语句简化?我尝试了定义一个变量z,代码如下:
Private Sub 试验1_Click()
Dim z As Integer
For z = 2 To 32
Sheet1.Range("c&z") = Application.WorksheetFunction.Sum(Sheets(CStr(z)).Range("i4:i35"))
Next
End Sub
运行后提示下标越界,该如何修改,问题出在哪里,该如何修改?求指教。 展开
将表sheet1的C2至C32单元格依次赋值:C2的值为表sheet2的i4到i35之和,C3的值为表sheet3的i4到i35之和,依次类推。
Sheet1.Range("c2") = Application.WorksheetFunction.Sum(Sheet2.Range("i4:i35"))
Sheet1.Range("c3") = Application.WorksheetFunction.Sum(Sheet3.Range("i4:i35"))
Sheet1.Range("c4") = Application.WorksheetFunction.Sum(Sheet4.Range("i4:i35"))
......
Sheet1.Range("c32") = Application.WorksheetFunction.Sum(Sheet32.Range("i4:i35"))
上述语句如何用for语句简化?我尝试了定义一个变量z,代码如下:
Private Sub 试验1_Click()
Dim z As Integer
For z = 2 To 32
Sheet1.Range("c&z") = Application.WorksheetFunction.Sum(Sheets(CStr(z)).Range("i4:i35"))
Next
End Sub
运行后提示下标越界,该如何修改,问题出在哪里,该如何修改?求指教。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询