VBA Selecting cell using the result of another sub + Row number

jozefos

New Member
Joined
Nov 15, 2011
Messages
33
Hi,

I'm trying to write code to select a cell based on the result of a previous sub (which gives me a column) + row number, and copy the contents of that cell. I have tried referencing a cell and moving a certain number of rows down to reach the desired cell but then the copy function will only select the initial cell.

I'm new to this (started last Thursday) so if this code is a mess then you know why:

Sub Cheese()
Dim Column As String
Column = "AD"
If OptionButton47 = True Then
Call M2
ElseIf OptionButton46 = True Then
Call M25
End If
End Sub

Sub M25()
Sheet83.Range("D8") = LengthTextBox.Value
Sheet83.Range("Column", 8).Select
Call CopyCell
End Sub

In this instance I'm trying to select AD8 (which changes based on the value of D8), but there are many more permutations.

If anyone could help I'd be super grateful!
 

Some videos you may like

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Gary McMaster

Well-known Member
Joined
Feb 8, 2009
Messages
1,977
It is not a good idea to use words like "Column" as a variable name. When you are in the VBA IDE, press F2 to bring up the "Object Browser". In the "Object Browser" you can search for a word you intend to use as a variable. If that exact word is found it will be a good idea to choose something else.

I don't think your variable "Column" is visible to the "M25" procedure. See the VBA help topics on "Variable Scope". I believe you should be either passing the "Column" variable (after you change the name) to the "M25" procedure or declaring the "Column" variable as a "Global" variable. See the "Public" keyword in VBA help.

Finally, your cell address should be a concatenation of the column and row Column & 8 rather than Column, 8

Here's a sample that I hope will point you in the right direction:


Code:
Option Explicit

Public Sub Cheese()

Dim sColumn As String ' Press F2 when in the VBA IDE. Avoid using words found therein as variable names

sColumn = "AD"

M25 sColumn ' Pass the sColumn variable to the "M25" procedure

End Sub

Private Sub M25(sColumn As String)

'Change sheet name to suit
Worksheets("Sheet2").Range(sColumn & 8).Select ' No comma ... concatenate sColumn and row # instead

End Sub
Hope it helps.

Gary
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,122,469
Messages
5,596,322
Members
414,053
Latest member
Dual Showman

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
Top