473,394 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Novice Excel VBA coder - type mismatch error

11
Hi,

I'm a total beginner to VBA, so please bear with me if I seem a bit dense.

What I'm Trying to Achieve

I'm trying to write a procedure in Excel, which is supposed to
- look at the pH values stored in column E, row by row
- compare this to a threshold value of 5
- enter a string ("pH range C") in column K, if the value in column E exceeds 5.

The Problem
I'm getting a type mismatch error, as shown in the source code below:

Expand|Select|Wrap|Line Numbers
  1. Sub PNEC()
  2.  
  3. Dim pH As Double
  4. Dim PNEC As String
  5. Dim usedrows As Double
  6. Dim Row As Integer
  7.  
  8. usedrows = Worksheets("Fal").UsedRange.Rows.Count
  9. pH = Worksheets("Fal").Columns("E").Value 'TYPE MISMATCH ERROR HERE
  10. PNEC = Worksheets("Fal").Columns("K").Value
  11.  
  12. For Row = 2 To usedrows Step 1
  13.  
  14. If pH > 5 Then
  15. PNEC = "pH range C"
  16. End If
  17.  
  18. Next Row
  19.  
  20. End Sub
  21.  
What I think Is Happening
In the sheet "Fal", all data in columns E and K are numeric except for the header row, which is text. I think it is this text that is causing the problem, conflicting with the "Double" variable type I've defined for pH.

My Questions
  • Does my diagnosis seem reasonable, or have I misunderstood the problem entirely?
  • How can I get around the problem?

Any insights gratefully received,

Lara
Jan 11 '08 #1
5 9943
MikeTheBike
639 Expert 512MB
Hi,

I'm a total beginner to VBA, so please bear with me if I seem a bit dense.

What I'm Trying to Achieve

I'm trying to write a procedure in Excel, which is supposed to
- look at the pH values stored in column E, row by row
- compare this to a threshold value of 5
- enter a string ("pH range C") in column K, if the value in column E exceeds 5.

The Problem
I'm getting a type mismatch error, as shown in the source code below:

Expand|Select|Wrap|Line Numbers
  1. Sub PNEC()
  2.  
  3. Dim pH As Double
  4. Dim PNEC As String
  5. Dim usedrows As Double
  6. Dim Row As Integer
  7.  
  8. usedrows = Worksheets("Fal").UsedRange.Rows.Count
  9. pH = Worksheets("Fal").Columns("E").Value 'TYPE MISMATCH ERROR HERE
  10. PNEC = Worksheets("Fal").Columns("K").Value
  11.  
  12. For Row = 2 To usedrows Step 1
  13.  
  14. If pH > 5 Then
  15. PNEC = "pH range C"
  16. End If
  17.  
  18. Next Row
  19.  
  20. End Sub
  21.  
What I think Is Happening
In the sheet "Fal", all data in columns E and K are numeric except for the header row, which is text. I think it is this text that is causing the problem, conflicting with the "Double" variable type I've defined for pH.

My Questions
  • Does my diagnosis seem reasonable, or have I misunderstood the problem entirely?
  • How can I get around the problem?

Any insights gratefully received,

Lara
Hi

Your code has a number of fundermental errors (ie coulmns collection takes Integer arguments), but from your description I think something like this is what you want
Expand|Select|Wrap|Line Numbers
  1.     Dim pH As Double
  2.     Dim PNEC As String
  3.     Dim usedrows As Double
  4.     Dim Row As Integer
  5.  
  6.     usedrows = Sheets("Fal").UsedRange.Rows.Count
  7.     For Row = 2 To usedrows Step 1
  8.         pH = Sheets("Fal").Cells(Row, 5)
  9.         If pH > 5 Then
  10.             Sheets("Fal").Cells(Row, 11) = "pH range C"
  11.         Else
  12.             '????
  13.         End If
  14.     Next Row
??

MTB
Jan 11 '08 #2
Lara1
11
OMG - it worked!!!!!!

MikeTheBike thankyouthankyouthankyou, I am SO grateful :-)
Jan 11 '08 #3
Lara1
11
Hi, I'm in trouble again.

This time, I'm trying to amend my procedure so that it will work through the spreadsheet line-by-line, checking the values in columns D and E, and selecting appropriate values to enter into columns M through to S.

At the moment, I can only get it to put a value in column M. If I try to get it to put values in columns M and N, I get a type mismatch error.

