PC Review


Reply
Thread Tools Rate Thread

Create N variables

 
 
Jive
Guest
Posts: n/a
 
      18th Aug 2009
I have a piece of vba code which can be simplified to

Dim Piece As Picture
For Each Piece In ActiveSheet.Pictures
******
******
******
Next Piece

Within the piece iteration I wish to retrieve some information about each
picture and store it as a variable i.e. variable(1.....n) =
Piece(1.....n).topleftcell. I am unsure how to automatically generate
variable sequence to the correct number of variables. i.e. so that it stores
a new variable and value for later use off of each loop.

VBA is amusing, there is always something new to learn and when you do you
only have another two things to learn afther that so please help. thanks in
advance.
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      18th Aug 2009
Option Explicit
Sub testme()

Dim picArray() As String
Dim wks As Worksheet
Dim Piece As Picture
Dim pCtr As Long

Set wks = ActiveSheet

With wks
If .Pictures.Count = 0 Then
MsgBox "no pictures!"
Exit Sub
End If

ReDim picArray(1 To .Pictures.Count)

pCtr = 0
For Each Piece In .Pictures
pCtr = pCtr + 1
picArray(pCtr) = Piece.TopLeftCell.Address(0, 0)
Next Piece
End With
End Sub

Another way is to just loop through the pictures -- and maybe keep track of more
stuff (like the name???):


Option Explicit
Sub testme2()

Dim picArray() As String
Dim wks As Worksheet
Dim pCtr As Long

Set wks = ActiveSheet

With wks
If .Pictures.Count = 0 Then
MsgBox "no pictures!"
Exit Sub
End If

ReDim picArray(1 To 2, 1 To .Pictures.Count)

For pCtr = 1 To .Pictures.Count
picArray(pCtr, 1) = .Pictures(pCtr).Name
picArray(pCtr, 2) = .Pictures(pCtr).TopLeftCell.Address(0, 0)
Next pCtr
End With
End Sub




Jive wrote:
>
> I have a piece of vba code which can be simplified to
>
> Dim Piece As Picture
> For Each Piece In ActiveSheet.Pictures
> ******
> ******
> ******
> Next Piece
>
> Within the piece iteration I wish to retrieve some information about each
> picture and store it as a variable i.e. variable(1.....n) =
> Piece(1.....n).topleftcell. I am unsure how to automatically generate
> variable sequence to the correct number of variables. i.e. so that it stores
> a new variable and value for later use off of each loop.
>
> VBA is amusing, there is always something new to learn and when you do you
> only have another two things to learn afther that so please help. thanks in
> advance.


--

Dave Peterson
 
Reply With Quote
 
Jive
Guest
Posts: n/a
 
      18th Aug 2009
Thanks Dave

I cant test it until tomorrow but i think i understand how the variables are
assembled. In your second option should the array be

ReDim picArray(1 To .Pictures.Count, 1 To 2)

or should the order change as you have listed it? if so can you explain why
or maybe it doesnt matter which way around?

Regards

Antony

"Dave Peterson" wrote:

> Option Explicit
> Sub testme()
>
> Dim picArray() As String
> Dim wks As Worksheet
> Dim Piece As Picture
> Dim pCtr As Long
>
> Set wks = ActiveSheet
>
> With wks
> If .Pictures.Count = 0 Then
> MsgBox "no pictures!"
> Exit Sub
> End If
>
> ReDim picArray(1 To .Pictures.Count)
>
> pCtr = 0
> For Each Piece In .Pictures
> pCtr = pCtr + 1
> picArray(pCtr) = Piece.TopLeftCell.Address(0, 0)
> Next Piece
> End With
> End Sub
>
> Another way is to just loop through the pictures -- and maybe keep track of more
> stuff (like the name???):
>
>
> Option Explicit
> Sub testme2()
>
> Dim picArray() As String
> Dim wks As Worksheet
> Dim pCtr As Long
>
> Set wks = ActiveSheet
>
> With wks
> If .Pictures.Count = 0 Then
> MsgBox "no pictures!"
> Exit Sub
> End If
>
> ReDim picArray(1 To 2, 1 To .Pictures.Count)
>
> For pCtr = 1 To .Pictures.Count
> picArray(pCtr, 1) = .Pictures(pCtr).Name
> picArray(pCtr, 2) = .Pictures(pCtr).TopLeftCell.Address(0, 0)
> Next pCtr
> End With
> End Sub
>
>
>
>
> Jive wrote:
> >
> > I have a piece of vba code which can be simplified to
> >
> > Dim Piece As Picture
> > For Each Piece In ActiveSheet.Pictures
> > ******
> > ******
> > ******
> > Next Piece
> >
> > Within the piece iteration I wish to retrieve some information about each
> > picture and store it as a variable i.e. variable(1.....n) =
> > Piece(1.....n).topleftcell. I am unsure how to automatically generate
> > variable sequence to the correct number of variables. i.e. so that it stores
> > a new variable and value for later use off of each loop.
> >
> > VBA is amusing, there is always something new to learn and when you do you
> > only have another two things to learn afther that so please help. thanks in
> > advance.

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      18th Aug 2009
Why do that! You've always got the latest up to date properties of all
pictures in the collection "mySheet.Pictures". The only reason to assign
their properties elsewhere would be to keep a record of what they were
before changing them - is that the objective?

