Advertisement
Guest User

Untitled

a guest
Jul 10th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Private Sub CommandButton1_Click()
  2. 'Si quiero que no me alerte que estoy sobreescribiendo un archivo, pongo Application.DisplayAlerts = False
  3. Application.DisplayAlerts = True
  4. Call Guardar
  5. Call Value
  6. ActiveWorkbook.Save
  7. Call Append2CSV
  8. End Sub
  9.  
  10. Sub Value()
  11. Dim ws As Worksheet
  12. For Each ws In ActiveWorkbook.Sheets
  13.     ws.UsedRange.Value = ws.UsedRange.Value
  14. Next
  15. End Sub
  16.  
  17. Sub Guardar()
  18. Dim nombre As String
  19. 'Elijo la hoja y la celda para el nuevo nombre de archivo
  20. nombre = ThisWorkbook.Sheets("Presupuesto").Range("B2").Value
  21.  
  22. Dim Path As String
  23. 'Elijo el directorio donde guardarlo, con la barra invertida al final
  24. Path = "C:\Users\Public\Desktop\"
  25. ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
  26.  Path & nombre & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:= _
  27.  True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  28. ActiveWorkbook.SaveAs Filename:=Path & nombre, FileFormat:=xlOpenXMLWorkbook, AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
  29.  
  30. End Sub
  31.  
  32. Sub Append2CSV()
  33. Dim tmpCSV As String 'string to hold the CSV info
  34. Dim f As Integer
  35.  
  36. Const CSVFile As String = "C:\Users\Pablo\Desktop\test.csv" 'replace with your filename
  37.  
  38. f = FreeFile
  39.  
  40. Open CSVFile For Append As #f
  41. tmpCSV = Range2CSV(Range("B5:F27"))
  42. Print #f, tmpCSV
  43. Close #f
  44. End Sub
  45.  
  46. Function Range2CSV(list) As String
  47. Dim tmp As String
  48. Dim cr As Long
  49. Dim r As Range
  50. Dim cliente As String
  51.  
  52. cliente = Worksheets("Presupuesto").Range("B2").Value
  53.  
  54. If TypeName(list) = "Range" Then
  55. cr = 5
  56.  
  57. For Each r In list.Cells
  58. If IsEmpty(r.Value) Then
  59. Exit For
  60. End If
  61. If r.Row = cr Then
  62. If tmp = vbNullString Then
  63. tmp = cliente & ";" & r.Value
  64. Else
  65. tmp = tmp & ";" & r.Value
  66. End If
  67. Else
  68. cr = cr + 1
  69. If tmp = vbNullString Then
  70. tmp = r.Value
  71. Else
  72. tmp = tmp & vbNewLine & cliente & ";" & r.Value
  73. End If
  74. End If
  75. Next
  76. End If
  77.  
  78. Range2CSV = tmp
  79. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement