我遇到了一个有点问题的错误。以下代码的目标是允许用户选择他们想要修改的工作表(HR1 - HR20),然后选择原始工作表上要复制到所选工作表的单元格(单元格范围)。
问题是用户必须选择他们想要更改的范围,然后运行宏(通过按钮),选择工作表,然后重新安装范围。如果它们在宏运行之前只选择了一个单元格,则宏将复制该单个单元格的值,即使它们突出显示范围后,当宏提示时,它们也需要更改。有没有办法告诉宏只使用选定的范围?
Dim WSN As Worksheet
Set WSN = ActiveSheet
Dim sheetname As String
sheetname = ActiveSheet.Name
Unload Me
On Error GoTo Cancel
Dim rng As Range
Dim myString As String
Set rng = Application.InputBox("select cell range with changes", "Cells to be copied", Default:="Select Cell Range", Type:=8)
Application.ScreenUpdating = False
myString = Selection.Address(ReferenceStyle:=xlA1, RowAbsolute:=False, ColumnAbsolute:=False)
'MsgBox Selection.Address(ReferenceStyle:=xlA1, RowAbsolute:=False, ColumnAbsolute:=False)
Range(myString).Copy
If CheckBoxALL.Value = True Then
Sheets("Cost").Select
For i = 1 To 20
ActiveSheet.Next.Select
rngS = rng.Address
Range(rngS).Select
ActiveSheet.Paste
ActiveWindow.ScrollRow = 85
Next i
End If
If CheckBoxHR1.Value = True Then
Sheets("Cost").Next.Select
rngS = rng.Address
Range(rngS).Select
ActiveSheet.Paste
ActiveWindow.ScrollRow = 85
End If
答案 0 :(得分:1)
没有测试它,但我认为你可以使用' rng'变量直接
我的意思是
而不是
Set rng = Application.InputBox("select cell range with changes", "Cells to be copied", Default:="Select Cell Range", Type:=8)
Application.ScreenUpdating = False
myString = Selection.Address(ReferenceStyle:=xlA1, RowAbsolute:=False, ColumnAbsolute:=False)
'MsgBox Selection.Address(ReferenceStyle:=xlA1, RowAbsolute:=False, ColumnAbsolute:=False)
Range(myString).Copy
你可以这样走
Set rng = Application.InputBox("select cell range with changes", "Cells to be copied", Default:="Select Cell Range", Type:=8)
Application.ScreenUpdating = False
rng.Copy