VBA新手,所以请保持温和。
我有7列,A:H。第一列是唯一的数字标识符(从0开始,每次通过此宏粘贴新选择时都会增加)。第二列是Date,在提示后手动输入。
我希望能够突出显示一系列单元格,激活宏,并且宏移动突出显示的数据并将其粘贴到C和I列之间的下一个可用空间块中。开始时,宏会提示对话框询问用户日期。我希望在选择中每个单元格的B列(在下一个空单元格中)的每个点输入此日期。
以下是现在格式化列的方式:http://i.imgur.com/7ytAnr9.png
然后,对于选择中的每个单元格,我希望它与数字ID相关联。因此,脚本将查看A列中的最后一个数字,添加一个,并将其粘贴到当前选择中的每个单元格。
这是我的代码,但由于我是新手,它完全被打破了。
对于DialogBox:
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
如果有更有效的方法,请告诉我。
自己为别人提供解决方案:
Sub SuperMacro()
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
答案 0 :(得分: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