Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private Sub CommandButton1_Click()
- 'Si quiero que no me alerte que estoy sobreescribiendo un archivo, pongo Application.DisplayAlerts = False
- Application.DisplayAlerts = True
- Call Guardar
- Call Value
- ActiveWorkbook.Save
- Call Append2CSV
- End Sub
- Sub Value()
- Dim ws As Worksheet
- For Each ws In ActiveWorkbook.Sheets
- ws.UsedRange.Value = ws.UsedRange.Value
- Next
- End Sub
- Sub Guardar()
- Dim nombre As String
- 'Elijo la hoja y la celda para el nuevo nombre de archivo
- nombre = ThisWorkbook.Sheets("Presupuesto").Range("B2").Value
- Dim Path As String
- 'Elijo el directorio donde guardarlo, con la barra invertida al final
- Path = "C:\Users\Public\Desktop\"
- ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
- Path & nombre & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:= _
- True, IgnorePrintAreas:=False, OpenAfterPublish:=False
- ActiveWorkbook.SaveAs Filename:=Path & nombre, FileFormat:=xlOpenXMLWorkbook, AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
- End Sub
- Sub Append2CSV()
- Dim tmpCSV As String 'string to hold the CSV info
- Dim f As Integer
- Const CSVFile As String = "C:\Users\Pablo\Desktop\test.csv" 'replace with your filename
- f = FreeFile
- Open CSVFile For Append As #f
- tmpCSV = Range2CSV(Range("B5:F27"))
- Print #f, tmpCSV
- Close #f
- End Sub
- Function Range2CSV(list) As String
- Dim tmp As String
- Dim cr As Long
- Dim r As Range
- Dim cliente As String
- cliente = Worksheets("Presupuesto").Range("B2").Value
- If TypeName(list) = "Range" Then
- cr = 5
- For Each r In list.Cells
- If IsEmpty(r.Value) Then
- Exit For
- End If
- If r.Row = cr Then
- If tmp = vbNullString Then
- tmp = cliente & ";" & r.Value
- Else
- tmp = tmp & ";" & r.Value
- End If
- Else
- cr = cr + 1
- If tmp = vbNullString Then
- tmp = r.Value
- Else
- tmp = tmp & vbNewLine & cliente & ";" & r.Value
- End If
- End If
- Next
- End If
- Range2CSV = tmp
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement