ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   assign variable (https://www.excelbanter.com/excel-programming/312143-assign-variable.html)

Tim

assign variable
 
If a cell in a sheet is "LL-CP-0163" how can i set a variable in a macro
equal to 0163 or the number part of that cell. also how can i set another
variable equal to 0164 or the previous variable + 1


Norman Jones

assign variable
 
Hi Tim.

One way:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStr(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i
End Sub

---
Regards,
Norman



"Tim" wrote in message
...
If a cell in a sheet is "LL-CP-0163" how can i set a variable in a macro
equal to 0163 or the number part of that cell. also how can i set another
variable equal to 0164 or the previous variable + 1




Bob Phillips[_6_]

assign variable
 
If there are 2 - as per the example, use InstrRev

--

HTH

RP

"Norman Jones" wrote in message
...
Hi Tim.

One way:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStr(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i
End Sub

---
Regards,
Norman



"Tim" wrote in message
...
If a cell in a sheet is "LL-CP-0163" how can i set a variable in a macro
equal to 0163 or the number part of that cell. also how can i set

another
variable equal to 0164 or the previous variable + 1






Norman Jones

assign variable
 
Hi Bob,

Yes, mia culpa, the consequence of not using the OP's sanple data.!

Thank you.

---
Regards,
Norman



"Bob Phillips" wrote in message
...
If there are 2 - as per the example, use InstrRev

--

HTH

RP

"Norman Jones" wrote in message
...
Hi Tim.

One way:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStr(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i
End Sub

---
Regards,
Norman



"Tim" wrote in message
...
If a cell in a sheet is "LL-CP-0163" how can i set a variable in a
macro
equal to 0163 or the number part of that cell. also how can i set

another
variable equal to 0164 or the previous variable + 1








Tim

assign variable
 
This looks good Norman but will
sStr = "CP - 163"
pos = InStr(sStr, "-")
take into acount that "CP - 163" is actually"LL-CP - 163" ?
the other ? is how can i use the variables "pos"=(163?) and "i"=(164) in
such code as

Windows("LL-CP-"pos".xls").Activate
Range("F3:H3").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Completions LL Register 2004-09-030.xls").Activate
Range("F"i"").Select
Selection.PasteSpecial Paste:=xlValues,
Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Windows("LL-CP-"pos".xls").Activate
Range("B12:J18").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Completions LL Register 2004-09-030.xls").Activate
Range("G"i"").Select
Selection.PasteSpecial Paste:=xlValues,
Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False

"Norman Jones" wrote:

Hi Tim.

One way:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStr(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i
End Sub

---
Regards,
Norman



"Tim" wrote in message
...
If a cell in a sheet is "LL-CP-0163" how can i set a variable in a macro
equal to 0163 or the number part of that cell. also how can i set another
variable equal to 0164 or the previous variable + 1


"Norman Jones" wrote:

Hi Tim.

One way:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStr(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i
End Sub

---
Regards,
Norman



"Tim" wrote in message
...
If a cell in a sheet is "LL-CP-0163" how can i set a variable in a macro
equal to 0163 or the number part of that cell. also how can i set another
variable equal to 0164 or the previous variable + 1





Bob Phillips[_6_]

assign variable
 
It's a pleasure Norman :-)

--

HTH

RP

"Norman Jones" wrote in message
...
Hi Bob,

Yes, mia culpa, the consequence of not using the OP's sanple data.!

Thank you.

---
Regards,
Norman



"Bob Phillips" wrote in message
...
If there are 2 - as per the example, use InstrRev

--

HTH

RP

"Norman Jones" wrote in message
...
Hi Tim.

One way:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStr(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i
End Sub

---
Regards,
Norman



"Tim" wrote in message
...
If a cell in a sheet is "LL-CP-0163" how can i set a variable in a
macro
equal to 0163 or the number part of that cell. also how can i set

another
variable equal to 0164 or the previous variable + 1










Tim

assign variable
 
Not sure what you mean Bob. i need two variables one which is greater than
the other by +1. The first variable needs to come from the number part of
LL-CP-0163 which is assigned to a cell and the other would be 164. similar is
i use LL-CP-0190 the second variable would be 191. Sorry i am not the best
with code.

"Bob Phillips" wrote:

If there are 2 - as per the example, use InstrRev

--

HTH

RP

"Norman Jones" wrote in message
...
Hi Tim.

One way:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStr(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i
End Sub

---
Regards,
Norman



"Tim" wrote in message
...
If a cell in a sheet is "LL-CP-0163" how can i set a variable in a macro
equal to 0163 or the number part of that cell. also how can i set

another
variable equal to 0164 or the previous variable + 1







Norman Jones

assign variable
 
Hi Tim,

As Bob indicates, I mis-read your data. Given that the required numeric
string follows the second hyphen character in your data, the InsrRev
function should be used. The suggested macro thus becomes:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStrRev(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i

End Sub


---
Regards,
Norman



"Norman Jones" wrote in message
...
Hi Tim.

One way:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStr(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i
End Sub

---
Regards,
Norman



"Tim" wrote in message
...
If a cell in a sheet is "LL-CP-0163" how can i set a variable in a macro
equal to 0163 or the number part of that cell. also how can i set another
variable equal to 0164 or the previous variable + 1






Norman Jones

assign variable
 
Hi Tim,

See my adjusted routine in the adjacent post.

Fortunately, Bob had already spotted that I had incorrectly read the
structure of your data strings.


---
Regards,
Norman



"Tim" wrote in message
...
This looks good Norman but will
sStr = "CP - 163"
pos = InStr(sStr, "-")
take into acount that "CP - 163" is actually"LL-CP - 163" ?
the other ? is how can i use the variables "pos"=(163?) and "i"=(164) in
such code as

Windows("LL-CP-"pos".xls").Activate
Range("F3:H3").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Completions LL Register 2004-09-030.xls").Activate
Range("F"i"").Select
Selection.PasteSpecial Paste:=xlValues,
Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Windows("LL-CP-"pos".xls").Activate
Range("B12:J18").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Completions LL Register 2004-09-030.xls").Activate
Range("G"i"").Select
Selection.PasteSpecial Paste:=xlValues,
Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False

"Norman Jones" wrote:

Hi Tim.

One way:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStr(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i
End Sub

---
Regards,
Norman



"Tim" wrote in message
...
If a cell in a sheet is "LL-CP-0163" how can i set a variable in a
macro
equal to 0163 or the number part of that cell. also how can i set
another
variable equal to 0164 or the previous variable + 1


"Norman Jones" wrote:

Hi Tim.

One way:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStr(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i
End Sub

---
Regards,
Norman



"Tim" wrote in message
...
If a cell in a sheet is "LL-CP-0163" how can i set a variable in a
macro
equal to 0163 or the number part of that cell. also how can i set
another
variable equal to 0164 or the previous variable + 1







Tim

assign variable
 
Thanks Norman heres what i have but it is not coded correct, here is what i
am trying to do thinking i = # and j = # -1 and what is "MsgBox i".

' Macro recorded 9/30/2004 by Husky User
'


Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long
Dim j As Long
Windows"current sheet".Activate
Range("J2").Select
sStr = "J2"
pos = InStrRev(sStr, "-")
i = Mid(sStr, pos + 1) + 1
j = i - 1
MsgBox i
MsgBox j

Windows("LL-CP-j.xls").Activate
Range("F3:H3").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Completions LL Register 2004-09-030.xls").Activate
Range("F"i"").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
End Sub

"Norman Jones" wrote:

Hi Tim,

As Bob indicates, I mis-read your data. Given that the required numeric
string follows the second hyphen character in your data, the InsrRev
function should be used. The suggested macro thus becomes:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStrRev(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i

End Sub


---
Regards,
Norman



"Norman Jones" wrote in message
...
Hi Tim.

One way:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStr(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i
End Sub

---
Regards,
Norman



"Tim" wrote in message
...
If a cell in a sheet is "LL-CP-0163" how can i set a variable in a macro
equal to 0163 or the number part of that cell. also how can i set another
variable equal to 0164 or the previous variable + 1







Norman Jones

assign variable
 
Hi Tim,

I used a MsgBox in my code merely to give you visual confirmation that the
correct value had been assigned to the i variable. It serves no other
purpose here and can be deleted.

Whilst I do not know the layout or structure of your workbooks and data.,
there are a number of syntax problems in your code. Additionally, you make
repeated selections/activations. These are rarely necessary.

Revising your code to deal with these issues, we have:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long
Dim j As Long

sStr = Sheets("current sheet").Range("J2").Value
pos = InStrRev(sStr, "-")
i = Mid(sStr, pos + 1) + 1
j = i - 1
Workbooks("LL-CP-j.xls").Sheets("SheetYYY"). _
Range("F3:H3").Copy
Workbooks("Completions LL Register 2004-09-030.xls"). _
Sheets("SheetXXX").Range("F" & i). _
PasteSpecial Paste:=xlValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
End Sub

You need to replace SheetXXX and SheetYYY with the appropriate sheet names.

Also, do you really have a workshheet named "Current Sheet"? If you do,
fine. If, however, you are endeavouring to reference the active worksheet,
replace the line:

sStr = Sheets("current sheet").Range("J2").Value

with

sStr = ActiveSheet.Range("J2").Value

Lastly, You have introduced a new variable j but there appears to be no use
made of the variable.

---
Regards,
Norman



"Tim" wrote in message
...
Thanks Norman heres what i have but it is not coded correct, here is what
i
am trying to do thinking i = # and j = # -1 and what is "MsgBox i".

' Macro recorded 9/30/2004 by Husky User
'


Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long
Dim j As Long
Windows"current sheet".Activate
Range("J2").Select
sStr = "J2"
pos = InStrRev(sStr, "-")
i = Mid(sStr, pos + 1) + 1
j = i - 1
MsgBox i
MsgBox j

Windows("LL-CP-j.xls").Activate
Range("F3:H3").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Completions LL Register 2004-09-030.xls").Activate
Range("F"i"").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=
_
False, Transpose:=False
End Sub

"Norman Jones" wrote:

Hi Tim,

As Bob indicates, I mis-read your data. Given that the required numeric
string follows the second hyphen character in your data, the InsrRev
function should be used. The suggested macro thus becomes:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStrRev(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i

End Sub


---
Regards,
Norman



"Norman Jones" wrote in message
...
Hi Tim.

One way:

Sub Atester()
Dim sStr As String
Dim pos As Long
Dim i As Long

sStr = "CP - 163"
pos = InStr(sStr, "-")
i = Mid(sStr, pos + 1) + 1
MsgBox i
End Sub

---
Regards,
Norman



"Tim" wrote in message
...
If a cell in a sheet is "LL-CP-0163" how can i set a variable in a
macro
equal to 0163 or the number part of that cell. also how can i set
another
variable equal to 0164 or the previous variable + 1










All times are GMT +1. The time now is 06:56 AM.

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