![](/img/trans.png)
[英]What is the maximum value for row and column Range offset in VBA/Excel?
[英]Excel VBA: Copy dynamic range to last row based on offset value
我有幾個需要幫助的問題。
用戶使用數據驗證下拉菜單選擇了活動月份(第3行)時,我想允許用戶按下一個按鈕,該按鈕將1)將公式添加到標識“活動月份”的位置2行下2 )將其復制到工作簿的最后一行。3)選擇該行以刪除公式,並僅替換為值。
我收到的第一個問題是1004消息。 如果我輸入一個基本公式,例如= 5 + 10,則此代碼有效,但不適用於此公式
第二個問題。 我在哪里有Range(“ Z5:Z”和Lastrow),我不明白如何根據用戶選擇“有效月份”的列值進行此選擇,它可能是AA,AB,AC等
代碼在下面出現1004錯誤的位置。
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)在確定“有效月份”的兩行位置添加一個公式
2)將其復制到工作簿的最后一行
3)選擇該行刪除公式,然后僅替換為值。
由於沒有在公式中加雙引號,因此出現“ Application Defined Error
。 替換""
具有""""
式中,使之成為"=IF(ISNUMBER(VLOOKUP(F5,Dormant!A:A,1,0)),Z4,"""")"
這是您要嘗試的嗎? ( 未經測試 )。 我已經注釋了代碼。 如果您仍然有任何疑問,請問:)
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.