Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Type mismatch while running If statement

I've got Type Mismatch error message while I'm running the following code
(It happens half way through, so I think the code can work ok, but I don't know why it stops right in the middle!!!!
How can I improve the code and solve the problem? Pls Help!!! Thanks!!!

Function Copy(ByVal RowNum As String
Sheets("Region").Selec
C1 =
For S2 = 2 To 5000
If Workbooks("Test.xls").Worksheets("Region").Cells(S 2, 1) = "" Then Exit For
If Right(Workbooks("Test.xls").Worksheets("Region").C ells(S2, 3), 3) _ <----- It stop right here
= Workbooks("Test.xls").Worksheets("Macro").Cells(Ro wNum, 4)
And Right(Workbooks("Test.xls").Worksheets("Region").C ells(S2, 3), 3)
< "South" The

For S3 = 1 To 3
Worksheets("Region").Cells(C1, S3)
= Workbooks("Test.xls").Worksheets("Region").Cells(S 2, S3
Next S
C1 = C1 +
ElseIf Left(Workbooks("Test.xls").Worksheets("Region").Ce lls(S2, 3), 4)
= Workbooks("Test.xls").Worksheets("Macro").Cells(Ro wNum, 5) The

For S3 = 1 To 3
Worksheets("Region").Cells(C1, S3)
= Workbooks("Test.xls").Worksheets("Region").Cells(S 2, S3
Next S
C1 = C1 +
End I
Next S
End Functio

  #2   Report Post  
Posted to microsoft.public.excel.programming
RWN RWN is offline
external usenet poster
 
Posts: 104
Default Type mismatch while running If statement

Not an expert, but you'll probably get a type mismatch with using the
"RowNum" as a subscript. You have defined it as a string (vs. integer)

HTH

--
Regards;
Rob
------------------------------------------------------------------------
"Stel" wrote in message
...
I've got Type Mismatch error message while I'm running the following

code:
(It happens half way through, so I think the code can work ok, but I

don't know why it stops right in the middle!!!!)
How can I improve the code and solve the problem? Pls Help!!!

Thanks!!!!

Function Copy(ByVal RowNum As String)
Sheets("Region").Select
C1 = 2
For S2 = 2 To 50000
If Workbooks("Test.xls").Worksheets("Region").Cells(S 2, 1) =

"" Then Exit For
If

Right(Workbooks("Test.xls").Worksheets("Region").C ells(S2, 3), 3) _
<----- It stop right here
=

Workbooks("Test.xls").Worksheets("Macro").Cells(Ro wNum, 4) _
And

Right(Workbooks("Test.xls").Worksheets("Region").C ells(S2, 3), 3) _
< "South" Then

For S3 = 1 To 30
Worksheets("Region").Cells(C1, S3) _
=

Workbooks("Test.xls").Worksheets("Region").Cells(S 2, S3)
Next S3
C1 = C1 + 1
ElseIf

Left(Workbooks("Test.xls").Worksheets("Region").Ce lls(S2, 3), 4) _
=

Workbooks("Test.xls").Worksheets("Macro").Cells(Ro wNum, 5) Then

For S3 = 1 To 30
Worksheets("Region").Cells(C1, S3) _
=

Workbooks("Test.xls").Worksheets("Region").Cells(S 2, S3)
Next S3
C1 = C1 + 1
End If
Next S2
End Function



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Type mismatch while running If statement

But I don't think it matters, because it works on the other code.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Type mismatch while running If statement

Hello,
I'm afraid I didn't understood your function completely, so What I
wrote may wide of the mark...
But here are some suggestions.

1.As RWN posted, a variable RowNum should be declared as a Long type.
If RowNum need to a string type, you can use it in the code as
Clng(RowNum)
2.Add the word "Option Explicit" in the very top line of your module.
3.Don't you need a result for this UDF?
4.You can use Worksheet type variables and it makes your code short.
5.The word "South" is 5 characters so the formula

Code:
--------------------

Right(Workbooks("Test.xls").Worksheets("Region").C ells(S2, 3), 3)

--------------------

would be

Code:
--------------------

Right(Workbooks("Test.xls").Worksheets("Region").C ells(S2, 3), 5)

--------------------



The code would be something like this.
Plece BREAK POINT in VBE, run your code Step by step with pressing [F8]
key.



Code:
--------------------


Option Explicit

Function UDFCopy(ByVal RowNum As Long) As Boolean
Sheets("Region").Select
Dim C1 As Long, S2 As Long, S3 As Long
Dim wsReg As Worksheet, wsMcr As Worksheet
On Error GoTo Terminate
Set wsReg = Workbooks("Test.xls").Worksheets("Region")
Set wsMcr = Workbooks("Test.xls").Worksheets("Macro")
C1 = 2
For S2 = 2 To 50000
If wsReg.Cells(S2, 1) = "" Then Exit For
If Right(wsReg.Cells(S2, 3), 3) = wsMcr.Cells(RowNum, 4) _
And Right(wsReg.Cells(S2, 3), 5) < "South" Then
For S3 = 1 To 30
wsReg.Cells(C1, S3) = wsReg.Cells(S2, S3)
Next S3
C1 = C1 + 1
ElseIf Left(wsReg.Cells(S2, 3), 4) = wsMcr.Cells(RowNum, 5) Then
For S3 = 1 To 30
wsReg.Cells(C1, S3) = wsReg.Cells(S2, S3)
Next S3
C1 = C1 + 1
End If
Next S2
UDFCopy = True
Exit Sub
Terminate:
UDFCopy = False
End Function

--------------------



---
Message posted from http://www.ExcelForum.com/

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default Type mismatch while running If statement

It would be easier to read & debug if you set worksheet
object to the sheets etc...


Function Copy(ByVal RowNum As Long)
Dim WBTest As Workbook
Dim WSRegion As Worksheet
Dim WSMacro As Worksheet

Set WBTest = Workbooks("Test.xls")
Set WSRegion = _
Workbooks("Test.xls").Worksheets("Region")
Set WSMacro = WBTest.Worksheets("Macro")


C1 = 2
For S2 = 2 To 50000
With WSR
If .Cells(S2, 1) = "" Then Exit For
If Right(.Cells(S2, 3), 3) _
= WSMacro.Cells(RowNum, 4) _
And Right(.Cells(S2, 3), 3) _
< "South" Then

For S3 = 1 To 30
.Cells(C1, S3) = .Cells(S2, S3)
Next S3
C1 = C1 + 1
ElseIf Left(.Cells(S2, 3), 4) _
= WSMacro.Cells(RowNum, 5) Then

For S3 = 1 To 30
.Cells(C1, S3) _
= .Cells(S2, S3)
Next S3
C1 = C1 + 1
End If
Next S2
End Function



Try this.
By the way otice that I changed the parameter to Long
In the code where you test the value of a string, VBA
will coerce the value. eg
If "123" 100 then
This is a text value ("123") which VB coerces to a number
123 then tests againt 100.
This isn't an error. However if the value was "A" then an
error would occur. By passing the parameter as long, you
enforce the rule and the coercion cannot happen as the
variable is now a number by definition.





-----Original Message-----
I've got Type Mismatch error message while I'm running

the following code:
(It happens half way through, so I think the code can

work ok, but I don't know why it stops right in the
middle!!!!)
How can I improve the code and solve the problem? Pls

Help!!! Thanks!!!!

Function Copy(ByVal RowNum As String)
Sheets("Region").Select
C1 = 2
For S2 = 2 To 50000
If Workbooks("Test.xls").Worksheets

("Region").Cells(S2, 1) = "" Then Exit For
If Right(Workbooks("Test.xls").Worksheets

("Region").Cells(S2, 3), 3) _ <----- It stop right here
= Workbooks("Test.xls").Worksheets

("Macro").Cells(RowNum, 4) _
And Right(Workbooks("Test.xls").Worksheets

("Region").Cells(S2, 3), 3) _
< "South" Then

For S3 = 1 To 30
Worksheets("Region").Cells(C1, S3) _
= Workbooks

("Test.xls").Worksheets("Region").Cells(S2, S3)
Next S3
C1 = C1 + 1
ElseIf Left(Workbooks("Test.xls").Worksheets

("Region").Cells(S2, 3), 4) _
= Workbooks("Test.xls").Worksheets

("Macro").Cells(RowNum, 5) Then

For S3 = 1 To 30
Worksheets("Region").Cells(C1, S3) _
= Workbooks

("Test.xls").Worksheets("Region").Cells(S2, S3)
Next S3
C1 = C1 + 1
End If
Next S2
End Function

.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Type Mismatch Error Minitman Excel Discussion (Misc queries) 7 June 21st 08 02:15 AM
Type mismatch... George[_4_] Excel Discussion (Misc queries) 7 December 19th 07 12:20 PM
Type Mismatch [email protected] Excel Worksheet Functions 1 May 16th 07 03:29 PM
type mismatch--how to fix rroach Excel Discussion (Misc queries) 2 July 14th 05 06:23 PM


All times are GMT +1. The time now is 01:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"