PC Review


Reply
Thread Tools Rate Thread

Array in VBA

 
 
=?Utf-8?B?Sk5X?=
Guest
Posts: n/a
 
      26th Jun 2007
I work with a program that stores excel files using encrypted names. The
program then assigns the file a recognizable name that I am able to retrieve
through VBA.

I am creating a tool to allow users to arrange a given number of workbooks
through an add-in that has a userform.

The problem: I can get everything to work if I have the encrypted file name
show up in the userform, but the user can't tell what file it is, because the
name makes no sense.

How can I display the recognizable name I retrieve through VBA but still
have the encrypted name used to resize the window.

P.S. I can get this to work if I drop all the names in an excel worksheet
and do a vlookup. I'm trying to avoid using a sheet to do the work.

Here is the userform code.
Private Sub Button_Horiz_Click()
'Horizontally arranges selected workbooks
Dim ictr As Long
Dim wkbkCtr As Long
SomeWkbkWasSelected = False
wkbkCtr = -1
With Me.ListBox1
ReDim WkbkNames(0 To .ListCount - 1)
For ictr = 0 To .ListCount - 1
If .Selected(ictr) = True Then
SomeWkbkWasSelected = True
wkbkCtr = wkbkCtr + 1
WkbkNames(wkbkCtr) = .List(ictr)
End If
Next ictr
End With
ReDim Preserve WkbkNames(0 To wkbkCtr)
ArrangeHorizontally
Unload Me
End Sub

Private Sub Button_Vert_Click()
'Vertically arranges selected workbooks
Dim ictr As Long
Dim wkbkCtr As Long
SomeWkbkWasSelected = False
wkbkCtr = -1
With Me.ListBox1
ReDim WkbkNames(0 To .ListCount - 1)
For ictr = 0 To .ListCount - 1
If .Selected(ictr) = True Then
SomeWkbkWasSelected = True
wkbkCtr = wkbkCtr + 1
WkbkNames(wkbkCtr) = .List(ictr)
End If
Next ictr
End With
ReDim Preserve WkbkNames(0 To wkbkCtr)
ArrangeVertically
Unload Me
End Sub

Private Sub UserForm_Initialize()
Dim wkbk As Workbook
Dim wkbknm As String
Dim myWin As Window

Me.ListBox1.MultiSelect = fmMultiSelectMulti
For Each wkbk In Application.Workbooks
For Each myWin In wkbk.Windows
If myWin.Visible = True Then
If Left(wkbk.Name, 1) = "{" Then
With wkbk.ActiveSheet.Range("A65536")
.Formula = "=wpname()"
wkbknm = .Value
.ClearContents
End With
Me.ListBox1.AddItem wkbknm
Exit For
Else
Me.ListBox1.AddItem wkbk.Name
Exit For
End If
End If
Next myWin
Next wkbk
End Sub

Here is the module code
Option Explicit
Public WkbkNames() As String
Public SomeWkbkWasSelected As Boolean

Sub ArrangeVertically()
Dim ictr As Long
Dim a As Long 'chosen books
Dim h As Long 'height
Dim l As Long 'left
Dim w As Long 'width
Dim wkbknm As String

ActiveWindow.WindowState = xlMaximized
a = 0
h = ActiveWindow.Height - 25
h = h
l = 0
w = ActiveWindow.Width
h = h
If SomeWkbkWasSelected = True Then
For ictr = LBound(WkbkNames) To UBound(WkbkNames)
'Debug.Print WkbkNames(ictr) & ictr + 1
a = ictr + 1
Next ictr
w = w / a
For ictr = LBound(WkbkNames) To UBound(WkbkNames)
'''''error happens right here. need to find a way to activate the
next workbook
'''''the problem is when the nextworkbook is an epace file and the
name has been
'''''encrypted
'''''1. find a way to activate the file, OR
'''''2. find a way to pull the real name without activating, OR
'''''3. create a name for the array differently

Workbooks(WkbkNames(ictr)).Activate
If ActiveWorkbook.Name <> Workbooks(WkbkNames(ictr)).Name Then
With ActiveSheet.Range("A65536")
.Formula = "=wpname"
wkbknm = .Value
.ClearContents
End With
If wkbknm <> Workbooks(WkbkNames(ictr)).Name Then
GoTo OnToNext
End If
End If
'Workbooks(WkbkNames(ictr)).Activate
With ActiveWindow
.WindowState = xlNormal
.Top = 0
.Left = l
.Width = w
.Height = h
l = .Left + .Width
End With
OnToNext:
Next ictr
End If
End Sub


Sub ArrangeHorizontally()
Dim ictr As Long
Dim a As Long 'chosen books
Dim h As Long 'height
Dim t As Long 'left
Dim w As Long 'width

ActiveWindow.WindowState = xlMaximized
a = 0
h = ActiveWindow.Height - 20
h = h
t = 0
w = ActiveWindow.Width - 5
w = w
If SomeWkbkWasSelected = True Then
For ictr = LBound(WkbkNames) To UBound(WkbkNames)
'Debug.Print WkbkNames(ictr) & ictr + 1
a = ictr + 1
Next ictr
h = h / a
For ictr = LBound(WkbkNames) To UBound(WkbkNames)
Workbooks(WkbkNames(ictr)).Activate
With ActiveWindow
.WindowState = xlNormal
.Left = 0
.Top = t
.Width = w
.Height = h
t = .Top + .Height
End With
Next ictr
End If
End Sub
--
JNW
 
Reply With Quote
 
 
 
 
Tom Ogilvy
Guest
Posts: n/a
 
      27th Jun 2007
The obvious solution is to translate the encrypted name into the unencrypted
equivalent.

try this demo:

Sub Demo1()
Dim v(1 To 4, 1 To 2)
v(1, 1) = "A"
v(1, 2) = "B"
v(2, 1) = "C"
v(2, 2) = "D"
v(3, 1) = "E"
v(3, 2) = "F"
v(4, 1) = "G"
v(4, 2) = "H"
res = Application.VLookup("G", v, 2, False)
If Not IsError(res) Then
MsgBox "Name G translates to: " & res
Else
MsgBox "Name G not found"
End If
End Sub

so you can do this with a two dimensional array.

--
Regards,
Tom Ogilvy

"JNW" <(E-Mail Removed)> wrote in message
news:30AE8CBA-919C-40F4-A978-(E-Mail Removed)...
>I work with a program that stores excel files using encrypted names. The
> program then assigns the file a recognizable name that I am able to
> retrieve
> through VBA.
>
> I am creating a tool to allow users to arrange a given number of workbooks
> through an add-in that has a userform.
>
> The problem: I can get everything to work if I have the encrypted file
> name
> show up in the userform, but the user can't tell what file it is, because
> the
> name makes no sense.
>
> How can I display the recognizable name I retrieve through VBA but still
> have the encrypted name used to resize the window.
>
> P.S. I can get this to work if I drop all the names in an excel worksheet
> and do a vlookup. I'm trying to avoid using a sheet to do the work.
>
> Here is the userform code.
> Private Sub Button_Horiz_Click()
> 'Horizontally arranges selected workbooks
> Dim ictr As Long
> Dim wkbkCtr As Long
> SomeWkbkWasSelected = False
> wkbkCtr = -1
> With Me.ListBox1
> ReDim WkbkNames(0 To .ListCount - 1)
> For ictr = 0 To .ListCount - 1
> If .Selected(ictr) = True Then
> SomeWkbkWasSelected = True
> wkbkCtr = wkbkCtr + 1
> WkbkNames(wkbkCtr) = .List(ictr)
> End If
> Next ictr
> End With
> ReDim Preserve WkbkNames(0 To wkbkCtr)
> ArrangeHorizontally
> Unload Me
> End Sub
>
> Private Sub Button_Vert_Click()
> 'Vertically arranges selected workbooks
> Dim ictr As Long
> Dim wkbkCtr As Long
> SomeWkbkWasSelected = False
> wkbkCtr = -1
> With Me.ListBox1
> ReDim WkbkNames(0 To .ListCount - 1)
> For ictr = 0 To .ListCount - 1
> If .Selected(ictr) = True Then
> SomeWkbkWasSelected = True
> wkbkCtr = wkbkCtr + 1
> WkbkNames(wkbkCtr) = .List(ictr)
> End If
> Next ictr
> End With
> ReDim Preserve WkbkNames(0 To wkbkCtr)
> ArrangeVertically
> Unload Me
> End Sub
>
> Private Sub UserForm_Initialize()
> Dim wkbk As Workbook
> Dim wkbknm As String
> Dim myWin As Window
>
> Me.ListBox1.MultiSelect = fmMultiSelectMulti
> For Each wkbk In Application.Workbooks
> For Each myWin In wkbk.Windows
> If myWin.Visible = True Then
> If Left(wkbk.Name, 1) = "{" Then
> With wkbk.ActiveSheet.Range("A65536")
> .Formula = "=wpname()"
> wkbknm = .Value
> .ClearContents
> End With
> Me.ListBox1.AddItem wkbknm
> Exit For
> Else
> Me.ListBox1.AddItem wkbk.Name
> Exit For
> End If
> End If
> Next myWin
> Next wkbk
> End Sub
>
> Here is the module code
> Option Explicit
> Public WkbkNames() As String
> Public SomeWkbkWasSelected As Boolean
>
> Sub ArrangeVertically()
> Dim ictr As Long
> Dim a As Long 'chosen books
> Dim h As Long 'height
> Dim l As Long 'left
> Dim w As Long 'width
> Dim wkbknm As String
>
> ActiveWindow.WindowState = xlMaximized
> a = 0
> h = ActiveWindow.Height - 25
> h = h
> l = 0
> w = ActiveWindow.Width
> h = h
> If SomeWkbkWasSelected = True Then
> For ictr = LBound(WkbkNames) To UBound(WkbkNames)
> 'Debug.Print WkbkNames(ictr) & ictr + 1
> a = ictr + 1
> Next ictr
> w = w / a
> For ictr = LBound(WkbkNames) To UBound(WkbkNames)
> '''''error happens right here. need to find a way to activate the
> next workbook
> '''''the problem is when the nextworkbook is an epace file and the
> name has been
> '''''encrypted
> '''''1. find a way to activate the file, OR
> '''''2. find a way to pull the real name without activating, OR
> '''''3. create a name for the array differently
>
> Workbooks(WkbkNames(ictr)).Activate
> If ActiveWorkbook.Name <> Workbooks(WkbkNames(ictr)).Name Then
> With ActiveSheet.Range("A65536")
> .Formula = "=wpname"
> wkbknm = .Value
> .ClearContents
> End With
> If wkbknm <> Workbooks(WkbkNames(ictr)).Name Then
> GoTo OnToNext
> End If
> End If
> 'Workbooks(WkbkNames(ictr)).Activate
> With ActiveWindow
> .WindowState = xlNormal
> .Top = 0
> .Left = l
> .Width = w
> .Height = h
> l = .Left + .Width
> End With
> OnToNext:
> Next ictr
> End If
> End Sub
>
>
> Sub ArrangeHorizontally()
> Dim ictr As Long
> Dim a As Long 'chosen books
> Dim h As Long 'height
> Dim t As Long 'left
> Dim w As Long 'width
>
> ActiveWindow.WindowState = xlMaximized
> a = 0
> h = ActiveWindow.Height - 20
> h = h
> t = 0
> w = ActiveWindow.Width - 5
> w = w
> If SomeWkbkWasSelected = True Then
> For ictr = LBound(WkbkNames) To UBound(WkbkNames)
> 'Debug.Print WkbkNames(ictr) & ictr + 1
> a = ictr + 1
> Next ictr
> h = h / a
> For ictr = LBound(WkbkNames) To UBound(WkbkNames)
> Workbooks(WkbkNames(ictr)).Activate
> With ActiveWindow
> .WindowState = xlNormal
> .Left = 0
> .Top = t
> .Width = w
> .Height = h
> t = .Top + .Height
> End With
> Next ictr
> End If
> End Sub
> --
> JNW



 
Reply With Quote
 
=?Utf-8?B?Sk5X?=
Guest
Posts: n/a
 
      27th Jun 2007
I want to make sure I understand. In this example B, D, F, and H are the
encrypted names. The rest are the unencrypted equivalent. And I can set
A,B, etc. either by my own string, or, say, v(1,2) = workbook(2).name.

