Reputation: 153
Trying to create code that will export my Excel invoice sheet to PDF, to a specified file path. The path is based on the whether the invoice lists a certain product, ProductX.
This is what I came up with, but seems cumbersome to loop through every single cell in a range to see if ProductX is there.
Is there an easier way to do this? Appreciate any help!
Sub ExportToPDF()
'
Dim file_path As String
Dim search_range As Range
Dim each_cell As Range
' Set search_range as desired search range
Set search_range = ActiveSheet.Range("A53:R56")
For Each each_cell In search_range.Cells
If InStr(1, each_cell.Value, "ProductX", vbTextCompare) Then
file_path = Some_path_A
Else: file_path = Some_path_B
End If
Next each_cell
'Export the sheet as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=file_path, Quality:=xlQualityStandard _
, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End Sub
Upvotes: 4
Views: 124
Reputation: 818
There is a way to find exactly that with just a line of code however it will work only the if you search on 1 column. For your case I think it will work because usually the product name will be in a column. The code is as follows:
Dim test As Variant
Product = "ProductX"
' Set search_range as desired search range
search_range = Application.WorksheetFunction.Transpose(Sheets(1).Range("A53:A56"))
If UBound(Filter(search_range, Product)) > -1 Then
file_path = Some_path_A
Else
file_path = Some_path_B
End If
You can give it a try and let me know if this works for you. If not I will try to find a way to do it with multiple columns and improve the answer
Upvotes: 1
Reputation: 29421
I think the shortest way is the following:
If WorksheetFunction.CountIf(ActiveSheet.Range("A53:R56"), "*ProductX*") = 0 Then Exit Sub
which can be further reduced to:
If WorksheetFunction.CountIf(Range("A53:R56"), "*ProductX*") = 0 Then Exit Sub
since ActiveSheet
is the default ragnge worksheet
qualification
Upvotes: 1
Reputation: 9976
Based on what brettdj has suggested, you can use the proposed code like below...
Sub ExportToPDF()
Dim file_path As String
Dim search_range As String
Dim each_cell As Range
Dim rng1 As Range
' Set search_range as desired search range
search_range = ActiveSheet.Range("A53:R56")
Set rng1 = ActiveSheet.Range("A53:R56").Find("ProductX", , xlFormulas, xlPart)
If Not rng1 Is Nothing Then
file_path = Some_path_A
Else
file_path = Some_path_B
End If
'Export the sheet as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=file_path, Quality:=xlQualityStandard _
, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End Sub
Upvotes: 3
Reputation: 55682
You can use Find
for a partial match.
This code assumes that the returned path contains the filepath variable you need - you may need to tweak this.
Dim rng1 As Range
Set rng1 = ActiveSheet.Range("A53:R56").Find("ProductX", , xlFormulas, xlPart)
If rng1 Is Nothing Then Exit Sub
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=rng1.Value, Quality:=xlQualityStandard _
, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
Upvotes: 10