2

I need to use my active column in my range statements. How to write down this:

Range((Activecolumn)2)

So I need a column value to be dynamic because I am moving left and right by pressing a userform button, but the row number always stays the same. At first I am working with column "C" and select it with

Columns("C").Select

and I navigate the selected column with

Private Sub Next_Format_Button_Click()

ActiveCell.Offset(, 1).EntireColumn.Select

End Sub

So when I navigate to the next column I need all my other range statements to go along with it. So basically the column letter in my range statements should be something like "current" or "active" column.

Could someone, please, help me?

4
  • 1
    Do you just need a variable where you get the number of the column you are in? Commented Mar 23, 2021 at 8:35
  • 3
    Just use Cells(2, activecell.column) Commented Mar 23, 2021 at 8:41
  • That is what I was looking for but in this example I was looking for "C1"not "C2" so that "2" in the code should be "1". But besides that - it's what I needed Commented Mar 23, 2021 at 9:33
  • How would it look if my rows range is "C4:C6" because "Cells(4:6, ActiveCell.Column)" seems to be incorrect as it highlights red in VBA editor Commented Mar 23, 2021 at 9:42

2 Answers 2

1
Range(ActiveCell.address).entireColumn.copy

Is what you are looking for I think?

I'd also recommend avoiding ".Select" unless absolutely necessary. It is adding unnecessary lines to your code and leaves the program at greater risk of inadvertent user corruption by selecting another range via mouse before the copy (or whatever) operation completes. Better to go straight to whatever operation you intend to do, or by assigning the range to a variable.

edit: Ah, sorry, I see now your only looking row 2, then its Cells(2, ActiveCell.Column) as pointed out above.

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

1 Comment

There's no possibility to accidentally select wrong place in the sheet as all the actions are happening in the userform and changes are instantly propagated to the sheet. Sheet has disabled editing and all that, but it's handy to see the active column selected from user standpoint
0

If you want to get the Number of the column where you selected something:

ActiveCell.Column

If you need the letter of the column your selection is in:

Split(ActiveCell.Address, "$")(1)

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.