2017-07-28 55 views
-1

我試圖錄制一個宏來自動格式化使用情況報告。這些報告並不都具有相同數量的行或列。在錄製的宏中選擇數據

我想要做的是選擇所有列和行與數據。當我點擊時錄製Ctrl + Shift + Down + Right它將選擇該報告中的數據。但是,如果我在具有更多行或列的數據集上運行它,它將不會將其包含在選擇中。

有沒有辦法從起始單元格中選擇可用數據的行或列的末尾?

+1

使用**使用相對引用**記錄您的Ctl + Shift +向下+向右。 Ctrl + A也會給你活動單元格的CurrentRegion。 – Jeeped

回答

0

使用相對引用一定會爲您提供正確的編碼。或者,您可以將此代碼Range("A1").CurrentRegion.Select放入宏中的適當位置(將最左上角的單元放置在「A1」位置)。

如果您想進行更高級的工作,您可以將當前區域保存爲變量並使用其上的不同屬性來執行除簡單選擇區域之外的任何操作。

例如:

sub test() 
dim rng as range 
dim rw as integer 
dim col as integer 

set rng = Range("A1").CurrentRegion 'choose the top leftmost cell in the range 
rw = rng.rows.count 'counts the number of rows in the selection 
col = rng.columns.count 'counts the number of columns 
rng.select 'selects the range in an efficient manner 
rng.columns(2).select 'selects the second column in the range 
rng.rows(2).select 'selects the second row in the range 
rng.cells(1,2).select 'selects the cell on the first row and second column in the range 

end sub 

希望這是有幫助的。我只是想擴展一些你可以做的事情來處理你的範圍。