+ Reply to Thread
Results 1 to 2 of 2

Apply Macro to all files in a specific folder.

Hybrid View

  1. #1
    Registered User
    Join Date
    04-29-2013
    Location
    Beirut
    MS-Off Ver
    Excel OSX
    Posts
    38

    Apply Macro to all files in a specific folder.

    So I have a macro to extract a lot of data from an online database that unfortunately only allowed me to do so in a complete useless format (like this: http://imgur.com/v6bZQc4). Once you extract to excel, with the option they provide, it looks like this (for the sake of this example I deleted most rows): http://imgur.com/7Hec8dn

    My macro flattens out the data to more useable format. However, I would now like to have the macro run over a folder that contains all the downloaded worksheets (the ones in the unusable format) and then generate one master sheet (in the desired format specified) from all of the files in this folder.

    Some issues:

    1) The second part of my macro, which adds columns for: "Country" "Month" "Year" "Transaction" are linked to a sheet1. I believe this would bring up errors and there might be a better.

    2) In the master sheet generated headers must remain the same in a way that the data generated by running the macro on each downloaded sheet goes underneath.

    I've tried fixing these issues and getting the macro to apply to all files in a specific folder, so far no luck. I was told to use Scripting.Filesystemobject, but I it didn't work for me - or more likely, I didn't use it properly.

    Thanks in advance for any help. If this makes it easier, here's one of the downloaded worksheets and the macro in question: http://www.filedropper.com/example_2

  2. #2
    Registered User
    Join Date
    04-29-2013
    Location
    Beirut
    MS-Off Ver
    Excel OSX
    Posts
    38

    Re: Apply Macro to all files in a specific folder.

    Macro in question.

    PHP Code: 
    Attribute VB_Name "HIADataImporter"
    Sub HIADataConvert()
    EndRow Range("A" Rows.Count).End(xlUp).Row
        Dim Druglist
    ()

    ReDim Druglist(EndRow 66)

    For 
    1 To EndRow
            
    If Cells(i1).Value "Medicine" Then
                    StartRow 
    i
                    Cells
    (i1).Offset(10).Activate
                    
    Exit For
            
    End If
    Next


    DrugIterator 
    0
    For ActiveCell.Row To EndRow
            
    If Cells(i1).Value <> "" Then
                    Cells
    (i1).Activate
                    DrugName 
    Left(Cells(i1).ValueInStr(Cells(i1).Value"-") - 2)
                    
    DosageType Right(Cells(i1).ValueLen(Cells(i1).Value) - InStrRev(Cells(i1).Value" "))
                    
    Strength Mid(Cells(i1).ValueLen(DrugName) + 4Len(Cells(i1).Value) - Len(DrugName) - Len(DosageType) - 3)

                    For 
    0 To 1
                            ActiveCell
    .Offset(10).Activate
                            
    If ActiveCell.Offset(01).Value <> "" Then
                                    
    For 0 To 2
                                            Druglist
    (DrugIterator J0) = DrugName
                                            Druglist
    (DrugIterator J1) = Strength
                                            Druglist
    (DrugIterator J2) = DosageType
                                            Druglist
    (DrugIterator J3) = ActiveCell.Offset(01).Value
                                            Druglist
    (DrugIterator J4) = Cells(StartRow4).Value
                                            Druglist
    (DrugIterator J5) = ActiveCell.Offset(02).Value
                                            Druglist
    (DrugIterator J6) = ActiveCell.Offset(05).Value
                                    Next
                                    DrugIterator 
    DrugIterator J
                            End 
    If
                    
    Next

            End 
    If
    Next

    Worksheets
    .Add
    Range
    ("A2").Resize(UBound(Druglist) + 1UBound(Druglist2) + 1) = Druglist
    Range
    ("A1").Resize(17) = Array("Medicine""Dosage Strength""Dosage Type""Type""Price Type""Price Value""Availability")
    Columns("A:G").AutoFit
    Attribute CountryDatePriceType
    .VB_ProcData.VB_Invoke_Func " \n14" ' ' CountryDatePriceType
    Columns
    ("A:A").Select
    Selection
    .Insert Shift:=xlToRightCopyOrigin:=xlFormatFromLeftOrAbove
    Selection
    .Insert Shift:=xlToRightCopyOrigin:=xlFormatFromLeftOrAbove
    Selection
    .Insert Shift:=xlToRightCopyOrigin:=xlFormatFromLeftOrAbove
    Range
    ("A1").Select
    ActiveCell
    .FormulaR1C1 "Country"
    Columns("B:B").Select
    Selection
    .Insert Shift:=xlToRightCopyOrigin:=xlFormatFromLeftOrAbove
    Range
    ("B1").Select
    ActiveCell
    .FormulaR1C1 "Year"
    Range("C1").Select
    ActiveCell
    .FormulaR1C1 "Month"
    Range("D1").Select
    Columns
    ("D:D").ColumnWidth 14
    ActiveCell
    .FormulaR1C1 "Transaction"
    Range("A2").Select
    Sheets
    ("Sheet1").Select
    ActiveWindow
    .SmallScroll Down:=-322
    Range
    ("A2:F2").Select
    With Selection
        
    .HorizontalAlignment xlGeneral
        
    .VerticalAlignment xlBottom
        
    .WrapText True
        
    .Orientation 0
        
    .AddIndent False
        
    .ShrinkToFit False
        
    .MergeCells True
    End With
    Selection
    .Merge True
    Range
    ("A2").Select
    Sheets
    ("Sheet1").Select
    Selection
    .Copy
    Sheets
    ("Sheet2").Select
    Selection
    .PasteSpecial Paste:=xlValuesOperation:=xlNoneSkipBlanks:= _
        False
    Transpose:=False
    Columns
    ("A:A").ColumnWidth 20.67
    Range
    ("A3").Select
    Application
    .CutCopyMode False
    Range
    ("A2").Select
    Selection
    .TextToColumns Destination:=Range("A2"), DataType:=xlDelimited_
        TextQualifier
    :=xlDoubleQuoteConsecutiveDelimiter:=FalseTab:=False_
        Semicolon
    :=FalseComma:=FalseSpace:=FalseOther:=TrueOtherChar _
        
    :=","FieldInfo:=Array(Array(11), Array(21))
    Range("B2").Select
    Selection
    .TextToColumns Destination:=Range("B2"), DataType:=xlDelimited_
        TextQualifier
    :=xlDoubleQuoteConsecutiveDelimiter:=TrueTab:=False_
        Semicolon
    :=FalseComma:=FalseSpace:=TrueOther:=FalseOtherChar _
        
    :=","FieldInfo:=Array(Array(19), Array(21), Array(31))
    Range("D2").Select
    ActiveCell
    .FormulaR1C1 "=Sheet1!R[2]C[-3]"
    Range("D3").Select
    ActiveCell
    .FormulaR1C1 "=R[-1]C"
    Range("D3").Select
    Selection
    .AutoFill Destination:=Range("D3:D247")
    Range("D3:D247").Select
    Range
    ("C3").Select
    ActiveCell
    .FormulaR1C1 "=R[-1]C"
    Range("C3").Select
    Selection
    .AutoFill Destination:=Range("C3:C247")
    Range("C3:C247").Select
    Range
    ("B3").Select
    ActiveCell
    .FormulaR1C1 "=R[-1]C"
    Range("B3").Select
    Selection
    .AutoFill Destination:=Range("B3:B247")
    Range("B3:B247").Select
    Range
    ("A3").Select
    ActiveCell
    .FormulaR1C1 "=R[-1]C"
    Range("A3").Select
    Selection
    .AutoFill Destination:=Range("A3:A247")
    Range("A3:A247").Select
    Range
    ("B6").Select
    ActiveWindow
    .SmallScroll Down:=63
    Range
    ("M94").Select
    ActiveWindow
    .SmallScroll Down:=-275
    MsgBox 
    "HIA Data was copied in the new format"
    End Sub 

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1