如何将新信息放在下一个空行?

huangapple go评论72阅读模式
英文:

My VBA code overwrites my last row. How can I write the code to put new information on the next empty row?

问题

我跟随一个YouTube教程来将一个工作簿中一个工作表的数据复制到另一个工作表中。请注意,我对VBA没有经验。

我注意到当我添加新条目时,它会覆盖之前的行。我希望代码能够找到最后一个空行并将最新的条目添加到那里。所以,如果第1行和第2行有信息,那么最新的信息将被传送到第3行,依此类推。

以下是代码:

Private Sub CommandButton1_Click()
    '创建并设置时间追踪表和主数据表的变量
    Dim TTrk As Worksheet, MDat As Worksheet

    Set TTrk = Sheet1
    Set MDat = Sheet2

    '为时间追踪表中的每个单元格创建并设置变量
    Dim Name As Range, AddDate As Range, TotalHours As Range, Activity As Range, Client As Range, Category As Range, AddHours As Range, TimeSpent As Range, Additional As Range

    Set Name = TTrk.Range("C2")
    Set AddDate = TTrk.Range("C3")
    Set TotalHours = TTrk.Range("C5")
    Set Activity = TTrk.Range("B8:B15")
    Set Client = TTrk.Range("C8:C15")
    Set Category = TTrk.Range("D8:D15")
    Set AddHours = TTrk.Range("E8:E15")
    Set TimeSpent = TTrk.Range("F8:F15")
    Set Additional = TTrk.Range("G8:G15")

    '创建主数据表中粘贴单元格的变量
    Dim DestCell As Range

    If MDat.Range("A2") = "" Then '如果A2为空
        Set DestCell = MDat.Range("A2") '...那么目标单元格是A2
    Else
        Set DestCell = MDat.Range("A1").End(xlDown).Offset(1, 0) '...否则,下一个空行

        If MDat.Range("A2") = "" Then '如果A2为空
            Set DestCell = MDat.Range("A2") '...那么目标单元格是A2
        Else
            Set DestCell = MDat.Range("A1").End(xlDown).Offset(2, 0) '...否则,下一个空行
        End If
    End If

    '从时间追踪表复制并粘贴数据到主数据表
    Name.Copy DestCell
    AddDate.Copy DestCell.Offset(0, 1)
    TotalHours.Copy DestCell.Offset(0, 2)
    Activity.Copy DestCell.Offset(0, 3)
    Client.Copy DestCell.Offset(0, 4)
    Category.Copy DestCell.Offset(0, 5)
    AddHours.Copy DestCell.Offset(0, 6)
    TimeSpent.Copy DestCell.Offset(0, 7)
    Additional.Copy DestCell.Offset(0, 8)

    '清除时间追踪表中的内容
    AddDate.ClearContents
    TotalHours.ClearContents
    Activity.ClearContents
    Client.ClearContents
    Category.ClearContents
    AddHours.ClearContents
    TimeSpent.ClearContents
    Additional.ClearContents

End Sub

我尝试去掉某些代码并尝试找到可以添加到我的当前代码中的代码,以查看是否可以解决问题,但我对VBA没有经验。

英文:

I followed a YouTube tutorial to copy data from one sheet into another sheet in the same workbook. Mind you, I have no experience in VBA.

I noticed that when I add a new entry it overwrites the previous row. I want the code to find the last empty row and add the newest entry there. So, if rows 1 and 2 have information, then the newest information gets transferred to row 3 and so forth.

Here is the code:

Private Sub CommandButton1_Click()
'Create and set variables for the Time Tracker & Master Data worksheets
Dim TTrk As Worksheet, MDat As Worksheet

Set TTrk = Sheet1
Set MDat = Sheet2

'Create and set variables for each cell in the Time Tracker sheet
Dim Name As Range, AddDate As Range, TotalHours As Range, Activity As Range, Client As Range, Category     As Range, AddHours As Range, TimeSpent As Range, Additional As Range

Set Name = TTrk.Range("C2")
Set AddDate = TTrk.Range("C3")
Set TotalHours = TTrk.Range("C5")
Set Activity = TTrk.Range("B8:B15")
Set Client = TTrk.Range("C8:C15")
Set Category = TTrk.Range("D8:D15")
Set AddHours = TTrk.Range("E8:E15")
Set TimeSpent = TTrk.Range("F8:F15")
Set Additional = TTrk.Range("G8:G15")

'Create a variable for the paste cell in the Master Data worksheet
Dim DestCell As Range

If MDat.Range("A2") = "" Then 'If A2 is empty
Set DestCell = MDat.Range("A2") '...then destination cell is A2
Else
Set DestCell = MDat.Range("A1").End(xlDown).Offset(1, 0) '...otherwise the next empty row

If MDat.Range("A2") = "" Then 'If A2 is empty
Set DestCell = MDat.Range("A2") '...then destination cell is A2
Else
Set DestCell = MDat.Range("A1").End(xlDown).Offset(2, 0) '...otherwise the next empty row

