VBA 通过使用用户输入框移动/上移一系列单元格来覆盖单元格的代码
VBA code to overwrite cells through moving /shifting up a range of cells with a user input box
我之前在 上问过一个问题,我得到了满足我需要的答案。但是我意识到我必须对每个 table 的所有范围进行硬编码才能执行此 vba 函数,这是有风险的,因为单元格对齐经常在 sheet 中发生变化。因此我想要一个允许用户 select 他们想要执行此功能的 table 单元格的输入框。我知道输入框是如何工作的,但是给定的代码按行和列显示,其中一些范围是用户 selects,不包含 。因此,无论如何输入框都可以在这段代码中工作而无需硬编码?或者是否有任何其他替代方法可以避免在此代码中进行硬编码并使其基于用户 selection 工作?非常感谢您的所有帮助。
根据给定的答案即兴创作,但我仍然收到类型不匹配错误。为什么会这样?
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
扩展你上一个问题的已接受答案,你可以这样做:
这样用户就可以 select 使用输入框作用的范围?
Dim y1 As Variant
Dim y2 As Variant
Dim x1 As Variant
Dim x2 As Variant
Dim cell1 As Integer
Dim cell2 As Integer
y1 = Application.InputBox("Input First Row")
If y1 = "" Or y1 = False Then GoTo handleNull
y2 = Application.InputBox("Input End Row")
If y2 = "" Or y2 = False Then GoTo handleNull
x1 = Application.InputBox("Input First Column")
If x1 = "" Or x1 = False Then GoTo handleNull
x2 = Application.InputBox("Input End Column")
If x2 = "" Or x2 = False Then GoTo handleNull
cell1 = y2 - 1
cell2 = x1
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(cell1, cell2))
End With
handleNull:
End
这将对所选范围进行操作:
Selection.Value = Selection.Offset(1,0).Value
我之前在
根据给定的答案即兴创作,但我仍然收到类型不匹配错误。为什么会这样?
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
扩展你上一个问题的已接受答案,你可以这样做:
这样用户就可以 select 使用输入框作用的范围?
Dim y1 As Variant
Dim y2 As Variant
Dim x1 As Variant
Dim x2 As Variant
Dim cell1 As Integer
Dim cell2 As Integer
y1 = Application.InputBox("Input First Row")
If y1 = "" Or y1 = False Then GoTo handleNull
y2 = Application.InputBox("Input End Row")
If y2 = "" Or y2 = False Then GoTo handleNull
x1 = Application.InputBox("Input First Column")
If x1 = "" Or x1 = False Then GoTo handleNull
x2 = Application.InputBox("Input End Column")
If x2 = "" Or x2 = False Then GoTo handleNull
cell1 = y2 - 1
cell2 = x1
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(cell1, cell2))
End With
handleNull:
End
这将对所选范围进行操作:
Selection.Value = Selection.Offset(1,0).Value