【问题标题】:Excel VBA Macro: Copying relative cell to another worksheetExcel VBA宏:将相关单元格复制到另一个工作表
【发布时间】:2013-05-01 14:46:21
【问题描述】:

我正在尝试修复丢失的条目

  1. 找到它然后
  2. 将相对于找到的条目最左边的单元格值复制到另一个工作表的第一个空底部单元格。
   With Worksheets("Paste Pivot").Range("A1:AZ1000")
   Dim source As Worksheet
   Dim destination As Worksheet
   Dim emptyRow As Long
   Set source = Sheets("Paste Pivot")
   Set destination = Sheets("User Status")
   Set c = .Find("MissingUserInfo", LookIn:=xlValues)
   If Not c Is Nothing Then
    firstAddress = c.Address
    Do
                   'Here would go the code to locate most left cell and copy it into the first empty bottom cell of another worksheet  
       emptyRow = destination.Cells(destination.Columns.Count, 1).End(xlToLeft).Row
       If emptyRow > 1 Then
       emptyRow = emptyRow + 1
       End If
       c.End(xlToLeft).Copy destination.Cells(emptyRow, 1)
        c.Value = "Copy User to User Status worksheet"

        Set c = .FindNext(c)
        If c Is Nothing Then Exit Do
    Loop While c.Address <> firstAddress
End If
End With  

【问题讨论】:

    标签: excel vba find


    【解决方案1】:

    我认为CurrentRegion 会在这里为您提供帮助。

    例如如果您在 A1:E4 范围内的每个单元格中都有一个值,那么

    Cells(1,1).CurrentRegion.Rows.Count 等于 4 和

    Cells(1,1).CurrentRegion.Columns.Count 等于 5

    因此你可以写:

    c.End(xlToLeft).Copy _
        destination.Cells(destination.Cells(1).CurrentRegion.Rows.Count + 1, 1)
    

    如果您的目标电子表格中间没有任何间隙,这会将用户 ID 从“MissingUserInfo”行的开头(在“粘贴数据透视表”表中)复制到第一个单元格“用户状态”表末尾的新行。

    然后你的 Do 循环变成:

    Do
        c.End(xlToLeft).Copy _
            destination.Cells(destination.Cells(1).CurrentRegion.Rows.Count + 1, 1)
        c.Value = "Copy User to User Status worksheet"
        Set c = .FindNext(c)
        If c Is Nothing Then Exit Do
    Loop While c.Address <> firstAddress
    

    【讨论】:

      【解决方案2】:

      按照原发帖人最初编辑的问题回答:

      With Worksheets("Paste Pivot").Range("A1:AZ1000")
      Dim source As Worksheet
      Dim sourceRowNumber As Long
      Dim destination As Worksheet
      Dim destCell As Range
      Dim destCellRow As Long
      Set source = Sheets("Paste Pivot")
      Set destination = Sheets("User Status")
      Set c = .Find("MissingUserInfo", LookIn:=xlValues)
      If Not c Is Nothing Then
          firstAddress = c.Address
          Do
      
            With destination
             Set destCell = .Cells(.Rows.Count, "A").End(xlUp)
             destCellRow = destCell.Row + 1
              End With
      
             sourceRowNumber = c.Row
      
             destination.Cells(destCellRow, 1).Value = source.Cells(sourceRowNumber, 1)
             destination.Cells(destCellRow, 2).Value = source.Cells(sourceRowNumber, 2)
             destination.Cells(destCellRow, 3).Value = source.Cells(sourceRowNumber, 3)
      
             c.Value = "Run Macro Again"
      
              Set c = .FindNext(c)
              If c Is Nothing Then Exit Do
          Loop While c.Address <> firstAddress
      End If
      End With
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多