Foros del Web » Programación para mayores de 30 ;) » Programación General » Visual Basic clásico »

Problema con excel y macros

Estas en el tema de Problema con excel y macros en el foro de Visual Basic clásico en Foros del Web. Soy nuevo en esto del Visual basic y me han encomendado un trabajo en el que no se me ocurre como solucionarlo: Tengo una tabla ...
  #1 (permalink)  
Antiguo 25/02/2008, 08:41
 
Fecha de Ingreso: febrero-2008
Mensajes: 4
Antigüedad: 16 años, 2 meses
Puntos: 0
Problema con excel y macros

Soy nuevo en esto del Visual basic y me han encomendado un trabajo en el que no se me ocurre como solucionarlo:

Tengo una tabla en la que he insertado un CommandButton1 y que cada fila cuenta con un OptionButton. Lo que trato de hacer es que cuando un optionbutton (solo puede estar activo uno) este activado al darle al commandbutton1 busque un archivo excel en una carpeta con el mismo nombre que tiene una casilla de la fila del optionbutton seleccionada y lo abra. En caso de no haber ninguno tiene que crear un excel con el nombre de la casilla para todos los casos que no exista concordancia.
Además algunas casillas de la fila del optionbutton tienen que copiarse a la nueva hoja creada.
Mirando esta pagina y en buscadores he aprendido a hacer cosas de arriba, pero combinarlas todas es otra historia. Es complicado, os agradeceré mucho si me ayudas
Un saludo
  #2 (permalink)  
Antiguo 25/02/2008, 11:39
Usuario no validado
 
Fecha de Ingreso: mayo-2006
Mensajes: 42
Antigüedad: 18 años
Puntos: 0
Re: Problema con excel y macros

Estas programando en excel o vbasic?

Obviamente ademas de la solucion por APIS bien puedes aplicar el metodo filter de VBA de los cuales hay multiples ajemplos en san google ... igualmente la creacion de archivos y demas.

Saludos

Última edición por opermty; 25/02/2008 a las 11:45
  #3 (permalink)  
Antiguo 25/02/2008, 11:40
Usuario no validado
 
Fecha de Ingreso: mayo-2006
Mensajes: 42
Antigüedad: 18 años
Puntos: 0
Re: Problema con excel y macros

Una solucion de busqueda por APIS

Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Const MAX_PATH = 260
Const MAXDWORD = &HFFFF
Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Function StripNulls(OriginalStr As String) As String
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If
StripNulls = OriginalStr
End Function

Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
'KPD-Team 1999
'E-Mail: [email protected]
'URL: http://www.allapi.net/

Dim FileName As String ' Walking filename variable...
Dim DirName As String ' SubDirectory Name
Dim dirNames() As String ' Buffer for directory name entries
Dim nDir As Integer ' Number of directories in this path
Dim i As Integer ' For-loop counter...
Dim hSearch As Long ' Search Handle
Dim WFD As WIN32_FIND_DATA
Dim Cont As Integer
If Right(path, 1) <> "\" Then path = path & "\"
' Search for subdirectories.
nDir = 0
ReDim dirNames(nDir)
Cont = True
hSearch = FindFirstFile(path & "*", WFD)
If hSearch <> INVALID_HANDLE_VALUE Then
Do While Cont
DirName = StripNulls(WFD.cFileName)
' Ignore the current and encompassing directories.
If (DirName <> ".") And (DirName <> "..") Then
' Check for directory with bitwise comparison.
If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
dirNames(nDir) = DirName
DirCount = DirCount + 1
nDir = nDir + 1
ReDim Preserve dirNames(nDir)
End If
End If
Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
Loop
Cont = FindClose(hSearch)
End If
' Walk through this directory and sum file sizes.
hSearch = FindFirstFile(path & SearchStr, WFD)
Cont = True
If hSearch <> INVALID_HANDLE_VALUE Then
While Cont
FileName = StripNulls(WFD.cFileName)
If (FileName <> ".") And (FileName <> "..") Then
FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
FileCount = FileCount + 1
List1.AddItem path & FileName
End If
Cont = FindNextFile(hSearch, WFD) ' Get next file
Wend
Cont = FindClose(hSearch)
End If
' If there are sub-directories...
If nDir > 0 Then
' Recursively walk into them...
For i = 0 To nDir - 1
FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
Next i
End If
End Function
Sub Command1_Click()
Dim SearchPath As String, FindStr As String
Dim FileSize As Long
Dim NumFiles As Integer, NumDirs As Integer
Screen.MousePointer = vbHourglass
List1.Clear
SearchPath = Text1.Text
FindStr = Text2.Text
FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
Screen.MousePointer = vbDefault
End Sub
  #4 (permalink)  
Antiguo 25/02/2008, 11:47
Usuario no validado
 
Fecha de Ingreso: mayo-2006
Mensajes: 42
Antigüedad: 18 años
Puntos: 0
Re: Problema con excel y macros

Sub Listado_archivos()
Dim Ruta As String, Nombre As String, i As Integer
Ruta = Application.ActiveWorkbook.Path + "\*.xls"
Nombre = Dir(Ruta)
i = 1
Do While Nombre <> ""
Cells(i, 1) = Nombre
i = i + 1
Nombre = Dir
Loop
End Sub
  #5 (permalink)  
Antiguo 25/02/2008, 11:56
Usuario no validado
 
Fecha de Ingreso: mayo-2006
Mensajes: 42
Antigüedad: 18 años
Puntos: 0
Re: Problema con excel y macros

O por script:

call prueba

sub prueba()
Dim fso, f1
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists("status.vbs")) Then set f1 = nothing: set fso = nothing: wscript.echo "Existe" : exit sub
Set f1 = fso.CreateTextFile("status.vbs", True)
end sub
  #6 (permalink)  
Antiguo 25/02/2008, 11:57
 
Fecha de Ingreso: febrero-2008
Mensajes: 4
Antigüedad: 16 años, 2 meses
Puntos: 0
Re: Problema con excel y macros

Muchísimas gracias, haber si lo consigo

Saludos
  #7 (permalink)  
Antiguo 25/02/2008, 12:22
 
Fecha de Ingreso: febrero-2008
Mensajes: 4
Antigüedad: 16 años, 2 meses
Puntos: 0
Re: Problema con excel y macros

jeje Me estoy mareando un poco jaja, pero pedazo código, demasiado para alguien como yo que no tiene ni idea de programación, estoy trabajando en excel. Lo de la relación entre el comand y el option button estan en el codigo?
Perdon por mi ignorancia

Un saludo y muchas gracias por tu tiempo
  #8 (permalink)  
Antiguo 25/02/2008, 15:05
Usuario no validado
 
Fecha de Ingreso: mayo-2006
Mensajes: 42
Antigüedad: 18 años
Puntos: 0
Re: Problema con excel y macros

Cita:
Iniciado por aska Ver Mensaje
jeje Me estoy mareando un poco jaja, pero pedazo código, demasiado para alguien como yo que no tiene ni idea de programación, estoy trabajando en excel. Lo de la relación entre el comand y el option button estan en el codigo?
Perdon por mi ignorancia

Un saludo y muchas gracias por tu tiempo

El codigo dentro del commandbutton sirve para validar la propiedad true del optionbutton ... dependiendo del caso hace la opcion que necesitas ...
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:21.