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!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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:
Upvote 0

Forum statistics

Threads
1,214,626
Messages
6,120,602
Members
448,974
Latest member
ChristineC

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