1

I would like to use the inputBox to pick a column by user and apply it to run on each row index but haven't had a luck. Its saying type mismatch. A lot of things to learn and lacking experience in coding. Thanks in advance for any inputs.

Sub FormatRange()
   
Dim i As Long, r As Range
        
Set r = Application.InputBox("Pick Column", , , , , , , 8)
        
   For i = 3 To 1500
       If r.Value <> "" Then r.Interior.Color = vbYellow
   Next i

End Sub
3
  • 1
    perhaps this may help: stackoverflow.com/questions/18237311/…
    – gns100
    Commented May 6 at 15:14
  • Thanks @gns100 for your response. I manage to copy and apply most error handling. It's a well documented code and was really helpful in that sense. Although I'm still stuck with "How to apply the code from the link to my application?". I guess I have to dig dip and I'm feeling it's more than I could chew. Cheers!
    – DongM
    Commented May 7 at 2:17
  • I run the reference link code but as per previous comments to the link it did not utilize the pick column? am I missing something. sorry for my lack of vba experience still learning at this stage. Although I would like to apply it to my code.
    – DongM
    Commented May 7 at 3:07

1 Answer 1

1

Somehow with trial and testing I manage to utilize the correct syntax for column index. It seems that using the Cells ([RowIndex], [ColumnIndex]) will be the correct way of writing the code if we want the column index, instead of trying the Range(Cell1,[Cell2]). Still a lot for me to Learn and this interest me a lot.

Sub FormatRange()
   
Dim i As Long, r As Range
        

On Error Resume Next    'Suppress error if user presses cancel
Set r = Application.InputBox("Pick Column", , , , , , , 8)

On Error GoTo 0         'Remove the On Error Resume Next condition
If rColumn Is Nothing Then Exit Sub   'Pressed cancel

   For i = 3 To 1500
        set r = cells(i,r.column)     '>>Syntax to utilize Column Index Number
        If r.Value <> "" Then r.Interior.Color = vbYellow
   Next i

End Sub

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .