Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default String to code

Hello,

Probably a stupid question, but I don't seem to be able to fix it myself.

I have build a line of code in VBA, based on some cell content (selection of
sheets in a workbook), and now I have, in a string-defined variable, the
appropriate code, which I can check with debug.print. The variable, named
txt1, contains the text

Sheets(Array("Data02", "Data03")).Move after:=Sheets("StartAkk")

which is actually what I want to do (the actual sheet names selected will
change at runtime according to some "flags" set in a worksheet).

But now I need to execute that line of code, ie., "activate" the content of
txt1.

How do I do that ?


Thank you very much in advance !


Morten Jansen

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default String to code

You can't build a line of code dynamically and then executed it in most
cases.

If the problem is you need to build the array dynamically, rather than
concatenating the string together do this

Dim myArray as Varian
Dim sh as worksheet
for each sh in worksheets
if somecondition then
if not isarray(myArray) then
redim myArray(0 to 0)
myArray(0) = sh.name
else
redim preserve myArray(0 to ubound(myArray,1)+1)
myArray(ubound(myArray)) = sh.name
end if
end if
Next
Sheets(myArray).Move after:=Sheets("StartAkk")

--
Regards,
Tom Ogilvy


"Factivator" wrote in message
...
Hello,

Probably a stupid question, but I don't seem to be able to fix it myself.

I have build a line of code in VBA, based on some cell content (selection

of
sheets in a workbook), and now I have, in a string-defined variable, the
appropriate code, which I can check with debug.print. The variable, named
txt1, contains the text

Sheets(Array("Data02", "Data03")).Move after:=Sheets("StartAkk")

which is actually what I want to do (the actual sheet names selected will
change at runtime according to some "flags" set in a worksheet).

But now I need to execute that line of code, ie., "activate" the content

of
txt1.

How do I do that ?


Thank you very much in advance !


Morten Jansen



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default String to code

Hello,

Thank you very much for your answer. It puzzles me a bit, though, as it
seems quite reasonable for me as a novice VBA user to want to do exactly what
I try to do. I will, however, try to adapt your frame to my use, thanks a
lot. Pls. find below my code that can't be used further, just for your info.

Best regards,

Morten Jansen
-----
Sub Udvaelg()
' Procedure for preparing sheets in workbook for "3D-summing":
' Arrange the selected sheets (selection is in sheet "lister", selected
sheet names have a "1" in the next column, other sheets have a "0")
' between sheets "StartAkk" and "SlutAkk"

Dim AktArk as Variant
Dim T1 As Integer
Dim Txt1 As String
Dim Txt2 As String

Sheets(Array("SlutAkk")).Move after:=Sheets("StartAkk")

ActiveWorkbook.Names.Add Name:="AktiveArk",
RefersToR1C1:="=OFFSET(Lister!R2C2,0,0,COUNTA(List er!R2C2:R65536C2),2)"
AktArk = Worksheets("Lister").Range("AktiveArk")

Txt1 = "Sheets(Array("

For T1 = 1 To UBound(AktArk)
If AktArk(T1, 2) = 1 Then Txt1 = Txt1 & Chr(34) & AktArk(T1, 1) & Chr(34) &
", "
Next T1

Txt2 = ")).Move after:=Sheets(" & Chr(34) & "StartAkk" & Chr(34) & ")"
Txt1 = WorksheetFunction.Replace(Txt1, Len(Txt1) - 1, 1, Txt2)

Debug.Print Txt1

' ... Application.Run Txt1 ...

End Sub

"Tom Ogilvy" wrote:

You can't build a line of code dynamically and then executed it in most
cases.

If the problem is you need to build the array dynamically, rather than
concatenating the string together do this

Dim myArray as Varian
Dim sh as worksheet
for each sh in worksheets
if somecondition then
if not isarray(myArray) then
redim myArray(0 to 0)
myArray(0) = sh.name
else
redim preserve myArray(0 to ubound(myArray,1)+1)
myArray(ubound(myArray)) = sh.name
end if
end if
Next
Sheets(myArray).Move after:=Sheets("StartAkk")

--
Regards,
Tom Ogilvy


"Factivator" wrote in message
...
Hello,

Probably a stupid question, but I don't seem to be able to fix it myself.

I have build a line of code in VBA, based on some cell content (selection

of
sheets in a workbook), and now I have, in a string-defined variable, the
appropriate code, which I can check with debug.print. The variable, named
txt1, contains the text

Sheets(Array("Data02", "Data03")).Move after:=Sheets("StartAkk")

which is actually what I want to do (the actual sheet names selected will
change at runtime according to some "flags" set in a worksheet).

But now I need to execute that line of code, ie., "activate" the content

of
txt1.

How do I do that ?


Thank you very much in advance !


Morten Jansen




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
string contra VBA code [email protected] Excel Worksheet Functions 8 July 14th 09 01:48 AM
code to remove rows containing a character string kevin Excel Discussion (Misc queries) 4 November 20th 08 03:06 PM
convert text string to a number code ramon Excel Discussion (Misc queries) 5 April 11th 07 05:02 PM
Excel XP VBA code to search all macro code in Excel module for specific search string criteria Ed[_18_] Excel Programming 4 May 20th 04 02:08 PM
Excel XP VBA code to search all macro code in Excel module for specific search string criteria Frank Kabel Excel Programming 0 May 19th 04 08:11 PM


All times are GMT +1. The time now is 10:43 AM.

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"