2016-07-31 94 views
0

我使用以下代碼塊來設置一些初始值。我發現我的自我使用這種構造很多,我想知道是否有更簡潔的東西?在Excel VBA中多次引用單元格的較簡單的方法

Sheets(AppTab).Select 
Cells(StartingRow, Range("AppTransEffDate").Column).Value = FirstPymtDueDate 
Cells(StartingRow, Range("AppTransAmt").Column).Value = RegularPymt 
Cells(StartingRow, Range("AppActionCode").Column).Value = SchPymtDueActionCode 
Cells(StartingRow, Range("AppDescr").Column).Value = TransDescr 

如果重要,這些範圍中的每一個都是一列寬。

回答

4

我會使用枚舉來引用列號。枚舉位於代碼模塊的頂部。列表中的下一個枚舉是從前一個增量1。枚舉列的最大優點是當您需要重新排列列時;你只需要更新你的枚舉,你的代碼將像以前一樣運行。枚舉也適用於intellisense。

enter image description here

Option Explicit 

Public Enum Cols 
    cnAppTransEffDate = 2 
    cnAppTransAmt = 4 
    cnAppActionCode = 5 
    cnAppDescr 
End Enum 


Sub FillData() 

    ' Some Code 

    With Worksheets(AppTab) 
     .Cells(StartingRow, cnAppTransEffDate).Value = FirstPymtDueDate 
     .Cells(StartingRow, cnAppTransAmt).Value = RegularPymt 
     .Cells(StartingRow, cnAppActionCode).Value = SchPymtDueActionCode 
     .Cells(StartingRow, cnAppDescr).Value = TransDescr 
    End With 
End Sub 
+0

我已經開始使用這種方法和代碼工作,但Intellisense不顯示枚舉(但工作,否則)。我還需要做些什麼來使它枚舉枚舉?在我鍵入以下內容後,是否應該顯示:StartingRow,cn [智能感知此處]? – WebUserLearner

+1

是鍵入cn cn,然後按住[ctrl] + [空格鍵]。 – 2016-07-31 19:05:22

3

使用一個或多個With ... End With陳述。

with Sheets(AppTab) 
    .Cells(StartingRow, .Range("AppTransEffDate").Column) = FirstPymtDueDate 
    .Cells(StartingRow, .Range("AppTransAmt").Column) = RegularPymt 
    .Cells(StartingRow, .Range("AppActionCode").Column) = SchPymtDueActionCode 
    .Cells(StartingRow, .Range("AppDescr").Column) = TransDescr 
end with 
'alternate 
with Sheets(AppTab) 
    with .rows(StartingRow) 
     .Cells(1, .Range("AppTransEffDate").Column) = FirstPymtDueDate 
     .Cells(1, .Range("AppTransAmt").Column) = RegularPymt 
     .Cells(1, .Range("AppActionCode").Column) = SchPymtDueActionCode 
     .Cells(1, .Range("AppDescr").Column) = TransDescr 
    end with 
end with 

我不認爲第二位....尾隨着將顯示儘可能多的改進在這種情況下,第一,但隔離你引用的細胞更有效,避免重複調用定位血統。

總是明確引用父工作表被廣泛認爲是「最佳實踐」,並避免使用.Select和.Activate。

+0

有什麼辦法縮短 .Cells(StartingRow,.Range(「AppTransEffDate」)。Column)= FirstPymtDueDate (我需要保持StartingRow作爲一個變量,因爲它會改變) – WebUserLearner

+0

我不知道AppTransEffDate,AppTransAmt,AppActionCode和AppDescr指的是哪裏或什麼,所以不,我不能建議任何東西。 – Jeeped

+0

這些都是範圍。每個基本上只是F,H等Col. – WebUserLearner

0

我通常的方法是設置行範圍的第一個單元格,然後使用Range._Default財產

Set r = Sheets(AppTab).Range("A" & StartingRow) 
r(, Range("AppTransEffDate").Column) = FirstPymtDueDate 

而是命名範圍的,我會用Enum像在Thomas Inzina的回答中。

如果您的命名範圍從第1行開始,那麼這可能會奏效

Range("AppTransEffDate")(StartingRow) = FirstPymtDueDate 

Set areas = Sheets(AppTab).Range("AppTransEffDate,AppTransAmt,AppActionCode,AppDescr").Areas 

areas(1)(StartingRow) = FirstPymtDueDate 
areas(2)(StartingRow) = RegularPymt 
areas(3)(StartingRow) = SchPymtDueActionCode 
areas(4)(StartingRow) = TransDescr 

也許交集可能工作太:

Set areas = Sheets(AppTab).Range("(AppTransEffDate,AppTransAmt,AppActionCode,AppDescr) " & StartingRow & ":" & StartingRow).Areas 

areas(1) = FirstPymtDueDate 
areas(2) = RegularPymt 
areas(3) = SchPymtDueActionCode 
areas(4) = TransDescr 
相關問題