Display a MsgBox when any value in a column matches specified value - excel

I want to display a MsgBox when
any value of the "I" column is equal to "*"
any value of the "AT" column equals "Inexistente"
I receive errors.
All the values in the columns are strings
Sub Booluno()
Dim concepto As Excel.Range
Set concepto = ThisWorkbook.Worksheets("Base 2019").Range("AT6:AT1040")
Dim valoresconcepto As String
valoresconcepto = concepto.Value
Dim request As Excel.Range
Set request = ThisWorkbook.Worksheets("Base 2019").Range("I6:I1040")
Dim valoresrequest As String
valoresrequest = request.Value
Dim Bool As Boolean
If valoresconcepto = "Inexistente" And valoresrequest <> "*" Then
Bool = True
ElseIf valoresconcepto = "Inexistente" And valoresrequest = "*" Then
Bool = False
End If
If Bool = True Then
MsgBox "Ojo, Hay nuevos conceptos por agregar para los requerimientos"
If Bool = False Then
MsgBox "No hay nuevos conceptos por añadir, puedes subir el archivo a nuestra carpeta una vez termines :)"
End If
End If
End Sub

These are problematic:
valoresconcepto = concepto.Value
and
valoresrequest = request.Value
Since concepto and request are multi-cell ranges, their .Values are 2D Variant arrays, not Strings.
One option to simplify is to use WorksheetFunction.CountIfs or its late-bound form, Application.CountIfs, using ~ to escape the *.
If Application.CountIfs(concepto, "Inexistente") > 0 Then
If Application.CountIfs(request, "~*") = 0 Then
MsgBox "Ojo, Hay nuevos conceptos por agregar para los requerimientos"
Else
MsgBox "No hay nuevos conceptos por añadir, puedes subir el archivo a nuestra carpeta una vez termines :)"
End If
End If

You need to loop:
For i = 1 to request.Rows.Count
if concepto.Cells(i, 1).Value = "*" Then
Msg box("blah blah 1")
Exit For
EndIf
if request.Cells(i, 1).Value = "Inexistante" Then
Msg box("blah blah 2")
Exit For
EndIf
Next

Related

Same macro crashes in Excel 365 but works perfectly on Excel 2007

I got a macro that opens 2 workbooks and make some calculations. It works perfectly on Excel 2007 32 bit.
But in Excel 365 64 bits it crashes right after opening the first workbook, with no messages errors. Excel quits directly with no warning.
After some testing, I think it fails right after asking first workbook. The code is:
Sub PROCESO(ByVal EstasHojas As String)
Dim WBSource As Workbook
Dim WBDestiny As Workbook
Dim WKSource As Worksheet
Dim WKDestiny As Worksheet
Dim WBintermedio As Workbook
Dim WKIntermedia As Worksheet
Dim Ruta As String
Dim MiMatriz As Variant
Dim MatrizCampos As Variant
Dim LR As Long
Dim LC As Long
Dim i As Long
Dim j As Long
Dim MiF As WorksheetFunction: Set MiF = WorksheetFunction
Dim FechaPrevista As Long
Dim FechaReal As Long
Dim PagoEur As Long
Dim Proveedor As Long
Dim MatrizHojas As Variant
Dim NoHayDatos As Byte
Dim STRColor As String
Dim MatrizFinal() As Variant
Dim DictFechas As Object
Dim FechaDict As Variant
RutaCostIncomes = ""
RutaCashflow = ""
Application.Calculation = xlCalculationManual
'primero total hojas
MatrizHojas = Split(EstasHojas, "||")
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Seleccione archivo COST AND INCOMES"
.AllowMultiSelect = False
If .Show = False Then
MsgBox "No se ha seleccionado ningún archivo.", vbCritical, "PROCESO ABORTADO"
GoTo Final
Else
Ruta = .SelectedItems(1)
Set WBSource = Application.Workbooks.Open(Ruta)
DoEvents
End If
End With
Stop
'//////////////////////////////////////añadimos primero comprobación de que cada campo sea del tipo que le corresponde.
' se crean variables solo para esta comprobación y no se usarán más
Dim HayDatosMal As Boolean
Dim WKErrores As Worksheet
Dim KK As Long
KK = 3
HayDatosMal = False
For j = 0 To UBound(MatrizHojas) - 1 Step 1
Set WKSource = Nothing
Set WKSource = WBSource.Worksheets(CByte(MatrizHojas(j))) 'la posición de la hoja
LR = WKSource.Range("A" & WKSource.Rows.Count).End(xlUp).Row
Dim ZZ As Long
For ZZ = 12 To 3 Step -1
Select Case ZZ
Case 3, 4, 9, 10 'son campos de fechas
For i = 2 To LR Step 1
If IsDate(WKSource.Cells(i, ThisWorkbook.Worksheets("PANEL CONTROL").Range("C" & ZZ).Value)) = False And WKSource.Cells(i, ThisWorkbook.Worksheets("PANEL CONTROL").Range("C" & ZZ).Value) <> "" Then
HayDatosMal = True
If WKErrores Is Nothing Then Set WKErrores = Application.Workbooks.Add.ActiveSheet
With WKErrores
.Range("A1").Value = "INFORME DE ERRORES ENCONTRADOS"
.Range("A3").Value = "HOJA"
.Range("B3").Value = "FILA"
.Range("C3").Value = "CAMPO"
KK = KK + 1
.Range("A" & KK).Value = UCase(WKSource.Name)
.Range("B" & KK).Value = i
.Range("C" & KK).Value = UCase(ThisWorkbook.Worksheets("PANEL CONTROL").Range("A" & ZZ).Value)
End With
End If
Next i
Case 5, 11 'tienen que ser numéricos
For i = 2 To LR Step 1
If IsNumeric(WKSource.Cells(i, ThisWorkbook.Worksheets("PANEL CONTROL").Range("C" & ZZ).Value)) = False And WKSource.Cells(i, ThisWorkbook.Worksheets("PANEL CONTROL").Range("C" & ZZ).Value) <> "" Then
HayDatosMal = True
If WKErrores Is Nothing Then Set WKErrores = Application.Workbooks.Add.ActiveSheet
With WKErrores
.Range("A1").Value = "INFORME DE ERRORES ENCONTRADOS"
.Range("A3").Value = "HOJA"
.Range("B3").Value = "FILA"
.Range("C3").Value = "CAMPO"
KK = KK + 1
.Range("A" & KK).Value = UCase(WKSource.Name)
.Range("B" & KK).Value = i
.Range("C" & KK).Value = UCase(ThisWorkbook.Worksheets("PANEL CONTROL").Range("A" & ZZ).Value)
End With
End If
Next i
Case Else 'son textos o están vacíos, no hacemos nada
DoEvents
End Select
Next ZZ
Next j
If HayDatosMal = True Then
'hay que abortar proceso
WBSource.Close False
WKErrores.Activate
WKErrores.Columns("A:C").EntireColumn.AutoFit
Set WKErrores = Nothing
MsgBox "Se cancela el proceso porque se han encontrado errores en los datos de origen. Se ha generado un informe de errores para consultar.", vbCritical, "PROCESO CANCELADO"
GoTo Final
End If
DoEvents
'////////////////////// fin comprobación
'compruebo que los campos coincida con mis datos del configurador
MatrizCampos = ThisWorkbook.Worksheets("PANEL CONTROL").Range("A2").CurrentRegion.Value
'compruebo todas las hojas
For j = 0 To UBound(MatrizHojas) - 1 Step 1
Set WKSource = Nothing
Set WKSource = WBSource.Worksheets(CByte(MatrizHojas(j))) 'la posición de la hoja
With WKSource
'los campos empiezan en la fila 2 de los datos de la matriz de campos
'comprobamos que en source estén con el mismo nombre en su posición
For i = 2 To UBound(MatrizCampos) Step 1
If MiF.CountIf(.Rows(1), MatrizCampos(i, 1)) = 0 Then
'el campo no está presente. Abortamos
MsgBox "El campo " & UCase(MatrizCampos(i, 1)) & " no está en la hoja " & WKSource.Index & " de COST AND INCOMES", vbCritical, "PROCESO ABORTADO"
WBSource.Close False
GoTo Final
Else
'compruebo que esté en su posición
LR = MiF.Match(MatrizCampos(i, 1), .Rows(1), 0)
If LR <> MatrizCampos(i, 3) Then
'no está donde marca el PANEL CONTROL
MsgBox "El campo " & UCase(MatrizCampos(i, 1)) & " no está en la posición que marca PANEL CONTROL en la hoja " & WKSource.Index & " de COST AND INCOMES", vbCritical, "PROCESO ABORTADO"
WBSource.Close False
GoTo Final
End If
End If
Next i
End With
Next j
Set WKSource = Nothing
'también comprobamos los campos de ingresos
MatrizCampos = ThisWorkbook.Worksheets("PANEL CONTROL").Range("A8").CurrentRegion.Value
'compruebo todas las hojas
For j = 0 To UBound(MatrizHojas) - 1 Step 1
Set WKSource = Nothing
Set WKSource = WBSource.Worksheets(CByte(MatrizHojas(j))) 'la posición de la hoja
With WKSource
'los campos empiezan en la fila 2 de los datos de la matriz de campos
'comprobamos que en source estén con el mismo nombre en su posición
For i = 2 To UBound(MatrizCampos) Step 1
If MiF.CountIf(.Rows(1), MatrizCampos(i, 1)) = 0 Then
'el campo no está presente. Abortamos
MsgBox "El campo " & UCase(MatrizCampos(i, 1)) & " no está en la hoja " & WKSource.Index & " de COST AND INCOMES", vbCritical, "PROCESO ABORTADO"
WBSource.Close False
GoTo Final
Else
'compruebo que esté en su posición
LR = MiF.Match(MatrizCampos(i, 1), .Rows(1), 0)
If LR <> MatrizCampos(i, 3) Then
'no está donde marca el PANEL CONTROL
MsgBox "El campo " & UCase(MatrizCampos(i, 1)) & " no está en la posición que marca PANEL CONTROL en la hoja " & WKSource.Index & " de COST AND INCOMES", vbCritical, "PROCESO ABORTADO"
WBSource.Close False
GoTo Final
End If
End If
Next i
End With
Next j
Set WKSource = Nothing
MatrizCampos = ThisWorkbook.Worksheets("PANEL CONTROL").Range("A2").CurrentRegion.Value
'the code never reachs this part when it crashes
EstasHojas is just a string that contains text like 1|2|
I've read this but could not find a solution.
VBA force closes Excel 365 but works fine in Excel 2019
64-bit Excel 365 crashes, 32-bit Excel 365 works fine
Also tried adding DoEvents right after opening the workbook with no luck.
No add-ins involved at all.
Now comes the funny part. If I add a Stop command right after opening the first workbook, and then VBa stops there, I press F5 so macro keeps going, everything works perfect!
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Seleccione archivo COST AND INCOMES"
.AllowMultiSelect = False
If .Show = False Then
MsgBox "No se ha seleccionado ningún archivo.", vbCritical, "PROCESO ABORTADO"
GoTo Final
Else
Ruta = .SelectedItems(1)
Set WBSource = Application.Workbooks.Open(Ruta)
DoEvents
End If
End With
Stop 'this fixes everything
So if I try to execute all at once, it crashed with no errors. But if I force it to make a break and then continue, it works.
I would like to know why adding the Stop makes the code works perfectly on Eccel 365 but without it it crashed and closes Excel with no errors. Tried DoEvents as I said, but it did not help in this case.
By the way, the workbooks opened are just data in XLSX files, no other macros or events. Just this code. I can post the full code if needed but it's really long.
Thanks in advance.

Userform doesn't fill the sheet I want it to

I'm new at VBA and would like to ask for help.
I have a userform that is supposed to help me fill a range in my worksheet.
It was working fine when I made it a few days ago.
Now though, it only adds an empty row to my table and I don't know where to look to find the error. I've been at it for a couple of hours now..
Any help would be appreciated.
This is my code :
Private Sub CommandButton2_Click()
Dim dl As Integer
Dim list_num As Integer
Dim ligne As Integer
list_num = Me.liste_com3.ListCount - 1
If Me.liste_com3.ListCount > 0 Then 'contrôl si la liste n'est pas vide
If MsgBox("Voulez-vous enregistrer cette transaction ?", vbYesNo) = vbYes Then
For ligne = 0 To list_num
'ajouter nouvelle ligne dans le tableau
Sheets(8).ListObjects(1).ListRows.Add
'chercher numéro prochaine ligne du tableau
dl = Sheets(8).Range("b9999").End(xlUp).Row
'ajouter les infos dans la bdd
Sheets(8).Range("Z" & dl) = Me.info1
Sheets(8).Range("C" & dl) = Format(Me.txt_fac3, """FAC-""00000")
Sheets(8).Range("D" & dl) = Format(CDate(Now()), "dd/mm/yyyy hh:mm:ss")
Sheets(8).Range("E" & dl) = Me.cbx_com
'controler si c'est un fournisseur ou un client
If Me.label_type = "Fournisseur :" Then
Sheets(8).Range("F" & dl) = Me.cbx_type3
Else
Sheets(8).Range("G" & dl) = Me.cbx_type3
End If
'ajouter les données de la zone de liste
Sheets(8).Range("H" & dl) = Me.liste_com3.List(ligne, 0)
Sheets(8).Range("J" & dl) = Int(Me.liste_com3.List(ligne, 1))
Next ligne
MsgBox "Enregistrement réussi !"
Unload Me
ThisWorkbook.Save
End If
End If
Sheets(8).Range("K6:N9999").NumberFormat = "#,##0 [$XOF]"
End Sub
Private Sub liste_com3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
If Me.liste_com3.ListIndex >= 0 Then
If MsgBox("Voulez-vous supprimer cette entrée ?", vbYesNo) = vbYes Then
Me.liste_com3.RemoveItem Me.liste_com3.ListIndex
memo = memo - 1
End If
End If
End Sub
Private Sub opt_in_Click()
Me.label_type = "Fournisseur :"
Me.cbx_type3.RowSource = "col_fourni"
Me.cbx_com.RowSource = "col_comm"
Me.info1 = "Entrée"
End Sub
Private Sub opt_out_Click()
Me.label_type = "Client :"
Me.cbx_type3.RowSource = "col_clients"
Me.cbx_com.RowSource = ""
Me.info1 = "Sortie"
End Sub
Private Sub txt_fac3_Change()
If Not IsNumeric(txt_fac3) And txt_fac3 <> "" Then
MsgBox "Veuillez entrer un nombre..."
Me.txt_fac3 = ""
End If
End Sub
Private Sub txt_num3_Change()
'controle si numérique
If Not IsNumeric(txt_num3) And txt_num3 <> "" Then
MsgBox "Veuillez entrer un nombre..."
Me.txt_num3 = ""
End If
End Sub
Private Sub UserForm_Initialize()
Me.label_info_3.Caption = "Mouvements"
End Sub
Also the weird thing is that when I change the sheet that the userform is supposed to write to, it works. But not on the sheet I want.
This is a picture of my userform
Thanks in advance !

Error to verify a password when I run the userform

I have this code to valid a username and a password but every try I make to see if it´s working does not find the password and I can not find the error in the code for that, I don´t know if I need to set an specific sheet where the password is settle or what. If you can help me I´ll be thankfull.
Private Sub CommandButton1_Click()
Dim usuario As String
Dim password As Variant
Dim IDExistente
Dim DatoEncontrado
Dim Rango As Range
IDExistente = Application.WorksheetFunction.CountIf(Sheets("Cines").Range("B:B"), Me.TextBox1.Value)
Set Rango = Sheets("Cines").Range("B:C")
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox "Se requiere del ID y la contraseña para obtener los folios", vbOKOnly, "Aviso"
TextBox1.SetFocus
ElseIf IDExistente = 0 Then
MsgBox "Debes ingresar un ID valido para obtener los folios favor de validar.", vbOKOnly, "Error"
ElseIf IDExistente = 1 Then
DatoEncontrado = Rango.Find(What:=Me.TextBox1.Value, MatchCase:=True, LookAt:=xlWhole).Address
Contrasena = CStr(Range(DatoEncontrado).Offset(RowOffset:=0, ColumnOffset:=1).Value)
If CStr(Range(DatoEncontrado).Value) = Me.TextBox1.Value And Contrasena = Me.TextBox2.Value Then
Hoja1.Range("E1") = TextBox1.Text
Hoja1.Range("E2") = TextBox2.Text
Else: MsgBox "La contraseña no es correcta favor de validar.", vbOKOnly, "Error"
TextBox2.SetFocus
End If
End If
End Sub

How to change variables of a Word footer from Excel vba?

I have several variables to be updated in a Word footer from Excel.
I am only able to change variables out of the footer.
Sub Internal_Offer()
Dim datos(1 To 100) As String
Dim reemp(1 To 100) As String
wArch = Hoja1.Range("B2").Text & Hoja1.Range("B1").Text & ".docx"
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Add Template:=wArch, NewTemplate:=False, DocumentType:=0
lenght = Hoja1.Range("B3").Value
For i = 1 To lenght - 1 'celda dónde está la cuenta
datos(i) = Hoja1.Range("A" & i + 3).Text 'dónde están los datos
reemp(i) = Hoja1.Range("B" & i + 3).Text 'dónde están las etiquetas
Next i
objWord.Activate 'Activa el documento de word
For i = 1 To lenght - 1 'celda dónde está la cuenta
With objWord.Selection.Find
.Text = datos(i) 'busca el texto de datos
.Replacement.Text = reemp(i) 'reemplaza por el texto
.Forward = True
.Wrap = 1
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=2
End With
Next i
End Sub
There are at least two ways to address all footers of a Word document:
StoryRanges
Sections.Footers
Please try this (ActiveDocument is your objWord):
Private Sub CheckAllDocumentFooters()
Dim r As Word.Range
Dim s As Word.Section
Dim hf As Word.HeaderFooter
' either all story ranges:
For Each r In ActiveDocument.StoryRanges
Select Case r.StoryType
Case wdEvenPagesFooterStory, wdPrimaryFooterStory, wdFirstPageFooterStory
r.WholeStory
Debug.Print r.Text
End Select
' further sections:
While Not (r.NextStoryRange Is Nothing)
Set r = r.NextStoryRange
Select Case r.StoryType
Case wdEvenPagesFooterStory, wdPrimaryFooterStory, wdFirstPageFooterStory
r.WholeStory
Debug.Print r.Text
End Select
Wend
Next r
' or all sections:
For Each s In ActiveDocument.Sections
For Each hf In s.Footers
Debug.Print hf.Index
Debug.Print hf.Range.Text
Next hf
Next s
End Sub

How to export outlook attachment to excel?

I've tried to export an Outlook attachment to Excedl using the following cocde:
Public bCancel As Boolean
Public dDate As Date
Public sPath As String
Public Sub TESTMACRO()
Dim MyOlApp As New Outlook.Application
Dim MyOlExp As Outlook.Explorer
Dim MySelection As Outlook.Selection
Dim msg As MailItem
Dim amount As Long
Set MyOlExp = MyOlApp.ActiveExplorer
Set MySelection = MyOlExp.Selection
amount = MySelection.Count
bCancel = True
TESTMACROX.Show
If Not bCancel Then
If amount = 1 Then
WaitFinish.Show (False)
Dim objTesteInforme As New TesteInforme
objTesteInforme.fecha = dDate
objTesteInforme.path = sPath
' Procesamos el mensaje seleccionado
If MySelection.Item(1).Class = olMail Then
' El objeto seleccionado es un correo
Set msg = MySelection.Item(1)
If InStr(1, msg.Subject, "YYYYY") > 0 And InStr(1, msg.Subject, "ZZZZZ") > 0 Then
' Se trata de un mensaje de xxxxxxxxxxxxxxxxxx
objTesteInforme.CLIENTE = "YYYYYZZZZZ"
If (Not objTesteInforme.GeneraTestMacro(msg)) Then
' El procesamiento fracasó
MsgBox "El procesamiento del mensaje " & msg.Subject & " fracasó; por favor, revisa el formato del archivo adjunto", vbCritical
End If
Else
MsgBox "El mensaje " & msg.Subject & " no se reconoce como diario Batch de ningún cliente", vbCritical
End If
End If
Set msg = Nothing
Else
MsgBox "Por favor, selecciona un correo electrónico", vbExclamation
End If
WaitFinish.Hide
Else
MsgBox "Por favor selecciona uno y solo un mesaje. Gracias", vbExclamation
End If
Set MySelection = Nothing
Set MyOlExp = Nothing
End Sub
The problem is that I select only one message, with the subject YYYYY ZZZZZ - LLLLLLL: 140109, but instead of opening an excel sheet with the attachment it send teh error message "Por favor selecciona uno y solo un mesaje. Gracias". Why?
I tried your code, but I can't translate the spanish methods into english properly (I can't find equivalent of New TestInforme).
Based on the error message, it seems that your loop logic is failing. Try to not use a negative loop. Something like:
...
If bCancel=False then
...
Else
MsgBox
End If
...

Resources