Am I way off on this?

I've always been fuzzy on 2D arrays. Thanks for this succint explaination.
--
JNW


"Tom Ogilvy" wrote:

> The obvious solution is to translate the encrypted name into the unencrypted
> equivalent.
>
> try this demo:
>
> Sub Demo1()
> Dim v(1 To 4, 1 To 2)
> v(1, 1) = "A"
> v(1, 2) = "B"
> v(2, 1) = "C"
> v(2, 2) = "D"
> v(3, 1) = "E"
> v(3, 2) = "F"
> v(4, 1) = "G"
> v(4, 2) = "H"
> res = Application.VLookup("G", v, 2, False)
> If Not IsError(res) Then
> MsgBox "Name G translates to: " & res
> Else
> MsgBox "Name G not found"
> End If
> End Sub
>
> so you can do this with a two dimensional array.
>
> --
> Regards,
> Tom Ogilvy
>
> "JNW" <(E-Mail Removed)> wrote in message
> news:30AE8CBA-919C-40F4-A978-(E-Mail Removed)...
> >I work with a program that stores excel files using encrypted names. The
> > program then assigns the file a recognizable name that I am able to
> > retrieve
> > through VBA.
> >
> > I am creating a tool to allow users to arrange a given number of workbooks
> > through an add-in that has a userform.
> >
> > The problem: I can get everything to work if I have the encrypted file
> > name
> > show up in the userform, but the user can't tell what file it is, because
> > the
> > name makes no sense.
> >
> > How can I display the recognizable name I retrieve through VBA but still
> > have the encrypted name used to resize the window.
> >
> > P.S. I can get this to work if I drop all the names in an excel worksheet
> > and do a vlookup. I'm trying to avoid using a sheet to do the work.
> >
> > Here is the userform code.
> > Private Sub Button_Horiz_Click()
> > 'Horizontally arranges selected workbooks
> > Dim ictr As Long
> > Dim wkbkCtr As Long
> > SomeWkbkWasSelected = False
> > wkbkCtr = -1
> > With Me.ListBox1
> > ReDim WkbkNames(0 To .ListCount - 1)
> > For ictr = 0 To .ListCount - 1
> > If .Selected(ictr) = True Then
> > SomeWkbkWasSelected = True
> > wkbkCtr = wkbkCtr + 1
> > WkbkNames(wkbkCtr) = .List(ictr)
> > End If
> > Next ictr
> > End With
> > ReDim Preserve WkbkNames(0 To wkbkCtr)
> > ArrangeHorizontally
> > Unload Me
> > End Sub
> >
> > Private Sub Button_Vert_Click()
> > 'Vertically arranges selected workbooks
> > Dim ictr As Long
> > Dim wkbkCtr As Long
> > SomeWkbkWasSelected = False
> > wkbkCtr = -1
> > With Me.ListBox1
> > ReDim WkbkNames(0 To .ListCount - 1)
> > For ictr = 0 To .ListCount - 1
> > If .Selected(ictr) = True Then
> > SomeWkbkWasSelected = True
> > wkbkCtr = wkbkCtr + 1
> > WkbkNames(wkbkCtr) = .List(ictr)
> > End If
> > Next ictr
> > End With
> > ReDim Preserve WkbkNames(0 To wkbkCtr)
> > ArrangeVertically
> > Unload Me
> > End Sub
> >
> > Private Sub UserForm_Initialize()
> > Dim wkbk As Workbook
> > Dim wkbknm As String
> > Dim myWin As Window
> >
> > Me.ListBox1.MultiSelect = fmMultiSelectMulti
> > For Each wkbk In Application.Workbooks
> > For Each myWin In wkbk.Windows
> > If myWin.Visible = True Then
> > If Left(wkbk.Name, 1) = "{" Then
> > With wkbk.ActiveSheet.Range("A65536")
> > .Formula = "=wpname()"
> > wkbknm = .Value
> > .ClearContents
> > End With
> > Me.ListBox1.AddItem wkbknm
> > Exit For
> > Else
> > Me.ListBox1.AddItem wkbk.Name
> > Exit For
> > End If
> > End If
> > Next myWin
> > Next wkbk
> > End Sub
> >
> > Here is the module code
> > Option Explicit
> > Public WkbkNames() As String
> > Public SomeWkbkWasSelected As Boolean
> >
> > Sub ArrangeVertically()
> > Dim ictr As Long
> > Dim a As Long 'chosen books
> > Dim h As Long 'height
> > Dim l As Long 'left
> > Dim w As Long 'width
> > Dim wkbknm As String
> >
> > ActiveWindow.WindowState = xlMaximized
> > a = 0
> > h = ActiveWindow.Height - 25
> > h = h
> > l = 0
> > w = ActiveWindow.Width
> > h = h
> > If SomeWkbkWasSelected = True Then
> > For ictr = LBound(WkbkNames) To UBound(WkbkNames)
> > 'Debug.Print WkbkNames(ictr) & ictr + 1
> > a = ictr + 1
> > Next ictr
> > w = w / a
> > For ictr = LBound(WkbkNames) To UBound(WkbkNames)
> > '''''error happens right here. need to find a way to activate the
> > next workbook
> > '''''the problem is when the nextworkbook is an epace file and the
> > name has been
> > '''''encrypted
> > '''''1. find a way to activate the file, OR
> > '''''2. find a way to pull the real name without activating, OR
> > '''''3. create a name for the array differently
> >
> > Workbooks(WkbkNames(ictr)).Activate
> > If ActiveWorkbook.Name <> Workbooks(WkbkNames(ictr)).Name Then
> > With ActiveSheet.Range("A65536")
> > .Formula = "=wpname"
> > wkbknm = .Value
> > .ClearContents
> > End With
> > If wkbknm <> Workbooks(WkbkNames(ictr)).Name Then
> > GoTo OnToNext
> > End If
> > End If
> > 'Workbooks(WkbkNames(ictr)).Activate
> > With ActiveWindow
> > .WindowState = xlNormal
> > .Top = 0
> > .Left = l
> > .Width = w
> > .Height = h
> > l = .Left + .Width
> > End With
> > OnToNext:
> > Next ictr
> > End If
> > End Sub
> >
> >
> > Sub ArrangeHorizontally()
> > Dim ictr As Long
> > Dim a As Long 'chosen books
> > Dim h As Long 'height
> > Dim t As Long 'left
> > Dim w As Long 'width
> >
> > ActiveWindow.WindowState = xlMaximized
> > a = 0
> > h = ActiveWindow.Height - 20
> > h = h
> > t = 0
> > w = ActiveWindow.Width - 5
> > w = w
> > If SomeWkbkWasSelected = True Then
> > For ictr = LBound(WkbkNames) To UBound(WkbkNames)
> > 'Debug.Print WkbkNames(ictr) & ictr + 1
> > a = ictr + 1
> > Next ictr
> > h = h / a
> > For ictr = LBound(WkbkNames) To UBound(WkbkNames)
> > Workbooks(WkbkNames(ictr)).Activate
> > With ActiveWindow
> > .WindowState = xlNormal
> > .Left = 0
> > .Top = t
> > .Width = w
> > .Height = h
> > t = .Top + .Height
> > End With
> > Next ictr
> > End If
> > End Sub
> > --
> > JNW

>
>
>

 
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
Prevent cell/array references from changing when altering/moving thecell/array nme Microsoft Excel Misc 1 19th Sep 2008 01:53 PM
Redimming an array dynamically assigned from range (how to redim first dimension of a 2-D array? /or/ reverse the original array order) Keith R Microsoft Excel Programming 3 13th Nov 2007 04:08 PM
meaning of : IF(Switch; Average(array A, array B); array A) =?Utf-8?B?RFhBVA==?= Microsoft Excel Worksheet Functions 1 24th Oct 2006 06:11 PM
Pass from C# (framework 1.1) array of delegates to unmanaged DLL as array of function pointers verpeter@gmail.com Microsoft C# .NET 0 23rd Aug 2006 02:20 PM
select variables ranges, copy to array, paste the array in new workbook Mathew Microsoft Excel Worksheet Functions 1 1st Apr 2005 09:40 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:40 PM.