Regards,
Peter T




"Jive" <(E-Mail Removed)> wrote in message
news:302454DE-4FFC-4D4D-958B-(E-Mail Removed)...
>I have a piece of vba code which can be simplified to
>
> Dim Piece As Picture
> For Each Piece In ActiveSheet.Pictures
> ******
> ******
> ******
> Next Piece
>
> Within the piece iteration I wish to retrieve some information about each
> picture and store it as a variable i.e. variable(1.....n) =
> Piece(1.....n).topleftcell. I am unsure how to automatically generate
> variable sequence to the correct number of variables. i.e. so that it
> stores
> a new variable and value for later use off of each loop.
>
> VBA is amusing, there is always something new to learn and when you do you
> only have another two things to learn afther that so please help. thanks
> in
> advance.



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      18th Aug 2009
You're right.

This code would work:

ReDim picArray(1 To .Pictures.Count, 1 To 2)

For pCtr = 1 To .Pictures.Count
picArray(pCtr, 1) = .Pictures(pCtr).Name
picArray(pCtr, 2) = .Pictures(pCtr).TopLeftCell.Address(0, 0)
Next pCtr

But so would this code:

ReDim picArray(1 To 2, 1 To .Pictures.Count)

For pCtr = 1 To .Pictures.Count
picArray(1, pCtr) = .Pictures(pCtr).Name
picArray(2, pCtr) = .Pictures(pCtr).TopLeftCell.Address(0, 0)
Next pCtr
End With

The difference would be how those arrays are structured.

The top one is equivalent to x Rows by 2 columns.
The bottom one is equivalent to 2 rows by x columns.

And as long as you use them correctly <vbg>, it wouldn't matter which one you
used!


Jive wrote:
>
> Thanks Dave
>
> I cant test it until tomorrow but i think i understand how the variables are
> assembled. In your second option should the array be
>
> ReDim picArray(1 To .Pictures.Count, 1 To 2)
>
> or should the order change as you have listed it? if so can you explain why
> or maybe it doesnt matter which way around?
>
> Regards
>
> Antony
>
> "Dave Peterson" wrote:
>
> > Option Explicit
> > Sub testme()
> >
> > Dim picArray() As String
> > Dim wks As Worksheet
> > Dim Piece As Picture
> > Dim pCtr As Long
> >
> > Set wks = ActiveSheet
> >
> > With wks
> > If .Pictures.Count = 0 Then
> > MsgBox "no pictures!"
> > Exit Sub
> > End If
> >
> > ReDim picArray(1 To .Pictures.Count)
> >
> > pCtr = 0
> > For Each Piece In .Pictures
> > pCtr = pCtr + 1
> > picArray(pCtr) = Piece.TopLeftCell.Address(0, 0)
> > Next Piece
> > End With
> > End Sub
> >
> > Another way is to just loop through the pictures -- and maybe keep track of more
> > stuff (like the name???):
> >
> >
> > Option Explicit
> > Sub testme2()
> >
> > Dim picArray() As String
> > Dim wks As Worksheet
> > Dim pCtr As Long
> >
> > Set wks = ActiveSheet
> >
> > With wks
> > If .Pictures.Count = 0 Then
> > MsgBox "no pictures!"
> > Exit Sub
> > End If
> >
> > ReDim picArray(1 To 2, 1 To .Pictures.Count)
> >
> > For pCtr = 1 To .Pictures.Count
> > picArray(pCtr, 1) = .Pictures(pCtr).Name
> > picArray(pCtr, 2) = .Pictures(pCtr).TopLeftCell.Address(0, 0)
> > Next pCtr
> > End With
> > End Sub
> >
> >
> >
> >
> > Jive wrote:
> > >
> > > I have a piece of vba code which can be simplified to
> > >
> > > Dim Piece As Picture
> > > For Each Piece In ActiveSheet.Pictures
> > > ******
> > > ******
> > > ******
> > > Next Piece
> > >
> > > Within the piece iteration I wish to retrieve some information about each
> > > picture and store it as a variable i.e. variable(1.....n) =
> > > Piece(1.....n).topleftcell. I am unsure how to automatically generate
> > > variable sequence to the correct number of variables. i.e. so that it stores
> > > a new variable and value for later use off of each loop.
> > >
> > > VBA is amusing, there is always something new to learn and when you do you
> > > only have another two things to learn afther that so please help. thanks in
> > > advance.

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
Jive
Guest
Posts: n/a
 
      19th Aug 2009
