简体   繁体   English

使用另一个工作表中单元格的位置获取最后一个单元格的值

[英]Getting the Value of the Last Cell Using the Location of a Cell in Another Sheet

I have a sheet of raw data with the average located at the bottom, I want to run through every column on a summary sheet, and place the average given on the raw data sheet below.我有一张原始数据表,平均值位于底部,我想遍历汇总表上的每一列,并将给出的平均值放在下面的原始数据表中。 what I have:我有的:

On Error Resume Next
Application.DisplayAlerts = False
Sheets.Add After:=Worksheets("Raw PivotTable")
ActiveSheet.Name = "Summary"
Application.DisplayAlerts = True
Set SSheet = Worksheets("Summary")
Set RSheet = Worksheets("Raw PivotTable")
Set PSheet = Worksheets("PivotTable")
Dim C As Integer
Dim PSheetVal As Long

C = 2

RSheet.Range("B1:" & ActiveSheet.Range(Cells(1, Columns.Count).End(xlToRight).Address).End(xlUp).Address).Copy
ActiveSheet.Paste Destination:=SSheet.Range("A1")

For Each cell In SSheet.Range("A1:" & SSheet.Range("A1").End(xlToRight).Address)

    PSheetVal = PSheet.Range(Cells(2, C)).End(xlDown).Value
    ' ^^^^ this is causing my issues specifically.

    cell.Offset(1, 0).Value = PSheetVal

    C = C + 1
    Next

For me it is putting 0s instead of the actual values.对我来说,它是用 0 代替实际值。

汇总表

桌子

Instead of the copy-paste, you might do the following:您可以执行以下操作,而不是复制粘贴:

cell. ... .Offset(1, 0).Value = PSheet. ... .End(xlDown).Value

I fixed the issue, Instead of trying to find an exact cell I selected the entire column.我解决了这个问题,而不是试图找到一个确切的单元格,我选择了整个列。 and selected the last cell: My code:并选择了最后一个单元格:我的代码:

 For Each cell In SSheet.Range("A1:" & SSheet.Range("A1").End(xlToRight).Address)

        With Sheets(PSheet)
        PSheetVal = PSheet.Columns(C).End(xlDown).Value
        End With

        With Sheets(SSheet)
        cell.Offset(1, 0).Value = PSheetVal
        End With

        C = C + 1
        Next

Still not quite sure how to select a very specific cell but for my purposes that is not needed, would be helpful if anyone could point me in the right direction for the future!仍然不太确定如何选择一个非常具体的单元格,但出于我的目的,这不是必需的,如果有人能为我指明未来的正确方向,将会很有帮助!

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

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