简体   繁体   English

有没有办法在新工作表上打印列中的最后一行?

[英]Is there a way to print the last row in a column on a new sheet?

im trying to find the last row in a column with in each sheet, so if the last column in sheet1 is BD30 then i want that value to be printed on the sheet i created to get the values and input them there.我试图在每张表中找到一列中的最后一行,所以如果 sheet1 中的最后一列是 BD30,那么我希望将该值打印在我创建的表上以获取值并在那里输入它们。

    Dim wb As Workbook
    Dim sht As Worksheet
    Dim LastColumn As Integer
    
     Set wb = ActiveWorkbook
    Sheets.Add.Name = "Data"
    Dim shtMain As Worksheet
    Set shtMain = wb.Sheets("Data")
    
    Dim LastRow As Integer
    LastRow = shtMain.Range("A1").CurrentRegion.Rows.Count
    
    Dim c As Range

    For Each sht In wb.Worksheets
        If sht.Name <> shtMain.Name Then
            LastColumn = sht.UsedRange.Columns(sht.UsedRange.Columns.Count).Column
            With shtMain.Range("A1", "A" & LastRow)
                Set c = .Find(sht.Name, LookIn:=xlValues)
                If Not c Is Nothing Then
                    c.Offset(0, 1).Value = LastColumn
                Else
                    With shtMain.Range("A" & LastRow)
                        .Offset(1, 0).Value = sht.Name
                        .Offset(1, 1).Value = LastColumn
                        LastRow = LastRow + 1
                    End With
                End If
            End With
        End If
    Next sht
End Sub

this is the code i have it works, but the problem is that it counts how many rows there are so for example in sheet1 there are 55 rows it will show 55 and thats not what i want, i want to show me the value of the last row in column that contains data.这是我拥有它的代码,但问题是它计算有多少行,例如在 sheet1 中有 55 行它将显示 55,这不是我想要的,我想告诉我包含数据的列中的最后一行。

this is what i get when i run my code.这是我运行代码时得到的。 it counts the rows but i want it to paste the last row value not count the rows.它计算行数,但我希望它粘贴最后一行值而不计算行数。 so for example if the last row is BB40 then i want that to show.因此,例如,如果最后一行是 BB40,那么我希望它显示出来。

I think this is what you want.我想这就是你想要的。

Sub Test()        

    Dim wb As Workbook
    Dim sht As Worksheet
    Dim SourceRow As Integer
    Dim SourceCol As Integer
    Dim SourceValue As Variant
    Dim LastRow As Integer
    Dim shtMain As Worksheet
    Dim c As Range
    
     Set wb = ActiveWorkbook
    Sheets.Add.Name = "Data"
    Set shtMain = wb.Sheets("Data")
    
    LastRow = shtMain.Range("A1").CurrentRegion.Rows.Count
    

    For Each sht In wb.Worksheets
        If sht.Name <> shtMain.Name Then
            With sht.UsedRange
                SourceRow = .Rows(.Rows.Count).Row
                SourceCol = .Columns(.Columns.Count).Column
                SourceValue = .Cells(SourceRow, SourceCol).Value
            End With
            With shtMain.Range("A1", "A" & LastRow)
                Set c = .Find(sht.Name, LookIn:=xlValues)
                If Not c Is Nothing Then
                    c.Offset(0, 1).Value = LastColumn
                Else
                    With shtMain.Range("A" & LastRow)
                        .Offset(1, 0).Value = sht.Name
                        .Offset(1, 1).Value = SourceValue
                        LastRow = LastRow + 1
                    End With
                End If
            End With
        End If
    Next sht
End Sub

BTW, as a question of style, I would stongly recommend you move all you Dim statements up to the top of the function.顺便说一句,作为风格问题,我强烈建议您将所有 Dim 语句移到 function 的顶部。

List of Last Cell Values最后一个单元格值列表

  • It is assumed that the code is in the relevant workbook.假设代码在相关工作簿中。
  • Adjust the values in the constants section.调整常量部分中的值。
Option Explicit

Sub listLastCells()
    
    ' Constants
    Const dName As String = "Data"
    Const dFirst As String = "A2"
    
    ' Workbook
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' Delete Destination Worksheet (if it exists).
    On Error Resume Next
    Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
    On Error GoTo 0
    If Not dws Is Nothing Then
        Application.DisplayAlerts = False
        dws.Delete
        Application.DisplayAlerts = True
    End If
    
    ' Define Data Array.
    Dim Data As Variant: ReDim Data(1 To wb.Worksheets.Count, 1 To 2)
    
    ' Declare additional variables.
    Dim sws As Worksheet
    Dim sCell As Range
    Dim r As Long
    
    ' Write worksheet data to Data Array.
    For Each sws In wb.Worksheets
        r = r + 1
        Data(r, 1) = sws.Name
        Set sCell = sws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious)
        If Not sCell Is Nothing Then
            Set sCell = sws.Cells(sCell.Row, _
                sws.Cells.Find("*", , , , xlByColumns, xlPrevious).Column)
            Data(r, 2) = sCell.Value
        End If
    Next sws
    
    ' Add and rename Destination Worksheet.
    Set dws = wb.Worksheets.Add(After:=wb.Sheets(wb.Sheets.Count))
    dws.Name = dName
    
    ' Write values from Data Array to Destination Range.
    dws.Range(dFirst).Resize(r, 2).Value = Data

End Sub

暂无
暂无

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

相关问题 当我尝试使用循环在 excel 表中写入行和列时,仅打印最后一个循环值 - When i Try to write row and column in an excel sheet using a loop only the last loop values gets print 从列的最后一行自动填充到整个工作表的最后一行 - Autofill from Last row of a column till Last row of the entire sheet 宏以获取最后一列的值到新工作表 - Macro to get values of last column to new sheet 使用 Pandas 将过滤后的行复制到新工作表,有没有办法将旧行号 append 复制到每个复制行的最后一个单元格? - Using Pandas to copy filtered rows to new sheet, is there a way to append the old row# to the last cell for each copied row? 在列中搜索值并将行复制到新工作表 - Search for a value in a column and copy row to new sheet 将列移动到新工作表的宏仅移动最后一列 - Macro to move Column to new sheet moves only last column VBA将值粘贴到最后一行下方的新表中 - VBA Paste Value into new sheet below last row 当第一行、最后一行、第一列、最后一列仅保存变量时,对 Excel 工作表进行排序 - Sorting an Excel Sheet when First Row, Last Row, First Column, Last Column saved only variables 试图在 sheet1 的最后一行的 b 列中查找单元格的值 - Trying to find the value of a cell in column b of the last row in sheet1 在不打开Excel表的情况下获取最后使用的行和列 - Get last used Row and Column in an Excel Sheet without opening it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM