【问题标题】:VBA code to overwrite cells through moving /shifting up a range of cells with a user input boxVBA代码通过使用用户输入框移动/向上移动一系列单元格来覆盖单元格
【发布时间】: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

【问题讨论】:

    标签: vba excel inputbox


    【解决方案1】:

    从上一个问题中扩展接受的答案,您可以执行以下操作:

    这样用户可以使用输入框选择其作用的范围?

    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
    

    【讨论】:

    • 您输入的值是什么?
    • 相应的列、行。
    • 哦,那是因为您的输入框现在需要一个范围。我的期望是整数
    • 好的,我把它改回了你的代码。它现在完美运行:) 但是,如果所有 4 个框的输入均为空,则会出现编程错误。如何避免?
    • 那么在这种情况下你想做什么?您可以检查一下是否有任何值为空,然后使用 GOTO 结束宏或再次抛出输入框,直到输入正确的值等
    【解决方案2】:

    这将对选定的范围进行操作:

    Selection.Value = Selection.Offset(1,0).Value
    

    【讨论】:

    • 但是我怎么放这个。就像在一个输入框中一样,或者?
    • 使用可以先选择范围再运行宏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多