Yes that is the objective. I'm looking to construct a record of what
pictures have been moved and where.

I was planning to look for changes in the vlaues and then list out the
sequence of moves.

Are you asking as you can suggest a better approach?

"Peter T" wrote:

> Why do that! You've always got the latest up to date properties of all
> pictures in the collection "mySheet.Pictures". The only reason to assign
> their properties elsewhere would be to keep a record of what they were
> before changing them - is that the objective?
>
> Regards,
> Peter T
>
>
>
>
> "Jive" <(E-Mail Removed)> wrote in message
> news:302454DE-4FFC-4D4D-958B-(E-Mail Removed)...
> >I have a piece of vba code which can be simplified to
> >
> > Dim Piece As Picture
> > For Each Piece In ActiveSheet.Pictures
> > ******
> > ******
> > ******
> > Next Piece
> >
> > Within the piece iteration I wish to retrieve some information about each
> > picture and store it as a variable i.e. variable(1.....n) =
> > Piece(1.....n).topleftcell. I am unsure how to automatically generate
> > variable sequence to the correct number of variables. i.e. so that it
> > stores
> > a new variable and value for later use off of each loop.
> >
> > VBA is amusing, there is always something new to learn and when you do you
> > only have another two things to learn afther that so please help. thanks
> > in
> > advance.

>
>
>

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      19th Aug 2009
OK if it's to keep a record of picture properties "were" indeed you are
going the right way and use Dave's examples. I had wrongly speculated the
intension was to use the properties immediately.

Only for "amusement", as you had put it that way, here's another approach
(not better just different)

Private Type tPicProps
Name As String
Left As Double
Top As Double
rTLcell As Range
End Type

Private arrPicProps() As tPicProps ' at module level for later recall

Sub LetPicProps()
Dim i As Long, cntPics As Long
Dim pic As Picture

cntPics = ActiveSheet.Pictures.Count
If cntPics = 0 Then
Exit Sub
End If

ReDim arrPicProps(1 To cntPics)
For i = 1 To cntPics
Set pic = ActiveSheet.Pictures(i)
With arrPicProps(i)
Set .rTLcell = pic.TopLeftCell
.Left = pic.Left
.Top = pic.Top
.Name = pic.Name
End With
Next
End Sub

Sub GetOldPicProps()

For i = 1 To UBound(arrPicProps)
With arrPicProps(i)
Debug.Print .Name, .rTLcell.Address, .Left, .Top
End With
Next

End Sub

Sub ErasePicProps() ' call this when done
Erase arrPicProps
End Sub

' note the rTLcell range-object is not really a "record", it could have been
be cut and moved in the interim. Just to illustrate how you can use a "Type"

Regards,
Peter T


"Jive" <(E-Mail Removed)> wrote in message
news7CC526A-7D7A-4D7B-BCE7-(E-Mail Removed)...
> Yes that is the objective. I'm looking to construct a record of what
> pictures have been moved and where.
>
> I was planning to look for changes in the vlaues and then list out the
> sequence of moves.
>
> Are you asking as you can suggest a better approach?
>
> "Peter T" wrote:
>
>> Why do that! You've always got the latest up to date properties of all
>> pictures in the collection "mySheet.Pictures". The only reason to assign
>> their properties elsewhere would be to keep a record of what they were
>> before changing them - is that the objective?
>>
>> Regards,
>> Peter T
>>
>>
>>
>>
>> "Jive" <(E-Mail Removed)> wrote in message
>> news:302454DE-4FFC-4D4D-958B-(E-Mail Removed)...
>> >I have a piece of vba code which can be simplified to
>> >
>> > Dim Piece As Picture
>> > For Each Piece In ActiveSheet.Pictures
>> > ******
>> > ******
>> > ******
>> > Next Piece
>> >
>> > Within the piece iteration I wish to retrieve some information about
>> > each
>> > picture and store it as a variable i.e. variable(1.....n) =
>> > Piece(1.....n).topleftcell. I am unsure how to automatically generate
>> > variable sequence to the correct number of variables. i.e. so that it
>> > stores
>> > a new variable and value for later use off of each loop.
>> >
>> > VBA is amusing, there is always something new to learn and when you do
>> > you
>> > only have another two things to learn afther that so please help.
>> > thanks
>> > in
>> > advance.

>>
>>
>>



 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      19th Aug 2009
typo

> OK if it's to keep a record of picture properties "were" indeed you are
> ...


hopefully this makes more sense -

OK, if it's to keep a record of *what* picture properties "were", indeed you
are ...

Peter T


 
Reply With Quote
 
Jive
Guest
Posts: n/a
 
      19th Aug 2009
I think I have a new strapline for VB

One problem, infinate solutions.

Some new uses of proceedures in there for me to adapt. Sure they will come
in handy later on. Still cant decide if i should get some formal training in
visual basic or just keep learning as i go.

"Peter T" wrote:

> OK if it's to keep a record of picture properties "were" indeed you are
> going the right way and use Dave's examples. I had wrongly speculated the
> intension was to use the properties immediately.
>
> Only for "amusement", as you had put it that way, here's another approach
> (not better just different)
>
> Private Type tPicProps
> Name As String
> Left As Double
> Top As Double
> rTLcell As Range
> End Type
>
> Private arrPicProps() As tPicProps ' at module level for later recall
>
> Sub LetPicProps()
> Dim i As Long, cntPics As Long
> Dim pic As Picture
>
> cntPics = ActiveSheet.Pictures.Count
> If cntPics = 0 Then
> Exit Sub
> End If
>
> ReDim arrPicProps(1 To cntPics)
> For i = 1 To cntPics
> Set pic = ActiveSheet.Pictures(i)
> With arrPicProps(i)
> Set .rTLcell = pic.TopLeftCell
> .Left = pic.Left
> .Top = pic.Top
> .Name = pic.Name
> End With
> Next
> End Sub
>
> Sub GetOldPicProps()
>
> For i = 1 To UBound(arrPicProps)
> With arrPicProps(i)
> Debug.Print .Name, .rTLcell.Address, .Left, .Top
> End With
> Next
>
> End Sub
>
> Sub ErasePicProps() ' call this when done
> Erase arrPicProps
> End Sub
>
> ' note the rTLcell range-object is not really a "record", it could have been
> be cut and moved in the interim. Just to illustrate how you can use a "Type"
>
> Regards,
> Peter T
>
>
> "Jive" <(E-Mail Removed)> wrote in message
> news7CC526A-7D7A-4D7B-BCE7-(E-Mail Removed)...
> > Yes that is the objective. I'm looking to construct a record of what
> > pictures have been moved and where.
> >
> > I was planning to look for changes in the vlaues and then list out the
> > sequence of moves.
> >
> > Are you asking as you can suggest a better approach?
> >
> > "Peter T" wrote:
> >
> >> Why do that! You've always got the latest up to date properties of all
> >> pictures in the collection "mySheet.Pictures". The only reason to assign
> >> their properties elsewhere would be to keep a record of what they were
> >> before changing them - is that the objective?
> >>
> >> Regards,
> >> Peter T
> >>
> >>
> >>
> >>
> >> "Jive" <(E-Mail Removed)> wrote in message
> >> news:302454DE-4FFC-4D4D-958B-(E-Mail Removed)...
> >> >I have a piece of vba code which can be simplified to
> >> >
> >> > Dim Piece As Picture
> >> > For Each Piece In ActiveSheet.Pictures
> >> > ******
> >> > ******
> >> > ******
> >> > Next Piece
> >> >
> >> > Within the piece iteration I wish to retrieve some information about
> >> > each
> >> > picture and store it as a variable i.e. variable(1.....n) =
> >> > Piece(1.....n).topleftcell. I am unsure how to automatically generate
> >> > variable sequence to the correct number of variables. i.e. so that it
> >> > stores
> >> > a new variable and value for later use off of each loop.
> >> >
> >> > VBA is amusing, there is always something new to learn and when you do
> >> > you
> >> > only have another two things to learn afther that so please help.
> >> > thanks
> >> > in
> >> > advance.
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to create invoice with many variables M Brown Microsoft Access VBA Modules 1 25th Feb 2010 06:23 PM
Create Enviroment variables Jason Microsoft C# .NET 1 5th Mar 2007 04:41 PM
Create possible combinations from three variables =?Utf-8?B?Sm9lRA==?= Microsoft Excel Misc 3 20th Apr 2006 10:20 PM
How to create these variables? Shapper Microsoft ASP .NET 0 22nd Apr 2005 02:05 AM
Create Message Box containing different variables. grumpyoldsod@freeuk.com Microsoft Excel Programming 2 8th Apr 2005 06:11 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:05 AM.