簡體   English   中英

給定行號和列號,如何選擇單元格?

[英]How can I select a cell, given its row and column number?

我試圖通過給出要使用的行和列來選擇一個單元格。

它給了我一個錯誤:

“無法獲取范圍類的選擇屬性。”

當我到達這一行時:

Sheets("Ticker").Cells(currRow, etfCol).Select.Paste

這是我很長的代碼的一小段:

Dim etfCol As Integer, etfCount As Integer, currRow As Integer, currRowValue As String
etfCol = 1   'Just select the first column
etfCount = Sheets("Automated Table").Cells(Rows.Count, etfCol).End(xlUp).Row
'Repeat for every row.
For currRow = 5 To etfCount
    ''''''''''''''''''''''''''''''''''Copy and paste the ticker
    Cells(currRow, etfCol).Copy
    Sheets("Ticker").Cells(currRow, etfCol).Select.Paste
Next

我收到這個錯誤是因為“etfCount”是我從“自動表格”工作表中得到的值,而我試圖將它用於“股票代碼”工作表?
這是我能想到的唯一原因,但這並不能完全解釋這個錯誤。

我試過調試代碼。

代替:

Sheets("Ticker").Cells(currRow, etfCol).Select.Paste

和:

Sheets("Ticker").Cells(currRow, etfCol).Paste

這假設Sheets("Automated Table")處於活動狀態。

如果您使用.Select這在 VBA 中不是一個好習慣),您可能會從宏記錄器中獲益良多。 只需記錄復制和粘貼並檢查代碼:

Sub Makro4()
    Range("B4").Select
    Selection.Copy
    Range("G8").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
End Sub

如您所見, .Select.Paste位於不同的行上,因此它們應該保留。 在您的代碼中,它應該是:

Sheets("Ticker").Cells(currRow, etfCol).Paste Sheets("Ticker").Cells(currRow, etfCol).Paste

或者你可以做得更好一點:

With Sheets("Ticker").Cells(currRow, etfCol)
     .Select
     .Paste
End With

無論如何,正如鏈接中所指出的,不鼓勵使用Select (雖然它有效)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM