2

I have the example where I want to write a VBA statement which will select all data in a single column, there are no blanks in the column data. The column position will never change e.g. column A, and the data starts in row 3. However the total number of rows in the column will change regularly.

I want the system to dynamically select all the cells in column and then I can run a method against these selected pieces of data.

2
  • Please elaborate on your problem a bit more. As it is now i dont even see a questionmark. :) Commented Jun 1, 2017 at 12:37
  • What's the method you want to run? You may not need to actually select the data - as a general rule you don't have to select before performing an action on a range. Commented Jun 1, 2017 at 12:56

3 Answers 3

2

As an example of performing an action on your range without selecting it:

Public Sub Test()

    Dim rColA As Range

    With ThisWorkbook.Worksheets("Sheet1")
        Set rColA = .Range(.Cells(3, 1), .Cells(.Rows.Count, 1).End(xlUp))
        MsgBox "Column A range is " & rColA.Address 'Delete if you want.

        rColA.Interior.Color = RGB(255, 0, 0) 'Turn the back colour red.

        rColA.Cells(2, 1).Insert Shift:=xlDown 'Insert a blank row at second cell in range
                                               'So will insert at A4.

        'If the first cell in your range is a number then double it.
        If IsNumeric(rColA.Cells(1, 1)) Then
            rColA.Cells(1, 1) = rColA.Cells(1, 1) * 2
        End If

    End With

End Sub
Sign up to request clarification or add additional context in comments.

Comments

1

Try

Dim LastRow as Long, sht as worksheet
Set sht = ThisWorkbook.Worksheets("My Sheet Name")    
LastRow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row
sht.Range("A3:A" & LastRow).Select

Like Darren Bartrup-Cook says, you may not need to select the data, you can almost always perform actions directly which is much faster.

Comments

1

If your column is "isolated" meaning no other nonblank cells touch your data you can use:

Range("firstCellInYourColumn").CurrentRegion.Select

(this works the same way as Ctrl+* from keyboard)

otherwise use:

Range(Range("firstCellInYourColumn"), Range("firstCellInYourColumn").End(xlDown)).Select

both will work if there are really no blanks within your data. You should also prepend all Range with worksheet expression, I omitted this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.