简体   繁体   English

我需要通过在vba中指定列号来选择Excel中的列

[英]I need to be able to select columns in Excel by specifying column numbers in vba

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

Instead of column.selection , I need to be able to choose the column by specifying the column number or name. 我需要通过指定列号或名称来选择列,而不是column.selection

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

Worksheet.Columns Property (Excel) Worksheet.Columns属性(Excel)

Returns a Range object that represents all the columns on the active worksheet. 返回一个Range对象,该对象表示活动工作表上的所有列。 If the active document isn't a worksheet, the Columns property fails. 如果活动文档不是工作表,则Columns属性将失败。

Example

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

Or 要么

WorksheetFunction.CountA(Columns(1)) WorksheetFunction.CountA(列(1))

WorksheetFunction.CountA(Columns(1)), same as column 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM