[英]Excel 2007 VBA Get the row number of an active cell relative to its position in a range
[英]Edit a cell using its relative Position within a given Range
我開始使用excel vba時遇到問題,因為我試圖根據其在該range
位置從一個range
選擇一個cell
。
更具體地說,我的目標是讓用戶鍵入一個range
(在A5中),然后將該范圍的所有值都設置為10,但左上角的單元格必須為23。我的第一個猜測是這樣做
Private Sub CommandButton1_Click()
Dim enteredValue As String
enteredValue = Range("A5").Text
Dim focusArea As Range
Set focusArea = Range(enteredValue)
focusArea.Value = 10
facusArea.Cells(1, 1).Value = 23
End Sub
但我得到一個錯誤
424“需要對象”
因此,你們中有誰知道在給定范圍內如何進行此類操作的提示?
最后一行是錯誤的類型。 您使用過facusArea
,而不是focusArea
。 始終在代碼表的聲明區域中始終使用Option Explicit
另一個很好的理由(工具►選項►編輯器►要求變量聲明)。
由於以下原因,可能會關閉此問題:
該問題是由無法重現的問題或簡單的印刷錯誤引起的。 盡管此處可能會出現類似的問題,但該問題的解決方式不太可能對將來的讀者有所幫助。 通常可以通過在發布之前識別並仔細檢查重現問題所需的最短程序來避免這種情況。
但是,我想提供一個無變量替代方法,以替代您要使用With ... End With語句來標識范圍的方法。
Private Sub CommandButton1_Click()
With Range(Range("A5").Text)
.Value = 10
.Cells(1).Value = 23
End With
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.