I need to be able to select columns in Excel by specifying the 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, column.selection

I need to select a column by specifying the column number or name.

+3


source to share


1 answer


Just use Columns(1) = Column one

orColumns("A") = Column one

Worksheet.Columns Property (Excel)

Returns a Range object that represents all of the columns in the active sheet. If the active document is not a sheet, the columns property fails.

Example



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

      

Or

WorksheetFunction.CountA (columns (1))

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

      

+1


source







All Articles