Results 1 to 2 of 2

Thread: VBA HELP - LOOP THROUGH FILES

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    VBA HELP - LOOP THROUGH FILES

    Dear All,

    I have following problem, please find the details below:

    This is the piece of code that I am using.
    The lines I have marked in red are basically the sheets where i want to paste my data.
    The way I have done it now works fine but in case anyone adds or removes the sheet this code will not work.
    I wanted to enter the name of the name of sheets in another excel and wanted to code to loop through those sheets and pass that range name as worksheet name but i wasn’t able to do it.
    Can you explain how I can pass range name as a worksheet name?

    Code:
    Sub LoopThroughFiles()
        Dim MyObj As Object, MySource As Object, file As Variant, Dest As Range
        Set rDest = Sheets("lookup").Range("A1").Offset(1, 0)
        'Set sheetname = Sheets("lookup").Range("B1").Offset(1, 0)
        file = Dir("c:\Reports\")
        'Do While sheetname <> ""
        While (file <> "")
        For i = 28 To 32
             If InStr(file, "test") > 0 Then
             MsgBox "found " & file
             Exit Sub
          End If
             If file <> rDest Then
             
         MsgBox file & " " & "does not exists"
         Else
         Workbooks.Open "c:\Reports" & "\" & file
         Workbooks(file).Activate
         Sheets("Sheet1").Range("A1:IV65536").Select
         Selection.Copy
         'Range(Selection, Selection.End(xlDown)).Select
         'Range(Selection, Selection.End(xlToRight)).Copy
         'Selection.Copy
         'ActiveWorkbook.Close
         ThisWorkbook.Activate
         Sheets(i).Activate
         Sheets(i).Range("A1:IV65536").Select
         Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
         ‘Sheets (i).Range("A1").PasteSpecial Paste:=xlPasteValues
          End If
          Workbooks(file).Close
       file = Dir
        Set rDest = rDest.Offset(1, 0)
        'Set sheetname = sheetname.Offset(1, 0)
        Next
        Wend
    End Sub

    Please help.
    Last edited by Admin; 10-19-2013 at 03:16 PM. Reason: code tag added

  2. #2
    Member
    Join Date
    Jun 2013
    Posts
    93
    Rep Power
    12
    can you attach a sample file and explain better your goal ?

Similar Threads

  1. Loop Through Files In A Folder Using VBA
    By Excel Fox in forum Word Help
    Replies: 99
    Last Post: 11-01-2021, 04:38 PM
  2. Replies: 15
    Last Post: 08-23-2013, 12:03 PM
  3. Replies: 3
    Last Post: 06-25-2013, 07:51 AM
  4. Replies: 1
    Last Post: 06-12-2013, 07:42 PM
  5. Speed up Loop VBA
    By PcMax in forum Excel Help
    Replies: 15
    Last Post: 04-09-2012, 04:20 PM

Posting Permissions

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