Results 1 to 19 of 19

Thread: Solved: New to VBA - Help me in making this code shorter and better

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Works okay for me. What error do you get?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  2. #2
    After opening the second file , i get the error on line [saying Run time error'-2147221080 (800401a8)
    Automation errror]

    wb.Sheets(1).Copy After:=ThisWb.Sheets(1)

  3. #3
    Hi I did some trial and error and could solve it. Here is the code which i used. Apprantely Set Wb = was missing from the second loop. Can you tell me what does Set do?


    [VBA]Sub Copy_Data()
    Dim ThisWb As Workbook
    Dim wb As Workbook

    Set ThisWb = Workbooks("Consolidated.xls")

    Set wb = Workbooks.Open(FileName:=("C:/VBA Source/Euro.xls"))
    wb.Sheets(1).Copy After:=ThisWb.Sheets(1)
    ThisWb.Worksheets(2).Name = Replace(Replace(wb.Name, ".xls", ""), "C:/VBA Source/", "")
    wb.Close

    Set wb = Workbooks.Open(FileName:=("C:/VBA Source/USD.xls"))
    wb.Sheets(1).Copy After:=ThisWb.Sheets(1)
    ThisWb.Worksheets(2).Name = Replace(Replace(wb.Name, ".xls", ""), "C:/VBA Source/", "")
    wb.Close

    Set wb = Workbooks.Open(FileName:=("C:/VBA Source/JPY.xls"))
    wb.Sheets(1).Copy After:=ThisWb.Sheets(1)
    ThisWb.Worksheets(2).Name = Replace(Replace(wb.Name, ".xls", ""), "C:/VBA Source/", "")
    wb.Close

    Set wb = Workbooks.Open(FileName:=("C:/VBA Source/GBP.xls"))
    wb.Sheets(1).Copy After:=ThisWb.Sheets(1)
    ThisWb.Worksheets(2).Name = Replace(Replace(wb.Name, ".xls", ""), "C:/VBA Source/", "")
    wb.Close

    MsgBox ("Copying worksheets completed")

    End Sub[/VBA]

  4. #4
    If I want to run above Macro in the same file again How to first delete existing sheets in the file with name USD, Euro, JPY and GBP (Or to say other way round delete all sheets except for Sheet1), so that programme runs properly.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Quote Originally Posted by vicks
    Hi I did some trial and error and could solve it. Here is the code which i used. Apprantely Set Wb = was missing from the second loop. Can you tell me what does Set do?
    It assigns an object to an object variable.

    Most variables such as

    myvar = 10

    are really saying

    Let myvar = 10

    Set is used to distinguish objects from data types.
    Last edited by Bob Phillips; 02-06-2009 at 10:55 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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