我在这里有一个vba代码,其中最后5行不会显示在我的Excel电子表格中。 我做错了什么?
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Set ws1 = ThisWorkbook.Worksheets("Count and Avg.")
Set ws2 = ThisWorkbook.Worksheets("Individual Board Info")
Set ws3 = ThisWorkbook.Worksheets("Graph Info")
Dim i As Integer, ii As Integer
Dim a As Integer, b As Integer
a = ws2.UsedRange.Rows.Count
b = ws1.UsedRange.Rows.Count
For i = 2 To a
ws2.Cells(i, 4) = Date - ws2.Cells(i, 3)
Next i
ws1.Cells(1, b + 1) = Date
ws1.Cells(2, b + 1) = ws2.UsedRange.Rows.Count - 1
ws1.Cells(3, b + 1) = Application.WorksheetFunction.Max(ws2.Columns(4))
ws1.Cells(4, b + 1) = Application.WorksheetFunction.Min(ws2.Columns(4))
ws1.Cells(5, b + 1) = Application.WorksheetFunction.Average(ws2.Columns(4))
答案 0 :(得分:1)
正如@Axel Ritcher在评论中所提到的,Cells
函数采用以下格式:Cells([row reference], [column reference])
您应该只需要切换引用,以便现在可以读取:
ws1.Cells(b + 1, 1) = Date
ws1.Cells(b + 1, 2) = ws2.UsedRange.Rows.Count - 1
ws1.Cells(b + 1, 3) = Application.WorksheetFunction.Max(ws2.Columns(4))
ws1.Cells(b + 1, 4) = Application.WorksheetFunction.Min(ws2.Columns(4))
ws1.Cells(b + 1, 5) = Application.WorksheetFunction.Average(ws2.Columns(4))
答案 1 :(得分:1)
b = ws1.UsedRange.Rows.Count
这看起来像你正在尝试获取ws1中最后一个使用的行,在这种情况下我建议不要使用此方法,因为使用的范围不一定是出现的范围使用(即你可以看到的细胞)
尝试使用类似的东西:
b = ws1.Cells.Find(What:="*", After:=ws1.Cells(1), Lookat:=xlPart, _
LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False).Row
我怀疑使用的范围可能会变得臃肿,因此您的数据实际上会在表单中写得比预期的要多得多。
答案 2 :(得分:0)
我没有足够的声誉来添加评论,但你可以在即时窗口中执行此操作并发布答案吗?
Set ws1 = ThisWorkbook.Worksheets("Count and Avg.")
[Press Enter]
Set ws2 = ThisWorkbook.Worksheets("Individual Board Info")
[Press Enter]
?ws1.UsedRange.Rows.Count
[Press Enter]
?ws2.UsedRange.Rows.Count
[Press Enter]