Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I just wanted to say that you guys RULE, a million thank you's to whoever created, and/or manages this site. KEEP UP THE GOOD WORK..."

Geography

Where in the world do Tek-Tips members come from?
pluto1415 (MIS)
21 Jun 12 14:46
I've got an excel spreadsheet that will contain data in up to 3 columns. If column A is populated, then either column B or column C (or both) will also be populated. If A is empty then B&C are by definition empty (even if someone makes a mistake and enters stuff there) A is a date, B&C are currency.

My VBA reads these rows and writes to a pipe delimited text file for import into another system we have. The problem I'm having is that if either B or C is null (which it frequently is) then the code just bombs out and says Type Mismatch. What I need it to do is just write a zero.

CODE --> VBA

Sub ExportPremiums_Click()
Dim LastDataRow As Integer
Dim StartDataRow As Integer
Dim OutPutFileName As String
Dim OutPath As String
Dim FileName As String
Dim NumRows As Integer
Dim PremAmt As Currency
Dim PremDate As Date
Dim LoanAmt As Currency

'Check for Policy Number
If [b1] <> "" Then
'Unlock sheet
Worksheets("sheet1").Unprotect Password:="password"
'Obtain Number of the Last Row that has Premium Data
StartDataRow = 4 'This is always the first row that has Premium Data
With [A4].CurrentRegion
LastDataRow = .Rows.Count + .Row - 1
End With

'Declare name and location for new .txt file
'Use the policy number found on another tab to name the file
FileNum = FreeFile()
FileName = Worksheets("Sheet1").Cells(1, 2).Value
OutPath = "C:\Premium\"
OutPutFileName = OutPath & FileName & ".txt"
Open OutPutFileName For Output As #FileNum

'Determine total number of rows to write out
NumRows = LastDataRow - StartDataRow + 1

'Write Rows
For x = 1 To NumRows
PremDate = Trim(Worksheets("Sheet1").Cells(x + StartDataRow - 1, 1).Value)
PremAmt = Trim(Worksheets("Sheet1").Cells(x + StartDataRow - 1, 2).Value)
LoanAmt = Trim(Worksheets("Sheet1").Cells(x + StartDataRow - 1, 3).Value)

Print #FileNum, PremDate & "|" & PremAmt & "|" & LoanAmt

Next x
Close #FileNum

'Re-Lock sheet
Worksheets("Sheet1").Protect Password:="password"
MsgBox ("Output file has been created, go onto the next step")

Else: MsgBox ("No Policy Number Entered")
End If

End Sub
SkipVought (Programmer)
21 Jun 12 14:54

hi,

"Type Mismatch" on what statement?

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

pluto1415 (MIS)
21 Jun 12 15:17
Sorry - it's on whichever line is currently null - so one of these:
PremAmt = Trim(Worksheets("Sheet1").Cells(x + StartDataRow - 1, 2).Value)
LoanAmt = Trim(Worksheets("Sheet1").Cells(x + StartDataRow - 1, 3).Value)

Helpful Member!  SkipVought (Programmer)
21 Jun 12 15:28


It is the Data Type that you declared for these variables.

Your NUMERIC variables cannot be NULL. They can only be ZERO or any other real number.

You might consider using the Variant data type.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close