我想在每个工作表的M2单元格中得到每个工作表的几个特定列的总和..但是每次在每个工作表的那个单元格中得到0 ..其他值正确到来..它是一个非常基本的我知道..仍然无法找到我的代码出了什么问题..
Sub Sort_database()
For i = 2 To 14
Worksheets(i).Activate
lastcol = 1
While Cells(30, lastcol) <> ""
lastcol = lastcol + 1
Wend
hfp = 0
For counter = 4 To lastcol - 1
pnt = 0
For ihfp = 31 To 77
pnt = pnt + Cells(ihfp, counter).Value
Next ihfp
Cells(78, counter).Value = pnt
hfp = hfp + Cells(78, counter).Value
Next counter
Ranges("m2").Value = hfp
Next i
Sheet1.Activate
End Sub
答案 0 :(得分:0)
您的代码中存在一些错误,主要是Ranges("m2").Value = hfp
必须为Range("m2").Value = hfp
见下面的工作
Sub Sort_database()
Dim i As Long, lastCol As Long, counter As Double, ihfp As Long
Dim hfp As Double, pnt As Double
For i = 2 To 14
Worksheets(i).Activate
lastCol = 1
While Cells(30, lastCol) <> ""
lastCol = lastCol + 1
Wend
hfp = 0
For counter = 4 To lastCol - 1
pnt = 0
For ihfp = 31 To 77
pnt = pnt + Cells(ihfp, counter).Value
Next ihfp
.Cells(78, counter).Value = pnt
hfp = hfp + Cells(78, counter).Value
Next counter
Range("m2").Value = hfp
Next i
Sheet1.Activate
End Sub
但是Activate
/ Select
ActiveXX
/ Selection
几乎不是一个好的编码实践,你最好使用完全限定的范围引用,可能与{{1块,如下所示:
With-End With