costalat

VBA - Read data from pre selected excel files

Sep 7th, 2011
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub Macro4()
  2. '
  3. ' Criar tabela com requisições por ot
  4. '
  5. ' Atalho por teclado: Ctrl+q
  6. '
  7.   Dim tamanho As Variant
  8.    Dim l
  9.    Dim folder As Variant
  10.    Dim d_ot As Range
  11.    Dim i_ot      As Range
  12.    Dim req As Range
  13.    Dim caminho
  14.    Dim file   As Variant
  15.    Dim custo_est As Range
  16.    'Const partilha = "F:\RQ\"
  17.   Const partilha = "\\Path\EM_TRATAMENTO\"
  18.    Range("A16:e500").Clear
  19.  
  20.     Selection.Borders(xlDiagonalDown).LineStyle = xlNone
  21.     Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  22.     Selection.Borders(xlEdgeLeft).LineStyle = xlNone
  23.     Selection.Borders(xlEdgeTop).LineStyle = xlNone
  24.     Selection.Borders(xlEdgeBottom).LineStyle = xlNone
  25.     Selection.Borders(xlEdgeRight).LineStyle = xlNone
  26.     Selection.Borders(xlInsideVertical).LineStyle = xlNone
  27.     Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
  28.  
  29.   With CreateObject("Scripting.FileSystemObject")
  30.  
  31.   Set req = ActiveSheet.Cells(16, 1)
  32.   Set d_ot = ActiveSheet.Cells(16, 2)
  33.   Set i_ot = ActiveSheet.Cells(16, 3)
  34.   Set custo_est = ActiveSheet.Cells(16, 4)
  35.   Set c = ActiveSheet.Cells(10, 7)
  36.   tamanho = Range("I65536").End(xlUp).Row
  37.   For l = 3 To tamanho
  38.  
  39.    folder = ActiveSheet.Cells(l, 9)
  40.  
  41.    caminho = partilha & folder
  42.  
  43.    For Each file In .GetFolder(caminho).Files
  44.    If LCase(Left(.GetExtensionName(file.Name), 3)) = "xls" Then
  45.                If Right(.GetBaseName(file), 3) = "REQ" Then
  46.     req.Formula = "='" & file.ParentFolder & "\[" & file.Name & "]Folha1'!$A$6"
  47.     Set req = req.Offset(1)
  48.     d_ot.Formula = "='" & file.ParentFolder & "\[" & file.Name & "]Folha1'!$B$9"
  49.     Set d_ot = d_ot.Offset(1)
  50.     i_ot.Formula = "='" & file.ParentFolder & "\[" & file.Name & "]Folha1'!$B$10"
  51.     Set i_ot = i_ot.Offset(1)
  52.     custo_est.Formula = "='" & file.ParentFolder & "\[" & file.Name & "]Folha1'!$F$58"
  53.     Set custo_est = custo_est.Offset(1)
  54.         End If
  55.    End If
  56.  
  57.    Next
  58.  
  59.   Next l
  60.  
  61.   tamanho = Range("A65536").End(xlUp).Row
  62.   For l = tamanho To 16 Step -1
  63.    c = ActiveSheet.Cells(l, 4)
  64.    If c = 0 Then
  65.     Range(Cells(l, 1), Cells(l, 4)).Clear
  66.     Worksheets("Folha1").Range(Cells(16, 1), Cells(tamanho, 5)).Sort Key1:=Worksheets("Folha1").Columns("A"), Order1:=xlAscending
  67.  
  68.    End If
  69.   Next l
  70.  
  71.  
  72.   tamanho = Range("A600").End(xlUp).Row
  73.   Range(Cells(16, 1), Cells(tamanho, 4)).Select
  74.  
  75.     Selection.Borders(xlDiagonalDown).LineStyle = xlNone
  76.     Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  77.     With Selection.Borders(xlEdgeLeft)
  78.         .LineStyle = xlDouble
  79.         .ColorIndex = 0
  80.         .TintAndShade = 0
  81.         .Weight = xlThick
  82.     End With
  83.     With Selection.Borders(xlEdgeTop)
  84.         .LineStyle = xlDouble
  85.         .ColorIndex = 0
  86.         .TintAndShade = 0
  87.         .Weight = xlThick
  88.     End With
  89.     With Selection.Borders(xlEdgeBottom)
  90.         .LineStyle = xlDouble
  91.         .ColorIndex = 0
  92.         .TintAndShade = 0
  93.         .Weight = xlThick
  94.     End With
  95.     With Selection.Borders(xlEdgeRight)
  96.         .LineStyle = xlDouble
  97.         .ColorIndex = 0
  98.         .TintAndShade = 0
  99.         .Weight = xlThick
  100.     End With
  101.     With Selection.Borders(xlInsideVertical)
  102.         .LineStyle = xlContinuous
  103.         .ColorIndex = 0
  104.         .TintAndShade = 0
  105.         .Weight = xlThin
  106.     End With
  107.     With Selection.Borders(xlInsideHorizontal)
  108.         .LineStyle = xlContinuous
  109.         .ColorIndex = 0
  110.         .TintAndShade = 0
  111.         .Weight = xlThin
  112.     End With
  113.    
  114.     tamanho = tamanho + 2
  115.     Range(Cells(tamanho, 3), Cells(tamanho, 3)).Select
  116.         With Selection
  117.         .HorizontalAlignment = xlRight
  118.         .VerticalAlignment = xlBottom
  119.         .WrapText = False
  120.         .Orientation = 0
  121.         .AddIndent = False
  122.         .IndentLevel = 0
  123.         .ShrinkToFit = False
  124.         .ReadingOrder = xlContext
  125.         .MergeCells = True
  126.     End With
  127.     ActiveCell.FormulaR1C1 = "Total Estimado:"
  128.    Range(Cells(tamanho, 4), Cells(tamanho, 4)).Select
  129.    ActiveCell.FormulaR1C1 = Application.WorksheetFunction.Sum(Range(Cells(16, 4), Cells(tamanho, 4)))
  130.     Range(Cells(tamanho, 4), Cells(tamanho, 4)).Select
  131.         With Selection.Borders(xlEdgeLeft)
  132.         .LineStyle = xlDouble
  133.         .ColorIndex = 0
  134.         .TintAndShade = 0
  135.         .Weight = xlThick
  136.     End With
  137.     With Selection.Borders(xlEdgeTop)
  138.         .LineStyle = xlDouble
  139.         .ColorIndex = 0
  140.         .TintAndShade = 0
  141.         .Weight = xlThick
  142.     End With
  143.     With Selection.Borders(xlEdgeBottom)
  144.         .LineStyle = xlDouble
  145.         .ColorIndex = 0
  146.         .TintAndShade = 0
  147.         .Weight = xlThick
  148.     End With
  149.     With Selection.Borders(xlEdgeRight)
  150.         .LineStyle = xlDouble
  151.         .ColorIndex = 0
  152.         .TintAndShade = 0
  153.         .Weight = xlThick
  154.     End With
  155.  
  156.     'Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
  157.    Selection.NumberFormat = "#,##0.00 $"
  158.  
  159.     Range(Cells(3, 8), Cells(53, 10)).Interior.ColorIndex = 43
  160.     Range(Cells(3, 9), Cells(52, 9)).Interior.ColorIndex = 19
  161.   Range(Cells(3, 9), Cells(52, 9)).Select
  162.  
  163.     Selection.Borders(xlDiagonalDown).LineStyle = xlNone
  164.     Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  165.     With Selection.Borders(xlEdgeLeft)
  166.         .LineStyle = xlContinuous
  167.         .ColorIndex = 0
  168.         .TintAndShade = 0
  169.         .Weight = xlThin
  170.     End With
  171.     With Selection.Borders(xlEdgeTop)
  172.         .LineStyle = xlContinuous
  173.         .ColorIndex = 0
  174.         .TintAndShade = 0
  175.         .Weight = xlThin
  176.     End With
  177.     With Selection.Borders(xlEdgeBottom)
  178.         .LineStyle = xlContinuous
  179.         .ColorIndex = 0
  180.         .TintAndShade = 0
  181.         .Weight = xlThin
  182.     End With
  183.     With Selection.Borders(xlEdgeRight)
  184.         .LineStyle = xlDouble
  185.         .ColorIndex = 0
  186.         .TintAndShade = 0
  187.         .Weight = xlThin
  188.     End With
  189.     With Selection.Borders(xlInsideVertical)
  190.         .LineStyle = xlContinuous
  191.         .ColorIndex = 0
  192.         .TintAndShade = 0
  193.         .Weight = xlThin
  194.     End With
  195.     With Selection.Borders(xlInsideHorizontal)
  196.         .LineStyle = xlContinuous
  197.         .ColorIndex = 0
  198.         .TintAndShade = 0
  199.         .Weight = xlThin
  200.     End With
  201.  
  202.  
  203.   'Worksheets("Folha1").Range(Cells(16, 1), Cells(tamanho, 5)).Sort Key1:=Worksheets("Folha1").Columns("E"), Order1:=xlAscending
  204.  'Range("A1").Activate
  205.  Application.GoTo Reference:=Worksheets("Folha1").Range("A1"), scroll:=True
  206.  End With
  207. End Sub
  208.  
  209.  
  210. 'https://lh6.googleusercontent.com/-LJy4bPrI_kM/TmecErTbz0I/AAAAAAAAA70/zkXmMgLCvmY/s1055/prioridades_1.jpg
  211. 'https://lh4.googleusercontent.com/-ymTWrsBl-4I/TmecHF9yz9I/AAAAAAAAA78/w04rtVAezTc/s1055/prioridades_2.jpg
Advertisement
Add Comment
Please, Sign In to add comment