Skip to content

Instantly share code, notes, and snippets.

@fransafu
Created July 26, 2018 21:54
Show Gist options
  • Save fransafu/ffd0a2a4dc68213f26f79b503947d9f9 to your computer and use it in GitHub Desktop.
Save fransafu/ffd0a2a4dc68213f26f79b503947d9f9 to your computer and use it in GitHub Desktop.
Import and Clear filename from path route [MACRO][VB]
Sub ImportarData()
Dim pathToExcel As String
Dim sheetExcelOrigin As String
Dim numRows As Long
pathToExcel = "C:\Users\fransafu\Desktop\filename.xlsx"
sheetExcelOrigin = "Sheet1"
sheetExcelDest = "Destino"
strSplit = "/"
Dim wb As Workbook
Set wb = Workbooks.Open(pathToExcel)
numRows = wb.Sheets(sheetExcelOrigin).Range("A" & Rows.Count).End(xlUp).Row
ThisWorkbook.Sheets(sheetExcelDest).Range("A1:A" & numRows).Value = wb.Sheets(sheetExcelOrigin).Range("B2:B" & numRows).Value
wb.Close
' Llama a Subproceso para limpiar nombre
Call clearFilename
End Sub
Sub clearFilename()
On Error GoTo Errores
' Obtener Ultima fila y ultima columna
Dim numRows As Long
' Guardar texto de la ruta
Dim strDoc As Variant
' Guardar arreglo separado por la variable strSplit
Dim arraySplit() As String
Dim strSplit As String
' Posicion del elemento en el arreglo que queremos buscar
Dim position As Integer
' Obtener ultima fila basado en la columna A
numRows = Range("A" & Rows.Count).End(xlUp).Row
' Definir parametro para separar
strSplit = "/"
' Itera fila por fila
For i = 1 To numRows
' Obtener valor de la celda
strDoc = Cells(i, 1).Value
' Realizar Split del valor segun la separacion que queremos
arraySplit = Split(strDoc, strSplit)
' Obtener la ultima posicion del arreglo
position = UBound(arraySplit)
Cells(i, 1).Value = arraySplit(position)
Next i
Errores:
MsgBox "Puede que una celda no sea posible procesar por String." & _
" Revisar si el proceso se complelto adecuadamente"
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment