Column number inside selection

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
Selected 9 cells (3 rows/ 3 col) like below and wrote the code below. When I run the code and enter for example 5 as column number, to my surprise, it will enter hello inside column 5 which is outside my selection range. Why is that? Thank you very much.0

Code:
Sub myrow_selection()
    Dim xcell As Range
    Dim x As Integer
    x = InputBox("which col")
    Selection.Columns(x).Value = "hello"
    For Each xcell In Selection
        xcell.Interior.ColorIndex = WorksheetFunction.RandBetween(1, 56)
    Next xcell
End Sub

ps: sorry it wont allow me to correct the title
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
So you select 3 random columns and rows

And then you tell the script to do something in column 5 it does that.

What did you expect it to do?

Where did you want Yes entered?
 
Upvote 0
Thanks for your reply. I was experimenting Selection option. I thought I would get a message saying out of range or something. But you made a good point. Thank you very much.
 
Upvote 0

Forum statistics

Threads
1,214,989
Messages
6,122,622
Members
449,093
Latest member
catterz66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top