Searching For a value in a range and write a texte if the value is found

youbitto

New Member
Joined
Jun 8, 2022
Messages
35
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
Hello!

I have multiple Sheets This Ref contains "G"

the first one where I convert to PDF for each reference

Test.xlsx
ABCDEFGHI
1
2
3
4
5
6
7
8
9
10
11
12
13Ref : 017118200320272Comment
14
15
16
17
18
19
20
Branchement


The second sheet is where I take the information from

Test.xlsx
A
2
3
4Ref
5017051500050157
6017051502408156
7017051503018136
8017051503066167
9017051503080117
10017051503096100
11017059651030139
12017118200320272
13
14
TB



The Third one is where the values to search

Test.xlsx
AB
1Reftype
2017051500050157E
3017051502250195E
4017051502408156E
5017051503018136E
6017051503066167E
7017051503080117E
8017051503096100E
9017059651030139E
10017059651030139G
11017118200320272E
12017118200320272G
Contrat


And this is the Code I use to create PDF for each Refrence

VBA Code:
Sub Brt()

Dim Ref As Range

Application.ScreenUpdating = False

lastrow = Sheets("TB").Cells(Rows.Count, "A").End(xlUp).Row

    Dim rngSrc As Range
    Dim arrRef As Variant

Set rngSrc = Sheets("TB").Range("A5:A" & lastrow)
        arrRef = rngSrc.Value2

For i = 5 To lastrow

Sheets("Branchement").Range("C12:D12").ClearContents                                'Vider les cellules
Sheets("Branchement").Range("G12:G13").ClearContents

    
    Sheets("TB").Cells(i, 1).Copy                                 'Réference
    Sheets("Branchement").Range("C13").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    
    Sheets("Branchement").Cells(13, 3).NumberFormat = "@"         'Format texte de cellele C13

    Application.CutCopyMode = False

'________________PDF

Dim wsA As Worksheet
Dim wbA As Workbook
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant

Set wbA = ActiveWorkbook
Set wsA = Sheets("Branchement")
'
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If

strPath = strPath & "\"

strName = wsA.Range("C13").Value _

'create default name for savng file
strFile = strName & " Fiche branchement" & ".pdf"
strPathFile = strPath & strFile

   'export to PDF in current folder
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=strPathFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
      
    Next i

Application.ScreenUpdating = True



End Sub



My Request is that some refrences are doubled and contains "E" and "G" as you can see in "Contrat" sheet
I want while creating a PDF that if any refrence that contains "G" the value of the cell G13 in sheet "PDF" will be "This refrence Contains G"
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I simplified your code a bit and added the part to check if it has "G":

VBA Code:
Sub Brt()
  Dim wsA As Worksheet, wsC As Worksheet
  Dim strPath As String, strFile As String
  Dim c As Range
  
  Application.ScreenUpdating = False
  Set wsA = Sheets("Branchement")
  Set wsC = Sheets("Contrat")
  strPath = ActiveWorkbook.Path
  If strPath = "" Then strPath = Application.DefaultFilePath
  
  For Each c In Sheets("TB").Range("A5", Sheets("TB").Range("A" & Rows.Count).End(xlUp))
    wsA.Range("C12:D12, G12:G13").ClearContents                     'Vider les cellules
    wsA.Range("C13").Value = c.Value
    
    'Check if the reference contains G
    If WorksheetFunction.CountIfs(wsC.Range("A:A"), c.Value, wsC.Range("B:B"), "G") > 0 Then _
       wsA.Range("G13").Value = "This refrence Contains G"
    
    'Create default name for savng file and export to PDF in current folder
    strFile = c.Value & " Fiche branchement" & ".pdf"
    wsA.ExportAsFixedFormat xlTypePDF, strPath & "\" & strFile, xlQualityStandard, True, False, OpenAfterPublish:=False
  Next
  Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Solution
I simplified your code a bit and added the part to check if it has "G":

VBA Code:
Sub Brt()
  Dim wsA As Worksheet, wsC As Worksheet
  Dim strPath As String, strFile As String
  Dim c As Range
 
  Application.ScreenUpdating = False
  Set wsA = Sheets("Branchement")
  Set wsC = Sheets("Contrat")
  strPath = ActiveWorkbook.Path
  If strPath = "" Then strPath = Application.DefaultFilePath
 
  For Each c In Sheets("TB").Range("A5", Sheets("TB").Range("A" & Rows.Count).End(xlUp))
    wsA.Range("C12:D12, G12:G13").ClearContents                     'Vider les cellules
    wsA.Range("C13").Value = c.Value
   
    'Check if the reference contains G
    If WorksheetFunction.CountIfs(wsC.Range("A:A"), c.Value, wsC.Range("B:B"), "G") > 0 Then _
       wsA.Range("G13").Value = "This refrence Contains G"
   
    'Create default name for savng file and export to PDF in current folder
    strFile = c.Value & " Fiche branchement" & ".pdf"
    wsA.ExportAsFixedFormat xlTypePDF, strPath & "\" & strFile, xlQualityStandard, True, False, OpenAfterPublish:=False
  Next
  Application.ScreenUpdating = True
End Sub
Thank you very Much Mr. Dante it works perfectly

 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,138
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top