Results 1 to 7 of 7

Thread: [RESOLVED] programming macros in excel

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    4

    Resolved [RESOLVED] programming macros in excel

    I have 20 sheets with data, and I want save some data in another sheets, I have this code :

    VB Code:
    1. Windows("FICHAS SINANPE.xls").Activate
    2.     Sheets("001 Elbert Zavaleta Zavaleta").Select
    3.     Range("B6:B14").Select
    4.     Selection.Copy
    5.     Windows("Dbase personal.xls").Activate
    6.     Sheets("Datos Personales").Select
    7.     Range("A2").Select
    8.     Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
    9.         , Transpose:=True
    10.  
    11. Windows("FICHAS SINANPE.xls").Activate
    12.     Sheets("002 Dante Alemán De Lama").Select
    13.     Range("B6:B14").Select
    14.     Selection.Copy
    15.     Windows("Dbase personal.xls").Activate
    16.     Sheets("Datos Personales").Select
    17.     Range("A3").Select
    18.     Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
    19.         , Transpose:=True

    This code run, but I have 20 sheets and I want to do a bucle where the sheets are inside the bucle, but I don't know how to do

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: programming macros in excel

    Hi darwingomez, welcome to VBForums!

    I'm a bit confused, what do you mean by bucle?

    Do you mean that you want to paste the same data into multiple sheets?

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    4

    Re: programming macros in excel

    when I say bucle, I want to say (for i = 0 to i <20), but I don't know how to do write the code exactly to can do the same for the 20 sheets

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: programming macros in excel

    Assuming that those are the first 20 sheets in the Workbook, this code should do the trick:
    VB Code:
    1. Dim i as Integer
    2.   With Workbooks("FICHAS SINANPE.xls")      
    3.     For i = 1 to 20
    4.       .Activate
    5.       .Sheets(i).Select
    6.       Range("B6:B14").Select
    7.       Selection.Copy
    8.       Windows("Dbase personal.xls").Activate
    9.       Sheets("Datos Personales").Select
    10.       Range("A" & (i+1)).Select
    11.       Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
    12.         , Transpose:=True
    13.     Next i
    14.   End With

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    4

    Re: programming macros in excel

    Ok

    Very fast, tks

    Darwin

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: programming macros in excel

    I know it may be solved but as another option you can use the .Copy method for the sheets collection to do a Copy/Paste all in one line without doing any Selection or Activate. Should be faster still.
    VB Code:
    1. Workbooks("FICHAS SINANPE.xls").Sheets(i).Copy After:=Workbooks("Datos Personales").Sheets(Sheets.Count)
    This will copy each sheet in the FICHAS SINANPE.xls workbook and paste them into the Datos Personales workbook. Now if you dont want to copy the entire sheet then your current method is better.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: programming macros in excel

    Moved from ClassicVb.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width