Can we use offset using column name instead of number?

NDMDRB

Board Regular
Joined
Jun 20, 2016
Messages
164
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I'm wondering if there is a way to use column name instead of column number with offset function

Let's say we have the following sheet

A​
B​
c​
1
code
Plate
Type
2D1123Dozer
3E1456Excavator

VBA Code:
Dim ws as Worksheet
Set ws = Sheet5

If Me.Search_Code_BT.Object = True Then
    Set f = ws.Range("C:C").Find(what:=Me.Search_CMB.value, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
    If f Is Nothing Then Exit Sub
    Me.Plate_TXT = f.Offset(0, 1)
    Me.Type_CMB = f.Offset(0, 2)
    '..... the same for other textboxes
ElseIf Me.Search_Plate_BT.Object = True Then
    Set f = ws.Range("D:D").Find(what:=Me.Search_CMB.value, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
    If f Is Nothing Then Exit Sub
    Me.Code_TXT = f.Offset(0, -1)
    Me.Type_CMB = f.Offset(0, 1)
    '..... the same for other textboxes
End if
End If

In this case I have to use different Offset for each condition

I'm wondering if there is a shortcut to use the Offset one time after the If Else statement as the example below

VBA Code:
If Me.Search_Code_BT.Object = True Then
    Set f = ws.Range("C:C").Find(what:=Me.Search_CMB.value, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
    If f Is Nothing Then Exit Sub
ElseIf Me.Search_Plate_BT.Object = True Then
    Set f = ws.Range("D:D").Find(what:=Me.Search_CMB.value, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
End If

    Me.Plate_TXT = f.Offset(0, B)
    Me.Type_CMB = f.Offset(0, C)
    '..... the same for other textboxes

I know that the second code doesn't work, but I'm not sure if it's possible in another way
Or if you have another way to get what I need using another function, it will be appreciated to learn it

Many thanks in advanced
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
You can convert a single letter to the column number using this code:
VBA Code:
Sub test()
ii = "A"
yy = Asc(ii) - 64
MsgBox yy



End Sub
so you could try:
VBA Code:
Me.Plate_TXT = f.Offset(0, Asc("B") - 64)
 
Upvote 0
Maybe
VBA Code:
    Me.Plate_TXT = Application.Intersect(f.EntireRow, Range("B:B"))
    Me.Type_CMB = Application.Intersect(f.EntireRow, Range("C:C"))
 
Upvote 0
Solution
You can convert a single letter to the column number using this code:
VBA Code:
Sub test()
ii = "A"
yy = Asc(ii) - 64
MsgBox yy



End Sub
so you could try:
VBA Code:
Me.Plate_TXT = f.Offset(0, Asc("B") - 64)

Thank you offthelip,

It works but the same I have to use this for Code search
VBA Code:
Me.Plate_TXT = f.Offset(0, Asc("B") - 67)

And this for Plate search
VBA Code:
Me.Plate_TXT = f.Offset(0, Asc("B") - 68)
 
Upvote 0
Maybe
VBA Code:
    Me.Plate_TXT = Application.Intersect(f.EntireRow, Range("B:B"))
    Me.Type_CMB = Application.Intersect(f.EntireRow, Range("C:C"))
Thank you Tetra201,
This works great as I need
Just one small issue and not sure if we can do it
With your code, I have to select the needed sheet (Sheet5) to make it done, so just wondering if you can make it even without selecting this sheet it also works
 
Upvote 0
No worksheet activating is necessary. The code will work without the need to select or activate the worksheet.
If Sheet5 is not active this error happened
Method 'Intersect' of objects '_Application' failed
 
Upvote 0
Maybe because you didn't refer to the worksheet at this part
VBA Code:
Range("B:B")
Try refer to the worksheet before the range like that
Code:
Sheet1.Range("B:B")
 
Upvote 0
Hi again

I'm trying to get comment text from a cell to a textbox as below code, but it didn't work, any advices?

VBA Code:
Me.Plate_TXT = Application.Intersect(f.EntireRow, ws.Range("B:B"))

If Not Application.Intersect(f.EntireRow, ws.Range("B:B").Comment.Text) Is Nothing Then
    Me.Attached_TXT = Application.Intersect(f.EntireRow, ws.Range("B:B").Comment.Text)
End If
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,825
Members
449,096
Latest member
Erald

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