Ramy
Ramy

Reputation: 21271

Porting VBA To IronPython

I am trying to translate the VBA code found in this link into IronPython. Can anyone recommend a good VBA resource to explain how best to do this for a Python programmer?

I have all the Excel portions implemented, such as the treatment and use of objects, workbooks, worksheets, etc.

I will also settle for an explanation of this snippet of code:

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Put the index value of the sheet into Arr. Ensure there
' are no duplicates. If Arr(N) is not zero, we've already
' loaded that element of Arr and thus have duplicate sheet
' names.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Arr(N) > 0 Then
    ErrorText = "Duplicate worksheet name in NameArray."
    SortWorksheetsByNameArray = False
    Exit Function
End If

why would Arr(N) ever NOT be greater than 0?

Here is my current code, which is broken:

def move_worksheets_according_to_list(self, name_list):
    wb = self.com_workbook
    temp_list = []
    for n in range(len(name_list)):
        if name_list.count(name_list[n]) > 1:
            raise Exception("Duplicate worksheet name in NameArray.")
        else:
            temp_list.append(wb.Worksheets(name_list[n]).Index)

    for m in range(len(temp_list)):
        for n in range(m, len(temp_list)):
            if temp_list[n] < temp_list[m]:
                l = temp_list[n]
                temp_list[n] = temp_list[m]
                temp_list[m] = l

    if not all(temp_list[i] <= temp_list[i+1] for i in xrange(len(temp_list)-1)):
        return False

    print "current order"
    for sheet in wb.Worksheets:
        print sheet.Name
    wb.Worksheets(name_list[0]).Move(Before=wb.Worksheets(1))
    #WB.Worksheets(NameArray(LBound(NameArray))).Move before:=WB.Worksheets(Arr(1))
    for n in range(len(name_list)-1):
        print 'moving ', name_list[n], 'before ', name_list[n+1]
        wb.Worksheets(name_list[n]).Move(Before=wb.Worksheets(name_list[n + 1]))

Note:

With this answer as reference, here is all I had to do:

def move_worksheets_according_to_list(self, name_list):
    wb = self.com_workbook
    l = []
    # since wb.Worksheets is a com_object, you can't use the "if _ in XXX"
    # construct without converting to a list first
    for s in wb.Worksheets:
        l.append(s.Name)

    for n in range(len(name_list)):
        if name_list[n] in l:
            wb.Worksheets(name_list[n]).Move(After=wb.Worksheets(wb.Worksheets.Count))

Upvotes: 0

Views: 391

Answers (2)

Ramy
Ramy

Reputation: 21271

def move_worksheets_according_to_list(self, name_list): wb = self.com_workbook l = [] # since wb.Worksheets is a com_object, you can't use the "if _ in XXX" # construct without converting to a list first for s in wb.Worksheets: l.append(s.Name)

for n in range(len(name_list)):
    if name_list[n] in l:
        wb.Worksheets(name_list[n]).Move(After=wb.Worksheets(wb.Worksheets.Count)

Upvotes: 0

Tim Williams
Tim Williams

Reputation: 166725

Declaring and dimensioning an array of longs in VBA will create the array with the default value of 0 in each slot.

Upvotes: 1

Related Questions