Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Removing multiple sheets in excel automation
Message
De
21/12/2013 09:16:14
 
 
À
21/12/2013 09:07:22
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01590748
Message ID:
01590762
Vues:
58
Tamar --

Sure, your code avoids the re-evaluation of the bounds each time thru the loop. At this level, though, I can't say that I would really care about that speed difference.

I would much rather write self-explanatory code.

For me, at least, that Do While speaks for itself, but your code is somewhat opaque, and begs the question why you have to step backwards.



>>Yes, loop from the highest down.
>>
>>Or, even more simply, keep deleting the second sheet as long as there is one. (technique has been used, but this code not tested)
>>
Do While owb.Sheets.Count > 1
>>    mname=owb.Sheets(2).Name
>>    Wait Window 'removing '+ mname timeout 2
>>     owb.Sheets(2).Delete
>>Enddo
>>
>
>And no reason for DO WHILE here:
>
>
>FOR nSheet = owb.Sheets.Count TO 2 STEP -1
>  oWB.Sheets(m.nSheet).Delete()
>ENDFOR
>
>
>FOR evaluates its bounds only once, before the loop begins.
>
>Tamar
>>
>>>looping from highest # down to first might be more stable...
>>>
>>>HTH
>>>
>>>thomas
>>>
>>>>much cleaner and more logical - thanks borislav.
>>>>problem now: It gets rid of the furthest two sheets (of a 4 sheet file) then gets stuck in a loop at which point I still have 2 sheets remaining.
>>>>I can't seem to solve it even adding if clauses - strange behavior - seems to defy your logic.
>>>>k
>>>>
>>>>>>I have set this up to remove all multiple sheets except the first sheet.
>>>>>>It's not working - any help would be appreciated. thanks
>>>>>>k
>>>>>>
>>>>>>
>>>>>>lcFile='myfile.xlsx'
>>>>>>Erase (Forceext(lcFile,'xxx'))
>>>>>>ox=Createobject('excel.application')
>>>>>>owb=ox.Workbooks.Open(lcFile)
>>>>>>
>>>>>>If owb.Sheets.Count > 1
>>>>>>	Nu= owb.Sheets.Count
>>>>>>	ms=1
>>>>>>	Do While ms < Nu+1
>>>>>>		mname=owb.Sheets(m1).Name
>>>>>>		If ms>1
>>>>>>			Wait Window 'removing '+ mname timeout 2
>>>>>>			owb.Sheets(mname).Delete
>>>>>>		Endif
>>>>>>		ms=ms+1
>>>>>>	Enddo
>>>>>>Endif
>>>>>>ox.Visible=.T.
>>>>>>
>>>>>
>>>>>
>>>>>lcFile='myfile.xlsx'
>>>>>ox=Createobject('excel.application')
>>>>>owb=ox.Workbooks.Open(lcFile)
>>>>>
>>>>>If owb.Sheets.Count > 1
>>>>>    Nu= owb.Sheets.Count
>>>>>    Do While Nu > 1
>>>>>        mname=owb.Sheets(Nu).Name
>>>>>        Wait Window 'removing '+ mname timeout 2
>>>>>        owb.Sheets(mname).Delete
>>>>>        Nu= owb.Sheets.Count
>>>>>    Enddo
>>>>>Endif
>>>>>ox.Visible=.T.
>>>>>
Jim Nelson
Newbury Park, CA
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform