Originally Posted by
chakalido
the button is located in "HojaActiva"
HojaActiva is a sheet, right?
Originally Posted by
chakalido
the code is located in the userform code module and I have an auto sub in the worksheet module that shows the code in the spreadsheet
This is very confusing!
You have a userform! The button is on a sheet and the userform Private Sub CommandButton1_Click() event code responds to that button on the sheet being clicked!
I would have expected the code to be in the code module of the sheet which has the button.
So is there really a userform, or are you describing the sheet with a button and 2 textboxes a userform?
Assuming there is no real userform, try a tweaked version of the following in the code module of the sheet with the button (right-click the tab of the sheet with he button and choose 'View Code'):
Private Sub CommandButton1_Click()
Dim F1 As Long, F2 As Long, UF As Long, xNumero As Double
Dim gCell As Range
Dim ws As Worksheet
Dim path As String, nameWB As String, checknom As String, cadena As String, HojaActiva As Worksheet, HojaNueva As Worksheet
Dim numdoc As String, colm As Long, FirstRowToProcess As Long, SourceWb As Workbook, firstAddress As String
Set HojaActiva = ActiveSheet
UF = Cells(Rows.Count, 1).End(xlUp).Row
FirstRowToProcess = ActiveCell.Row 'you really want to start processing rows at the active cel?!!
Set HojaNueva = ActiveWorkbook.Sheets.Add(after:=HojaActiva)
F2 = 2
path = "randompath"
nameWB = "name"
Set SourceWb = Workbooks.Open(Filename:=path & nameWB)
With HojaActiva
For F1 = FirstRowToProcess To UF
If .Cells(F1, 1).Value = "CENTRO :" Then
xNumero = .Cells(F1, 2).Value
cadena = .Cells(F1, 3).Value & .Cells(F1, 4).Value
Else
If Len(.Cells(F1, 1).Value) > 0 And IsNumeric(.Cells(F1, 1).Value) = True Then
numdoc = .Cells(F1, 5).Value
With HojaNueva
For colm = 1 To 17
If colm < 13 Then
.Cells(F2, colm).Value = HojaActiva.Cells(F1, colm).Value
Else
.Cells(F2, colm).Value = Choose(colm - 12, xNumero, cadena, TextBox1.Value, TextBox2.Value, Now - .Cells(F2, 4).Value)
End If
Next colm
End With
For Each ws In SourceWb.Worksheets
checknom = Mid(ws.Name, 1, 3) 'left(ws.name,3)?
If IsNumeric(checknom) = True Then
Set gCell = ws.Columns("F").find(what:=numdoc, LookIn:=xlFormulas, lookat:=xlWhole, searchformat:=False)
If Not gCell Is Nothing Then
firstAddress = gCell.Address
Do
HojaNueva.Cells(F2, 18).Value = gCell.Offset(, 2).Value '? 'contactos
HojaNueva.Cells(F2, 19).Value = gCell.Offset(, 3).Value '? 'comentarios
F2 = F2 + 1
Set gCell = ws.Columns("F").FindNext(gCell)
Loop While Not gCell Is Nothing And gCell.Address <> firstAddress
End If
End If
'On Error Resume Next 'Will continue if an error results
Next ws
End If
End If
Next F1
End With
SourceWb.Close False
MsgBox ("Hay " & F2 - 2 & " Entradas de datos")
End Sub
It's not very robust (things such as Now - .Cells(F2, 4).Value require the right kind of data to be in cells). It puts multiple finds in columns R and S below the first line, not on the same line.