【发布时间】:2015-09-16 02:13:25
【问题描述】:
我之前在VBA code to overwrite cells through moving /shifting up a range of cells 上问过一个问题,我得到了一个满足我需求的答案。但是我意识到我必须对每个表的所有范围进行硬编码才能执行此 vba 功能,这是有风险的,因为工作表中的单元格对齐方式经常发生变化。因此我想要一个允许用户选择他们的单元格表的输入框想要执行此功能。我知道输入框是如何工作的,但是给定的代码按行和列排列,其中用户选择的范围不包含。因此,输入框是否可以在此代码中工作而无需硬编码?或者是否有任何其他替代方法可以避免在此代码中进行硬编码并使其基于用户选择的基础工作?非常感谢您的所有帮助。
对给定的答案进行了改进,但我仍然遇到类型不匹配错误。为什么会这样?
Sub Macro1()
Dim y1 As Variant
Dim y2 As Variant
Dim x1 As Variant
Dim x2 As Variant
y1 = Application.InputBox("Input First Row", Type:=8)
y2 = Application.InputBox("Input End Row", Type:=8)
x1 = Application.InputBox("Input First Column", Type:=8)
x2 = Application.InputBox("Input End Column", Type:=8)
Dim sh As Worksheet
Dim x As Long, y As Long
Set sh = ActiveSheet ' or the specific sheet
' The 12 months
For y = y1 To y2
' Your 7 columns
For x = x1 To x2
sh.Cells(y, x) = sh.Cells(y + 1, x)
Next x
Next y
'With sh.Cells(y2, 1)
'.Select
' This only works if your column A contains dates (not strings)
'.Value = DateAdd("m", 1, sh.Cells(y2 - 1, 1))
' End With
End Sub
【问题讨论】: