当用户按下宏快捷键时,我正在尝试根据活动单元格复制一组单元格。 例如,他们选择了AI10(R10C35),我希望它复制范围R10C36:R22C79 然后将值粘贴到R11C36:R23C79。 复制范围的结尾始终为R22C79,粘贴结尾始终为R23C79。范围的开始是唯一根据活动单元而变化的东西。
如果我可以获得选择范围并复制的帮助,则可以从中找出PasteRange和HolidayRange。
我确信我的If语句也可以简化,也可以对这些语句进行建设性的批评,但是范围字符串是我的主要目标,因为其余字符串照常工作。
目前,我得到:
Runtime Error 1004
Method 'Range' of object '_Global' failed
谢谢!
Dim CurrentColumn As Integer
Dim CopyRange As String
Dim PasteRange As String
Dim HolidayRange As String
CurrentRow = ActiveCell.Row
CurrentColumn = ActiveCell.Column
If CurrentColumn <> "35" Then MsgBox ("You must select a date in column AI")
If CurrentRow < 9 Then MsgBox ("You must select a date in column AI")
If CurrentRow > 22 Then MsgBox ("You must select a date in column AI")
If CurrentColumn <> "35" Then Exit Sub
If CurrentRow < 9 Then Exit Sub
If CurrentRow > 22 Then Exit Sub
CopyRange = "R" & CurrentRow & "C" & CurrentColumn + "1" & ":R22C79"
PasteRange = "R" & CurrentRow + "1" & "C79" & ":R23C79"
Range(CopyRange).Select
Selection.Copy
Range(PasteRange).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
HolidayRange = "R" & CurrentRow & "C36:R" & CurrentRow & "C79"
Range(HolidayRange).ClearContents
答案 0 :(得分:1)
我可能不太正确,但是应该给你一个方法。
也值得一读how to avoid select。
Sub x()
Dim CurrentColumn As Long
currentrow = ActiveCell.Row
CurrentColumn = ActiveCell.Column
If CurrentColumn <> 35 Then MsgBox ("You must select a date in column AI"):exit sub
If currentrow < 9 Then MsgBox ("You must select a date in column AI"):exit sub
If currentrow > 22 Then MsgBox ("You must select a date in column AI"):exit sub
Range(Cells(currentrow + 1, CurrentColumn + 1), Cells(23, 79)).Value = Range(Cells(currentrow, CurrentColumn + 1), Cells(22, 79)).Value
Range(Cells(currentrow, 36), Cells(currentrow, 79)).ClearContents
End Sub