2017-05-08 71 views
3
Dim count As Integer 
Dim myData As Workbook 
Dim col As Integer, rng As Range, n#, b# 

col = Selection.Column 'choose the column to count the values from 

If Application.WorksheetFunction.CountA(Columns(col)) = 0 Then 
    MsgBox "You have selected a blank column" 
    n = 0 
Else 
    Set rng = Intersect(Columns(col), ActiveSheet.UsedRange) 
    On Error Resume Next 
    b = rng.Cells.SpecialCells(xlCellTypeBlanks).count 
    n = rng.Cells.count - b - 1 
    On Error GoTo 0 
    Worksheets("sheet1").Select 

而不是column.selection指定列數來選擇Excel列,我需要能夠通過指定列編號或名稱來選擇列。我需要能夠在VBA

+1

'列(1)=柱one'或'列( 「A」)=柱一個'現在設置你的'Dim col As Range'和'col = Col umns(1)' – 0m3r

+0

運行時錯誤 - 對象變量或塊變量未設置 – 11user2614896

+1

您需要使用'set col = Columns(1)'sry已清除,請告知我 – 0m3r

回答

1

只需使用Columns(1) = Column oneColumns("A") = Column one

Worksheet.Columns Property (Excel)

返回表示活動工作表上的所有列的Range對象。如果活動文檔不是工作表,則「列」屬性將失敗。

Worksheets("Sheet1").Columns(1).Font.Bold = True 

或者

WorksheetFunction.CountA(Columns(1))

WorksheetFunction.CountA(Columns(1)), same as column 1 
相關問題