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: -
Sub PNEC()
-
-
Dim pH As Double
-
Dim PNEC As String
-
Dim usedrows As Double
-
Dim Row As Integer
-
-
usedrows = Worksheets("Fal").UsedRange.Rows.Count
-
pH = Worksheets("Fal").Columns("E").Value 'TYPE MISMATCH ERROR HERE
-
PNEC = Worksheets("Fal").Columns("K").Value
-
-
For Row = 2 To usedrows Step 1
-
-
If pH > 5 Then
-
PNEC = "pH range C"
-
End If
-
-
Next Row
-
-
End Sub
-
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
5 9943
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: -
Sub PNEC()
-
-
Dim pH As Double
-
Dim PNEC As String
-
Dim usedrows As Double
-
Dim Row As Integer
-
-
usedrows = Worksheets("Fal").UsedRange.Rows.Count
-
pH = Worksheets("Fal").Columns("E").Value 'TYPE MISMATCH ERROR HERE
-
PNEC = Worksheets("Fal").Columns("K").Value
-
-
For Row = 2 To usedrows Step 1
-
-
If pH > 5 Then
-
PNEC = "pH range C"
-
End If
-
-
Next Row
-
-
End Sub
-
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 - Dim pH As Double
-
Dim PNEC As String
-
Dim usedrows As Double
-
Dim Row As Integer
-
-
usedrows = Sheets("Fal").UsedRange.Rows.Count
-
For Row = 2 To usedrows Step 1
-
pH = Sheets("Fal").Cells(Row, 5)
-
If pH > 5 Then
-
Sheets("Fal").Cells(Row, 11) = "pH range C"
-
Else
-
'????
-
End If
-
Next Row
??
MTB
OMG - it worked!!!!!!
MikeTheBike thankyouthankyouthankyou, I am SO grateful :-)
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: -
Sub FalPNEC()
-
-
Dim pH As Double
-
Dim DOC As Double
-
Dim usedrows As Double
-
Dim Row As Integer
-
-
usedrows = Worksheets("Fal").UsedRange.Rows.Count
-
-
For Row = 2 To usedrows Step 1
-
-
pH = Sheets("Fal").Cells(Row, 5)
-
DOC = Sheets("Fal").Cells(Row, 4)
-
-
If pH < 7 And DOC <= 1 Then
-
Sheets("Fal").Cells(Row, 13) = "=pHBDOCAConA" 'this points to a named cell in another sheet, that contains a constant
-
-
Else: Sheets("Fal").Cells(Row, 13) = ""
-
-
End If
-
-
Next Row
-
-
End Sub
-
And here's where I run into trouble: -
Sub FalPNEC()
-
-
Dim pH As Double
-
Dim DOC As Double
-
Dim usedrows As Double
-
Dim Row As Integer
-
-
usedrows = Worksheets("Fal").UsedRange.Rows.Count
-
-
For Row = 2 To usedrows Step 1
-
-
pH = Sheets("Fal").Cells(Row, 5)
-
DOC = Sheets("Fal").Cells(Row, 4)
-
-
If pH < 7 And DOC <= 1 Then
-
Sheets("Fal").Cells(Row, 13) = "=pHBDOCAConA" And _
-
Sheets("Fal").Cells(Row, 14) = "=pHBDOCAConB"
-
-
Else: Sheets("Fal").Cells(Row, 13) = ""
-
-
End If
-
-
Next Row
-
-
End Sub
-
Can anyone advise on what's the problem here?
Many thanks,
Lara
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
HTH, many thanks for the response (I only just saw it). You were right of course :-)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
| |