Search code examples
vbaexcelinputbox

VBA code to overwrite cells through moving /shifting up a range of cells with a user input box


I previously asked a question on VBA code to overwrite cells through moving /shifting up a range of cells and i got an answer which met my needs. However i realised that i had to hard code all the ranges for each table to perform this vba function which is risky as the cell alignments often change in the sheet.. Thus i want an input box that allows user to select the table of cells they want to perform this function .I know how an input box works however the given code goes by rows and columns, something of which the range that the user selects, does not contain . Thus is there anyways for an input box to work in this code without having to hardcode? Or are there any other alternatives to avoid hardcording in this code and have it work based on the user selection basis? All of your help would be very much appreciated.

Improvised on the given answer but i am still getting a type mismatch error. Why is that so?

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

Solution

  • Extending the accepted answer from your last question you could do something like this:

    That way the User can select the range it acts on using input boxes?

    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