【问题标题】:i need to create loop to copy from excel to paste in Notepad我需要创建循环以从 excel 复制以粘贴到记事本中
【发布时间】:2021-03-01 16:01:35
【问题描述】:

我在两列 A(条形码)和 B(计数)中有数据。我需要一个宏,它从 A 列复制数据并将其多次粘贴到记事本中,如 B 列所述。下面是我的代码,仅适用于选定的行。请帮助创建一个循环,以便宏工作到包含数据的最后一行: Sample Data

Sub Receivinggg()
    Dim obj As New DataObject
    Dim bc As Variant
    Dim i As Integer
    Dim j As Integer
    j = Cells(ActiveCell.Row, 2).Value
    For i = 1 To j
        bc = Selection.Value
            If Len(bc) < 11 Then
                bc = "0" & Selection.Value
            End If
        obj.SetText bc
        obj.PutInClipboard
        VBA.AppActivate ("1 - Notepad"), 0
        SendKeys "+{INSERT}"
        Application.Wait Now + TimeValue("00:00:02")
        SendKeys "{Enter}"
        Next i
        VBA.AppActivate ("Receiving - Excel"), 0
    SendKeys "{Down}"
End Sub

【问题讨论】:

  • 为什么要将其粘贴到记事本中 - 将其写入文本文件然后用记事本打开该文件不是更容易吗?
  • 我实际上需要在网络表单中输入数据并在那里提交。我一直在用记事本练习。
  • 所以您的问题与记事本或剪切粘贴无关?你应该改写你的问题 - 至少对我来说,不清楚你的问题是什么。
  • 复制粘贴是基本要求。它适用于一个选定的单元格。问题是我希望它对 A 列中包含数据的所有单元格执行
  • 所以您的问题是如何遍历列的所有单元格?或者“如何找到范围内使用的最后一个单元格” i>。这里有很多关于这个的答案,例如stackoverflow.com/a/11169920/7599798。不要在问题中包含所有其他内容(尤其是在标题中)。

标签: excel vba loops


【解决方案1】:

问题已解决,我成功地在循环中添加了一个循环以获得所需的结果。无论如何,谢谢。

    Sub test()
    Dim bc As Variant
    Dim cellrow As Integer
    Dim cellcol As Integer
    Dim lastrow As Integer
    Dim x As Integer
    Dim i As Integer
    Dim obj As New DataObject
    Dim j As Integer
    cellrow = 2
    cellcol = 1
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 2 To lastrow
    Cells(cellrow, cellcol).Select
        j = Cells(ActiveCell.Row, 2).Value
            For x = 1 To j
                    bc = Selection.Value
                    If Len(bc) < 11 Then
                    bc = "0" & Selection.Value
                    End If
                    obj.SetText bc
                    obj.PutInClipboard
                    VBA.AppActivate ("1 - Notepad"), 0
               ' VBA.AppActivate ("aaaa - WebUtil"), 0
                    SendKeys "+{INSERT}"
                    Application.Wait Now + TimeValue("00:00:03")
                    SendKeys "{Enter}"
                Next x
    VBA.AppActivate ("Receiving - Excel"), 0
    cellrow = cellrow + 1
    Next i
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-09
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 2017-03-09
    • 1970-01-01
    相关资源
    最近更新 更多