【问题标题】:Is row not empty then go to next row to paste data based on current date in Column行不为空然后转到下一行以根据列中的当前日期粘贴数据
【发布时间】:2022-01-09 03:46:03
【问题描述】:

我是 VBA 新手,因此尝试执行此代码时,我能够在我想要的行中运行代码,但我也想为下 2 行运行代码,但不知道如何应用逻辑去下一个可用行。因为我想在同一日期复制数据 3 次。按钮点击后生成的代码如下。

添加其他信息:感谢大家的投入和时间,非常感谢您的帮助。

这很棒,但是我学到了新东西,因为我每天更新 3 次读取的数据,所以我想从我每天更新 3 次的行中复制我的阅读数据,所以为了跟踪目的,我想根据数据将读取数据复制到下面的行。

点击阅读数据应复制到上午、下午和晚上阅读的行,如您从此处的示例图像中看到的示例格式

Excel Format

我希望我能够解释我的问题,因为我不是技术人员,而是尽我所能。

感谢您的帮助。

Private Sub CommandButton1_Click()
Dim Rg As Range
   Set Rg = Me.UsedRange.Columns(1).Find(Application.Text(Date, [A16].NumberFormat), [A17], xlValues)
   
     If Rg Is Nothing Then MsgBox "Today's Date Not Found. Please check the 'Date Received'" Else Rg(1, 2).Resize(, 24).Value2 = [B16:W16].Value2: Set Rg = Nothing
     
           
    ' If Rg Is Nothing Then MsgBox "Today's Date Not Found. Please check the 'Date Received'" Else Rg(2, 2).Resize(, 24).Value2 = [B16:W16].Value2: Set Rg = Nothing
    ' If Rg Is Nothing Then MsgBox "Today's Date Not Found. Please check the 'Date Received'" Else Rg(3, 2).Resize(, 24).Value2 = [B16:W16].Value2: Set Rg = Nothing
    
End Sub

【问题讨论】:

  • 尝试使用Offsetmethod的范围,如Rg.Offset
  • 如果可能的话,最好在多行上编写代码 - 将If Then Else 放在一行上(然后再写一个语句)会更难理解。
  • 我试过了,但对我没用。
  • 对我有什么帮助..?

标签: vba range


【解决方案1】:

试试这个:您可以使用Resize() 填充 3 行而不是仅 1 行。

Private Sub CommandButton1_Click()
    Dim Rg As Range, copyRange As Range
    
    Set Rg = Nothing
    Set Rg = Me.UsedRange.Columns(1).Find(Application.Text(Date, [A16].NumberFormat), [A17], xlValues)
   
    If Rg Is Nothing Then
        MsgBox "Today's Date Not Found. Please check the 'Date Received'"
    Else
        Set copyRange = [B16:W16]
        Rg.Offset(0, 1).Resize(3, copyRange.Columns.Count).Value2 = [B16:W16].Value2
    End If
     
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 2022-07-21
    • 1970-01-01
    • 2019-09-02
    相关资源
    最近更新 更多