Here's the version that works:
Expand|Select|Wrap|Line Numbers
  1. Sub FalPNEC()
  2.  
  3. Dim pH As Double
  4. Dim DOC As Double
  5. Dim usedrows As Double
  6. Dim Row As Integer
  7.  
  8. usedrows = Worksheets("Fal").UsedRange.Rows.Count
  9.  
  10. For Row = 2 To usedrows Step 1
  11.  
  12. pH = Sheets("Fal").Cells(Row, 5)
  13. DOC = Sheets("Fal").Cells(Row, 4)
  14.  
  15. If pH < 7 And DOC <= 1 Then
  16. Sheets("Fal").Cells(Row, 13) = "=pHBDOCAConA" 'this points to a named cell in another sheet, that contains a constant
  17.  
  18. Else: Sheets("Fal").Cells(Row, 13) = ""
  19.  
  20. End If
  21.  
  22. Next Row
  23.  
  24. End Sub
  25.  
And here's where I run into trouble:
Expand|Select|Wrap|Line Numbers
  1. Sub FalPNEC()
  2.  
  3. Dim pH As Double
  4. Dim DOC As Double
  5. Dim usedrows As Double
  6. Dim Row As Integer
  7.  
  8. usedrows = Worksheets("Fal").UsedRange.Rows.Count
  9.  
  10. For Row = 2 To usedrows Step 1
  11.  
  12. pH = Sheets("Fal").Cells(Row, 5)
  13. DOC = Sheets("Fal").Cells(Row, 4)
  14.  
  15. If pH < 7 And DOC <= 1 Then
  16. Sheets("Fal").Cells(Row, 13) = "=pHBDOCAConA" And _
  17. Sheets("Fal").Cells(Row, 14) = "=pHBDOCAConB" 
  18.  
  19. Else: Sheets("Fal").Cells(Row, 13) = ""
  20.  
  21. End If
  22.  
  23. Next Row
  24.  
  25. End Sub
  26.  
Can anyone advise on what's the problem here?

Many thanks,

Lara
Jan 14 '08 #4
kadghar
1,295 Expert 1GB
Hi, I'm in trouble again.

This time, I'm trying to amend my procedure so that it will work through the spreadsheet line-by-line, checking the values in columns D and E, and selecting appropriate values to enter into columns M through to S.


Can anyone advise on what's the problem here?

Many thanks,

Lara
May be if you remove the "and_" from the 16th line

HTH
Jan 14 '08 #5
Lara1
11
HTH, many thanks for the response (I only just saw it). You were right of course :-)
Jan 24 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: LJgrnl | last post by:
I've got a type mismatch error that's driving me nutty. Variable blnNoData has the initial value False. If a recordset comes back empty (both .EOF and ..BOF are true) then blnNoData is set to...
4
by: Mike | last post by:
I am getting a type mismatch error when I do a bulk insert. ---Begin Error Msg--- Server: Msg 4864, Level 16, State 1, Line 1 Bulk insert data conversion error (type mismatch) for row 1, column...
17
by: Ange T | last post by:
Hi there, I'm having pain with the VB behind an Access form. The form is used to create reports in Excel based on the details entered in the form. This has always worked without error on my...
9
by: Edward S | last post by:
I budget for a Project in an Excel sheet as illustrated below. The months below are usually a 2 year period i.e. 24 months, though it could be over 24 months depending upon a Project. I then...
3
by: deko | last post by:
So I've decided to convert from Early Binding to Late Binding. Now that I've been baptized, I need some instruction in the faith. My former ways were thus: Dim xlapp As Excel.Application Set...
2
by: jason.teen | last post by:
Hi, I am having trouble importing a spreadsheet from Excel into an Access Database. I have noticed that even specifying the Cell Type of the Excel Data Cell, When I import it, still get...
1
by: wasishincar | last post by:
Hi all, I wrote a small app, which could read text file and fill the data from text file to a newly created excel file. It works fine on my computer, but did not succeed on someone's. I caught...
0
by: cht13er | last post by:
Good day all, I'm a newish VBA coder who was taught from Google Groups - so thanks to you all very much! I have run into a minor problem, I hope you can give me some help ... I'm running office...
1
by: House817 | last post by:
I consistently get an overflow error at the "SOxBenefits =" (source code below), while coding in a macros from within Excel, Microsoft XP. I've tried the following: 1. setting a variable equal to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.