Call UDF within Sub with subsequent If Then Else statement

mark_gv

New Member
Joined
Apr 10, 2013
Messages
2
Hi everyone, this is my first post so feel free to advise me on my formatting... I'm a big fan of the site and most of what I know about VBA I learnt from here.

I am trying to call a User Defined Function within a Sub that exports the spreadsheet to a PDF. I want the name of the PDF to be taken from one of two cells depending on how many different visible values are in column G. The Function works fine when used in a cell in the spreadsheet but I get a "Type Mismatch" error when I run it in the macro. It doesn't seem to like my ("G:G") range that I specify despite it working on the spreadsheet.

Code:
Sub CreatePDF()
Dim fname As String
Dim TypeCount As Integer
TypeCount = COUNTU("G:G") ***Error Occurs Here***
 
If TypeCount > 2 Then
Range("J6", Cells(Rows.Count, "J").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Select
fname = ActiveCell
Else
Range("G6", Cells(Rows.Count, "G").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Select
fname = ActiveCell
End If
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "F:\" & fname & ".pdf", _
          Quality:=xlQualityStandard, IncludeDocProperties:=True, _
          IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub

Public Function COUNTU(theRange As Range) As Long
Dim colUniques As New Collection
Dim vArr As Range
Dim vCell As Range
Dim vLcell As Range
Dim oRng As Range
Dim thsSheet As Worksheet
Set thsSheet = theRange.Parent
Set oRng = Intersect(theRange, theRange.Parent.UsedRange)
Set vArr = oRng
On Error Resume Next
For Each vCell In vArr
If thsSheet.Rows(vCell.Row).EntireRow.Hidden = False Then
If vCell <> vLcell Then
If Len(CStr(vCell)) > 0 Then
colUniques.Add vCell, CStr(vCell)
End If
End If
vLcell = vCell
End If
Next vCell
COUNTU = colUniques.Count
End Function

Thank you in advance.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Forum statistics

Threads
1,206,816
Messages
6,075,036
Members
446,114
Latest member
FadDak

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