On 23 Sep., cyber 1000s wrote:
> Hi cyberspace,
>
> I have some c programming background...
> Is it possible to increment a worksheet object (worksheets collection)
> in a loop such as below ?
>
> Public Sub CopyShNamesFromWkbToWkb2()
>
>   Dim i As Integer
>   Dim wkb As Object
>   Set wkb = Workbooks("Nouveau_Feuille_Excel_1.xls")
>   Dim wkb2 As Object
>   Set wkb2 = Workbooks("Classeur1.xls")
>   Dim ws As Object
>   Set ws = Worksheets
>
>   For i = 1 To ws.Count
>     wkb.Sheets(3).Cells(i, 4) = wkb2.ws(i).Name    '///A
>   Next i
>
> End Sub
>
> line ///A should be:     wkb.Sheets(3).Cells(i, 4) =
> wkb2.Sheets(i).Name
>
> Why a property pointing on an object variable such as count is working
> but not if it's being incremented like any excel vba name :
> sheets(i)... and not ws affected as a sheet ?

Since you have experience with C, let's transpose your VBA question
into a C question:

/* The following definitions simulate the Excel run-time.
 */
typedef struct
{
  char* Name;
} Worksheet;

typedef struct
{
  Worksheet* Sheets;
} Workbook;

workbook* Workbooks; /* Global Workbooks collection */
Worksheet* Worksheets; /* Global Worksheets collection */

/* Here comes your code.
 */
void CopyShNamesFromWKbToWkb2 ()
{
  int i;
  Workbook* wkb; /* = Nouveau_Feuille_Excel_1.xls */
  Workbook* wkb2;
  Worksheet* ws;
  ws = Worksheets;

  for (i = 0; i < Count(ws); ++i)
    wkb[3].Cells[i,3] = wkb2.ws[i].Name
}

Can you spot your mistake now?

Regards,
Stuart

-- 
----------------------------------------------------------------------------------
Some important links for excel users:
1. Follow us on TWITTER for tips tricks and links : 
http://twitter.com/exceldailytip
2. Join our LinkedIN group @ http://www.linkedin.com/groups?gid=1871310
3. Excel tutorials at http://www.excel-macros.blogspot.com
4. Learn VBA Macros at http://www.quickvba.blogspot.com
5. Excel Tips and Tricks at http://exceldailytip.blogspot.com
 
To post to this group, send email to excel-macros@googlegroups.com

<><><><><><><><><><><><><><><><><><><><><><>
Like our page on facebook , Just follow below link
http://www.facebook.com/pages/discussexcelcom/160307843985936?v=wall&ref=ts

Reply via email to