ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Need help with setting variables (https://www.excelbanter.com/excel-programming/376417-need-help-setting-variables.html)

erikkeith via OfficeKB.com

Need help with setting variables
 
I want to set variables so I can use them in the following way:

A = James
B = Ron
C = Tony

Range("M33").Select
If Range("AB22").Value = "N" Then
ActiveCell = "A"
Else: ActiveCell = "Alternate"
End If

What syntax to I use to make this run in VBA?

--
Message posted via http://www.officekb.com


NickHK

Need help with setting variables
 
Not sure what you are trying to achieve, but
- It is a good idea, in the VBE, to set ToolsOptionsEditorcheck Require
Variable Declaration
This will put "Option Explicit" at the top of all modules, forcing you to
declare them. Check the Help.
And add Option Explicit manually to your current module.

You mean something like

Dim A as string,B as string,C as string
A = "James"
B = "Ron"
C = "Tony"

Range("M33").Select
If Range("AB22").Value = "N" Then
ActiveCell = A
Else
ActiveCell = "Alternate"
End If

Like this though, A, B & C are not very variable, as all the values are
hard-coded, so you do not really gain anything by assigning the variables.

Depends what the source of your values is.

NickHK

"erikkeith via OfficeKB.com" <u13156@uwe wrote in message
news:68a0394555271@uwe...
I want to set variables so I can use them in the following way:

A = James
B = Ron
C = Tony

Range("M33").Select
If Range("AB22").Value = "N" Then
ActiveCell = "A"
Else: ActiveCell = "Alternate"
End If

What syntax to I use to make this run in VBA?

--
Message posted via http://www.officekb.com




Bob Phillips

Need help with setting variables
 
Dim sNames

sNames = Array("James", "Ron", "Tony")

Range("M33").Select
If Range("AB22").Value = "N" Then
ActiveCell.Value = sNames(0)
Else: ActiveCell.Value = sNames(1)
End If



--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"erikkeith via OfficeKB.com" <u13156@uwe wrote in message
news:68a0394555271@uwe...
I want to set variables so I can use them in the following way:

A = James
B = Ron
C = Tony

Range("M33").Select
If Range("AB22").Value = "N" Then
ActiveCell = "A"
Else: ActiveCell = "Alternate"
End If

What syntax to I use to make this run in VBA?

--
Message posted via http://www.officekb.com




erikkeith via OfficeKB.com

Need help with setting variables
 
What if I wanted to add to sNames together in a range?

For instance if I Dim sNames and Dim sNames2 and then want to do this:

Range("sNames + sNames2").Select how would I write the syntax?

Bob Phillips wrote:
Dim sNames

sNames = Array("James", "Ron", "Tony")

Range("M33").Select
If Range("AB22").Value = "N" Then
ActiveCell.Value = sNames(0)
Else: ActiveCell.Value = sNames(1)
End If

I want to set variables so I can use them in the following way:

[quoted text clipped - 9 lines]

What syntax to I use to make this run in VBA?


--
Message posted via http://www.officekb.com


erikkeith via OfficeKB.com

Need help with setting variables
 
What if I want to attach a Dim to a cell? For instance, Range("ABDim").
select How would I do it?

NickHK wrote:
Not sure what you are trying to achieve, but
- It is a good idea, in the VBE, to set ToolsOptionsEditorcheck Require
Variable Declaration
This will put "Option Explicit" at the top of all modules, forcing you to
declare them. Check the Help.
And add Option Explicit manually to your current module.

You mean something like

Dim A as string,B as string,C as string
A = "James"
B = "Ron"
C = "Tony"

Range("M33").Select
If Range("AB22").Value = "N" Then
ActiveCell = A
Else
ActiveCell = "Alternate"
End If

Like this though, A, B & C are not very variable, as all the values are
hard-coded, so you do not really gain anything by assigning the variables.

Depends what the source of your values is.

NickHK

I want to set variables so I can use them in the following way:

[quoted text clipped - 9 lines]

What syntax to I use to make this run in VBA?


--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200611/1


Bob Phillips

Need help with setting variables
 
Activecell.Value = "James" & " " & "Reed"

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"erikkeith via OfficeKB.com" <u13156@uwe wrote in message
news:68a2c501ef16d@uwe...
What if I wanted to add to sNames together in a range?

For instance if I Dim sNames and Dim sNames2 and then want to do this:

Range("sNames + sNames2").Select how would I write the syntax?

Bob Phillips wrote:
Dim sNames

sNames = Array("James", "Ron", "Tony")

Range("M33").Select
If Range("AB22").Value = "N" Then
ActiveCell.Value = sNames(0)
Else: ActiveCell.Value = sNames(1)
End If

I want to set variables so I can use them in the following way:

[quoted text clipped - 9 lines]

What syntax to I use to make this run in VBA?


--
Message posted via http://www.officekb.com





All times are GMT +1. The time now is 02:59 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com