0

VBA 新手,所以要温柔。

我有 7 列,A:H。第一列是唯一的数字标识符(从 0 开始,每次我通过此宏粘贴新选择时都应该增加)。第二列是日期,提示后手动输入。

我希望能够突出显示一系列单元格,激活宏,然后宏移动突出显示的数据并将其粘贴到 C 和 I 列之间的下一个可用空间块中。开始时,宏会提示一个对话框,询问用户日期。我希望在选择中的每个单元格沿 B 列(在下一个空单元格中)的每个点输入此日期。

以下是列现在的格式:http: //i.imgur.com/7ytAnr9.png

然后,对于选择中的每个单元格,我希望它与数字 ID 相关联。因此脚本将查看 A 列中的最后一个数字,将其添加到该数字上,然后将其粘贴到当前选择的每个单元格中。

这是我的代码,但由于我是新手,所以它完全被破坏了。

对于对话框:

Sub SuperMacro()

    Dim c As Object
    Dim dateManager As String
    dateManager = InputBox(Prompt:="Enter the Date for Selection", _
          Title:="Date Manager", Default:="1/24/2013")

    If strName = "Your Name here" Or _
        strName = vbNullString Then
        Exit Sub
    End If

    For Each c In Selection
        Range("A1").End(xlDown).Offset(1, 0).Select  'Paste the date for each cell in selection
        ActiveSheet.Paste
    Next c

     'Attempt to move all date from selected area to next available chunk of space between C1 and H1.  
     Selection.Copy
     Range("C1:H1").End(xlDown).Offset(1, 0).Select
     ActiveSheet.Paste

End Sub

编辑:想出了解决 ID 枚举问题和列移动的解决方案:

Sub CopyTest()

    Dim a As Range, b As Range
    Dim value As Integer

    Selection.Copy
    Set a = Selection
    Range("B1:H1").End(xlDown).Offset(1, 0).Select
    ActiveSheet.Paste

    value = (Range("A1").End(xlDown)) + 1
    For Each b In a.Rows
        Range("A1").End(xlDown).Offset(1, 0).Select
        ActiveCell.value = value
    Next

End Sub

让我知道是否有更有效的方法来做到这一点。

自己为他人找到了解决方案:

子超宏()

Dim a As Range, b As Range
Dim currentID As Integer


Set a = Selection
Selection.Cut Range("C1:I1").End(xlDown).Offset(1, 0) 'Pastes to appropriate column


currentID = Range("A1").End(xlDown).Value


For Each b In a.Rows
    Range("A1").End(xlDown).Offset(1, 0) = currentID + 1
    Range("B1").End(xlDown).Offset(1, 0) = InputBox("Enter Date", "Date Helper")
Next b

结束子

4

1 回答 1

0

这是我找到的答案:

Dim a As Range, b As Range
Dim currentID As Integer


Set a = Selection
Selection.Cut Range("C1:I1").End(xlDown).Offset(1, 0) 'Pastes to appropriate column


currentID = Range("A1").End(xlDown).Value


For Each b In a.Rows
    Range("A1").End(xlDown).Offset(1, 0) = currentID + 1
    Range("B1").End(xlDown).Offset(1, 0) = InputBox("Enter Date", "Date Helper")
Next b

End Sub
于 2013-01-25T17:32:34.667 回答