简体   繁体   中英

Excel VBA: Copy dynamic range to last row based on offset value

I have a couple of issues I need help with.

Where a user has selected an active month (row 3) by using a data validation drop down I want to allow the user to press a button which will 1) Add a formula to the location 2 rows under where "Active Month" was identified 2) Copy this to the last row in the work book 3) Select that row remove formulas and replace with the values only.

The first issue I get is a 1004 message. The code works if I enter a basic formula eg =5+10 but not for this one

2nd problem. Where I have Range("Z5:Z" & Lastrow) I don't understand how I can make this select based on the column value of where the user selected the "Active month" it could be AA, AB, AC etc

The code is below where I'm getting the 1004 error.

Dim Lastrow As Long

Lastrow = Range("D" & Rows.Count).End(xlUp).Row
Cells.Find(What:="Active Month", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate

ActiveCell.Offset(2).Select
Range("Z5:Z" & Lastrow).Formula = "=IF(ISNUMBER(VLOOKUP(F5,Dormant!A:A,1,0)),Z4,"")"

1) Add a formula to the location 2 rows under where "Active Month" was identified

2) Copy this to the last row in the work book

3) Select that row remove formulas and replace with the values only.

You are getting Application Defined Error becuase you did not pad up the double quotes in your formula. Replace "" with """" in that formula so that it becomes "=IF(ISNUMBER(VLOOKUP(F5,Dormant!A:A,1,0)),Z4,"""")"

Is this what you are trying? ( UNTESTED ). I have commented the code. If you still have any doubts then simply ask :)

Sub Sample()
    Dim ws As Worksheet
    Dim Lastrow As Long, StartRow As Long
    Dim aCell As Range
    Dim ColName As String, myformula As String

    '~~> Change formula here
    myformula = "=IF(ISNUMBER(VLOOKUP(F5,Dormant!A:A,1,0)),Z4,"""")"

    '~~> Set this to the relevant worksheet
    Set ws = Sheet1

    With ws
        '~~> Find the "Active Month"
        Set aCell = .Cells.Find(What:="Active Month", LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

        '~~> If Found
        If Not aCell Is Nothing Then
            '~~> Get offset to where it was found
            StartRow = aCell.Row + 2

            '~~> Get the column name where it is found
            ColName = Split(.Cells(, aCell.Column).Address, "$")(1)

            '~~> Find last row in that column
            Lastrow = .Range(ColName & .Rows.Count).End(xlUp).Row

            '~~> Identify the range and insert formula and convert it to values
            With .Range(ColName & StartRow & ":" & ColName & Lastrow)
                .Formula = myformula
                .Value = .Value
            End With
        End If
    End With
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