End If

'Copy and paste data from the Time Tracker worksheet to the Master Data worksheet
Name.Copy DestCell
AddDate.Copy DestCell.Offset(0, 1)
TotalHours.Copy DestCell.Offset(0, 2)
Activity.Copy DestCell.Offset(0, 3)
Client.Copy DestCell.Offset(0, 4)
Category.Copy DestCell.Offset(0, 5)
AddHours.Copy DestCell.Offset(0, 6)
TimeSpent.Copy DestCell.Offset(0, 7)
Additional.Copy DestCell.Offset(0, 8)

'Clear the contents in the Time Tracker worksheet
AddDate.ClearContents
TotalHours.ClearContents
Activity.ClearContents
Client.ClearContents
Category.ClearContents
AddHours.ClearContents
TimeSpent.ClearContents
Additional.ClearContents

End Sub

I tried taking away certain coding and trying to find coding to add into my current code to see if it would resolve the issue, but I have no experience in VBA.

答案1

得分: 2

将范围侧-by-侧复制
-
**一个快速修复**

- 以以下方式引用目标单元格

<!-- 语言lang-vb -->

    Dim DestCell As Range: Set DestCell = MDat.Range("A2")
    
    With MDat.UsedRange
        Dim drOffset As Long:
        drOffset = .Rows(.Rows.Count).Row - DestCell.Row + 1
        If drOffset > 0 Then
            Set DestCell = DestCell.Offset(drOffset)
        End If
    End With

**之前**

[![在这里输入图片描述][1]][1]

**之后**

[![在这里输入图片描述][2]][2]

**一个简化**

<!-- 语言lang-vb -->

    Private Sub CommandButton1_Click()
        
        Const SRC_RANGES As String = "C2,C3,C5,B8:G15"
        Dim sClearFlags(): sClearFlags = VBA.Array(0, 1, 1, 1)
        Const DST_FIRST_CELL As String = "A2"
        
        Dim sws As Worksheet: Set sws = Sheet1
        Dim dws As Worksheet: Set dws = Sheet2
        
        Dim dfCell As Range: Set dfCell = dws.Range(DST_FIRST_CELL)
        
        With dws.UsedRange
            Dim drOffset As Long: drOffset = .Rows(.Rows.Count).Row - dfCell.Row + 1
            If drOffset > 0 Then
                Set dfCell = dfCell.Offset(drOffset)
            End If
        End With
        
        With sws.Range(SRC_RANGES)
            Dim arg As Range, n As Long
            For Each arg In .Areas
                ' 复制.
                arg.Copy dfCell
                Set dfCell = dfCell.Offset(, arg.Columns.Count)
                ' 清除.
                If sClearFlags(n) = 1 Then
                    arg.ClearContents
                End If
                n = n + 1
            Next arg
        End With
     
    End Sub

  [1]: https://i.stack.imgur.com/qI1HJ.jpg
  [2]: https://i.stack.imgur.com/PLscV.jpg
英文:

Copy Ranges Side-By-Side

A Quick Fix

  • Reference the destination cell in the following way:

<!-- language: lang-vb -->

Dim DestCell As Range: Set DestCell = MDat.Range(&quot;A2&quot;)
With MDat.UsedRange
Dim drOffset As Long:
drOffset = .Rows(.Rows.Count).Row - DestCell.Row + 1
If drOffset &gt; 0 Then
Set DestCell = DestCell.Offset(drOffset)
End If
End With

Before

如何将新信息放在下一个空行?

After

如何将新信息放在下一个空行?

A Simplification

<!-- language: lang-vb -->

Private Sub CommandButton1_Click()
Const SRC_RANGES As String = &quot;C2,C3,C5,B8:G15&quot;
Dim sClearFlags(): sClearFlags = VBA.Array(0, 1, 1, 1)
Const DST_FIRST_CELL As String = &quot;A2&quot;
Dim sws As Worksheet: Set sws = Sheet1
Dim dws As Worksheet: Set dws = Sheet2
Dim dfCell As Range: Set dfCell = dws.Range(DST_FIRST_CELL)
With dws.UsedRange
Dim drOffset As Long: drOffset = .Rows(.Rows.Count).Row - dfCell.Row + 1
If drOffset &gt; 0 Then
Set dfCell = dfCell.Offset(drOffset)
End If
End With
With sws.Range(SRC_RANGES)
Dim arg As Range, n As Long
For Each arg In .Areas
&#39; Copy.
arg.Copy dfCell
Set dfCell = dfCell.Offset(, arg.Columns.Count)
&#39; Clear.
If sClearFlags(n) = 1 Then
arg.ClearContents
End If
n = n + 1
Next arg
End With
End Sub

huangapple
  • 本文由 发表于 2023年7月6日 13:49:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定