Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 408
Default 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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default 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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default 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







  #5   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 408
Default 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






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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









  #7   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 408
Default 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






  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default 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





  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default 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






  #10   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 408
Default 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








  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default 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








Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assign value to a variable name Gnarlodious Excel Discussion (Misc queries) 2 August 20th 06 07:10 AM
Assign value to variable rroach Excel Discussion (Misc queries) 1 July 13th 05 05:24 AM
Variable VB to assign new sheet name Kevin M Excel Worksheet Functions 0 June 17th 05 12:15 AM
Sum Cells and assign to variable Kitty Excel Programming 2 September 16th 04 08:42 PM
VBA routine to assign first name only to a variable fitful_thought Excel Programming 6 August 3rd 04 01:39 PM


All times are GMT +1. The time now is 11:37 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"