简体   繁体   中英

Excel cell range copy to next empty space, move back on demand

My general idea is to be able to move data (cell range) to the side (still being accessible and visible) for recall later (multiple calculations).
- Eg1: A1:E5 contains data (text and numbers) - being moved by macro.
- Eg2: A1:E17 as above to next available space.
- Eg3: A1:E13 as above to next available space.
My hope was to be able to recall specific data range at any point, I was hoping to achieve my goal with the below macro:

 Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Target.Range.Address = "$U$4" Then
        Range(ActiveCell, ActiveCell.Offset(300, 5)).Select
$$$COPY PASTE CODE PART $$$
        Exit Sub
    End If
 End Sub

I am using for this purpose self referring hyperlink which after being moved stops working and that's where I'm at. To help it to work I thought all I could use was just a Target Range Row 4 but that doesn't seem to work either. Any help much appreciated.

I believe this will do what you are looking for. In the spreadsheet (screen shot below) enter the location you want the archive to live to the left of the data (cell B4, B4). Enter your scenario data in cells C4, D4, and E4. To copy that data to the K1 archive, put your cursor in B4 (where "K1" lives) and run the Scenarios_Record() code.

If you want to retreive a scenario, put your cursor in the cell that defines where the data lives (eg, B4 or B5) and run Scenarios_Retreive().

Sub Scenarios_Record()
' Copies scenario to the archive
Dim strTarget As String, intNbrCells As Integer
intNbrCells = 3
' Target cell
strTarget = ActiveCell.Value
Debug.Print strTarget
Debug.Print Range(strTarget).Value
For i = 1 To intNbrCells
    Range(strTarget).Offset(0, i - 1).Value = ActiveCell.Offset(0, i).Value
Next i
End Sub

Sub Scenarios_Retrieve()
' Copies scenario from the archive
Dim strTarget As String, intNbrCells As Integer
intNbrCells = 3
' Target cell
strTarget = ActiveCell.Value
Debug.Print strTarget
Debug.Print Range(strTarget).Value
For i = 1 To intNbrCells
    ActiveCell.Offset(0, i).Value = Range(strTarget).Offset(0, i - 1).Value
Next i
End Sub

电子表格的屏幕截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM