简体   繁体   English

Excel VBA最后一行/列脚本?

[英]Excel VBA Last Row / Column Script?

I'm trying to get the correct ranges for a program that's supposed to find totals on a given sheet.我正在尝试为应该在给定工作表上找到总计的程序获取正确的范围。 I'm struggling a little bit here with the syntax.我在语法上有点挣扎。

I want to always select one column outside of the last column with data, and then autopopulate that column with data starting at Row 4. I'm doing something wrong here.我想总是选择最后一列之外的一列数据,然后用从第 4 行开始的数据自动填充该列。我在这里做错了。

Dim LastColumn As Long
Dim LastRow As Long

LastColumn = ActiveSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
LastRow = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Cells(LastColumn + 1).Select
    ActiveWorkbook.ShowPivotTableFieldList = False
    Selection.Copy
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "Percent Total"
    ActiveCell.FormulaR1C1 = "=RC[-1]/R34C[-1]"
    Selection.AutoFill Destination:=Range(LastColumn & "4" & ":" & LastColumn & LastRow)

I'm getting a Syntax error.我收到语法错误。 AutoFill Method of Range class failed so I know it's that line. Range 类的 AutoFill 方法失败了,所以我知道是那条线。

Here's what I'm looking for.这就是我要找的东西。 In the example, Column AD is created and filled with the data which is simple division.在示例中,列 AD 被创建并填充了简单除法的数据。

Picture of Data数据图片

It is easier to work with Cells here and to avoid using ActiveCell and Select/Selection .在这里使用Cells和避免使用ActiveCellSelect/Selection更容易。 Also there is no need to use AutoFill , since you can write a formula to a multi-cell range in one step.也不需要使用AutoFill ,因为您可以一步将公式写入多单元格范围。

With ActiveSheet
    LastColumn = .Cells.Find("*", _
                     SearchOrder:=xlByColumns, _
                     SearchDirection:=xlPrevious).Column
    LastRow =  .Cells.Find("*", _
                   SearchOrder:=xlByRows, _
                   SearchDirection:=xlPrevious).Row
    .Cells(4, lastColumn + 1).Value = "Percent Total"

    Dim rng As Range
    Set rng = .Range(.Cells(5, lastColumn + 1), .Cells(lastRow, lastColumn + 1))

    rng.FormulaR1C1 = "=RC[-1]/R" & lastRow & "C[-1]"
End With

Maybe something like this ?也许是这样的?

Sub test()
Dim FirstData As Range: Dim div As Range: Dim rg As Range
Set FirstData = Cells(4, Columns.Count).End(xlToLeft).Offset(1, 0)
Set div = FirstData.End(xlDown)
Set rg = Range(FirstData, div.Offset(-1, 0)).Offset(0, 1)
FirstData.Offset(-1, 1).Value = "Percent Total"
rg.Value = "=" & FirstData.Address(0, 0) & "/" & div.Address
End Sub

FirstData variable is to get the cell below "Grand Total". FirstData 变量是获取“总计”下方的单元格。
div variable is to get the cell of the Grand Total for columns of the pivot table. div 变量用于获取数据透视表列的总计单元格。
rg variable is to get the range which is going to be filled with formula. rg 变量用于获取将要填充公式的范围。

The formula is not using R1C1, but cell address.该公式不是使用 R1C1,而是使用单元格地址。

If you don't use "Show grand total for columns" in your pivot table option, then the rg variable like this : Set rg = Range(FirstData, div).Offset(0, 1)如果您不在数据透视表选项中使用“显示列总计”,则 rg 变量如下所示: Set rg = Range(FirstData, div).Offset(0, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM