Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Old January 5th 18, 01:43 AM posted to microsoft.public.excel.worksheet.functions
tb tb is offline
external usenet poster
 
First recorded activity by ExcelBanter: Jul 2011
Posts: 75
Default Excel Macro: Type mismatch (Error 13)

I am using Microsoft Excel 2010.

Several years ago, Claus Busch wrote the following macro for me:

Sub roundSelection()
Dim pctChange As Double
Dim Cell As Range
pctChange = Range("pctChange")
For Each Cell In Selection
Cell = WorksheetFunction.Round(Cell * (1 + pctChange), 2)
Next Cell
End Sub

When I try to run it now, I get "Run-time error '13': Type mismatch".
The problem seems to be this line of code:

Cell = WorksheetFunction.Round(Cell * (1 + pctChange), 2)

....but I don't know how to fix it as I am no expert with macros!

The macro should work like this: If one of the worksheet tabs has a
box named "pctChange" and a % value underneath that box, it should
apply the % value to all the worksheet boxes in that tab that are
formatted as currency (i.e. $ - U.S. Dollars in my case).

I should be able to specify a different "pctChange" value for each
worksheet tab and then run the macro.

Can anybody please help? I'm willing to post the spreadsheet
somewhere, if that helps.

Thanks.
--
tb

  #2   Report Post  
Old January 5th 18, 02:00 AM posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
First recorded activity by ExcelBanter: Apr 2015
Posts: 959
Default Excel Macro: Type mismatch (Error 13)

I am using Microsoft Excel 2010.

Several years ago, Claus Busch wrote the following macro for me:

Sub roundSelection()
Dim pctChange As Double
Dim Cell As Range
pctChange = Range("pctChange")
For Each Cell In Selection
Cell = WorksheetFunction.Round(Cell * (1 + pctChange), 2)
Next Cell
End Sub

When I try to run it now, I get "Run-time error '13': Type mismatch".
The problem seems to be this line of code:

Cell = WorksheetFunction.Round(Cell * (1 + pctChange), 2)

...but I don't know how to fix it as I am no expert with macros!

The macro should work like this: If one of the worksheet tabs has a
box named "pctChange" and a % value underneath that box, it should
apply the % value to all the worksheet boxes in that tab that are
formatted as currency (i.e. $ - U.S. Dollars in my case).

I should be able to specify a different "pctChange" value for each
worksheet tab and then run the macro.

Can anybody please help? I'm willing to post the spreadsheet
somewhere, if that helps.

Thanks.


Are any of the selected cells non-numeric?

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #3   Report Post  
Old January 5th 18, 02:14 AM posted to microsoft.public.excel.worksheet.functions
tb tb is offline
external usenet poster
 
First recorded activity by ExcelBanter: Jul 2011
Posts: 75
Default Excel Macro: Type mismatch (Error 13)

On 1/4/2018 at 7:00:30 PM GS wrote:

I am using Microsoft Excel 2010.

Several years ago, Claus Busch wrote the following macro for me:

Sub roundSelection()
Dim pctChange As Double
Dim Cell As Range
pctChange = Range("pctChange")
For Each Cell In Selection
Cell = WorksheetFunction.Round(Cell * (1 + pctChange), 2)
Next Cell
End Sub

When I try to run it now, I get "Run-time error '13': Type
mismatch". The problem seems to be this line of code:

Cell = WorksheetFunction.Round(Cell * (1 + pctChange), 2)

...but I don't know how to fix it as I am no expert with macros!

The macro should work like this: If one of the worksheet tabs has a
box named "pctChange" and a % value underneath that box, it should
apply the % value to all the worksheet boxes in that tab that are
formatted as currency (i.e. $ - U.S. Dollars in my case).

I should be able to specify a different "pctChange" value for each
worksheet tab and then run the macro.

Can anybody please help? I'm willing to post the spreadsheet
somewhere, if that helps.

Thanks.


Are any of the selected cells non-numeric?


The way I remember how to run the macro (it has been many years!!), I
did not have to select any cells. The macro would look for cells that
are formatted as currency (like U.S. Dollar) and apply the value
specified by the "pctChange" variable.

It would be impractical for me to select all cells in a tab that are
formatted as currency as I have such values spread out everywhere.

Perhaps the macro, as shown above, doesn't do what I am saying it
should do. Several years have passed since the last time I ran it...
If so, would it be possible for somebody to write a macro the way I
want it now?
--
tb
  #4   Report Post  
Old January 5th 18, 06:11 AM posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
First recorded activity by ExcelBanter: Apr 2015
Posts: 959
Default Excel Macro: Type mismatch (Error 13)

On 1/4/2018 at 7:00:30 PM GS wrote:

I am using Microsoft Excel 2010.

Several years ago, Claus Busch wrote the following macro for me:

Sub roundSelection()
Dim pctChange As Double
Dim Cell As Range
pctChange = Range("pctChange")
For Each Cell In Selection
Cell = WorksheetFunction.Round(Cell * (1 + pctChange), 2)
Next Cell
End Sub

When I try to run it now, I get "Run-time error '13': Type
mismatch". The problem seems to be this line of code:

Cell = WorksheetFunction.Round(Cell * (1 + pctChange), 2)

...but I don't know how to fix it as I am no expert with macros!

The macro should work like this: If one of the worksheet tabs has a
box named "pctChange" and a % value underneath that box, it should
apply the % value to all the worksheet boxes in that tab that are
formatted as currency (i.e. $ - U.S. Dollars in my case).

I should be able to specify a different "pctChange" value for each
worksheet tab and then run the macro.

Can anybody please help? I'm willing to post the spreadsheet
somewhere, if that helps.

Thanks.


Are any of the selected cells non-numeric?


The way I remember how to run the macro (it has been many years!!), I
did not have to select any cells. The macro would look for cells that
are formatted as currency (like U.S. Dollar) and apply the value
specified by the "pctChange" variable.

It would be impractical for me to select all cells in a tab that are
formatted as currency as I have such values spread out everywhere.

Perhaps the macro, as shown above, doesn't do what I am saying it
should do. Several years have passed since the last time I ran it...
If so, would it be possible for somebody to write a macro the way I
want it now?


Since worksheets don't have any boxes (only cells), you'd be better served to
post a download link to your file...

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #5   Report Post  
Old January 5th 18, 03:13 PM posted to microsoft.public.excel.worksheet.functions
tb tb is offline
external usenet poster
 
First recorded activity by ExcelBanter: Jul 2011
Posts: 75
Default Excel Macro: Type mismatch (Error 13)

On 1/4/2018 at 11:11:55 PM GS wrote:

On 1/4/2018 at 7:00:30 PM GS wrote:

I am using Microsoft Excel 2010.

Several years ago, Claus Busch wrote the following macro for me:

Sub roundSelection()
Dim pctChange As Double
Dim Cell As Range
pctChange = Range("pctChange")
For Each Cell In Selection
Cell = WorksheetFunction.Round(Cell * (1 + pctChange), 2)
Next Cell
End Sub

When I try to run it now, I get "Run-time error '13': Type
mismatch". The problem seems to be this line of code:

Cell = WorksheetFunction.Round(Cell * (1 + pctChange), 2)

...but I don't know how to fix it as I am no expert with macros!

The macro should work like this: If one of the worksheet tabs
has a box named "pctChange" and a % value underneath that box,
it should apply the % value to all the worksheet boxes in that
tab that are formatted as currency (i.e. $ - U.S. Dollars in my
case).

I should be able to specify a different "pctChange" value for
each worksheet tab and then run the macro.

Can anybody please help? I'm willing to post the spreadsheet
somewhere, if that helps.

Thanks.

Are any of the selected cells non-numeric?


The way I remember how to run the macro (it has been many years!!),
I did not have to select any cells. The macro would look for cells
that are formatted as currency (like U.S. Dollar) and apply the
value specified by the "pctChange" variable.

It would be impractical for me to select all cells in a tab that are
formatted as currency as I have such values spread out everywhere.

Perhaps the macro, as shown above, doesn't do what I am saying it
should do. Several years have passed since the last time I ran
it... If so, would it be possible for somebody to write a macro
the way I want it now?


Since worksheets don't have any boxes (only cells), you'd be better
served to post a download link to your file...


Yes, I'll be glad to.
I posted file "test.xlsm" to https://ufile.io/n9ibi

In each worksheet of the Excel workbook you will see two cells colored
in red. One is called "pctChange" and the other one has a % value.

For instance, in worksheet "Sorted", the two boxes are located in cells
M1 and M2. In worksheet "Cyl (BSP)" they are located in cells O1 and
O2, and so on and so on.

I have inserted some test values in each "pctChange" cells. Some are
negative, some positive, some are 0%. That should allow for testing
all the possibilities.

What I am looking for is a macro that parses each worksheet
_separately_ and for each cell that is formatted as currency ($ - U.S.
Dollars) applies the "pctChange" value specified in that worksheet.

I hope that it is possible to create a macro like that...

Thanks.
--
tb


  #6   Report Post  
Old January 6th 18, 12:00 AM posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
First recorded activity by ExcelBanter: Apr 2015
Posts: 959
Default Excel Macro: Type mismatch (Error 13)

For starters, the named range "pctChange" is global (workbook scope) meaning
its definition always refers to 'Cyl(BSP)'!$O$2. This name will need to be
redifined to sheet level scope for each sheet it is used on for code to work.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #7   Report Post  
Old January 6th 18, 06:15 AM posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
First recorded activity by ExcelBanter: May 2010
Posts: 226
Default Excel Macro: Type mismatch (Error 13)

After you have defined "pctChange" with local scope to each sheet the name is
used on, follow these steps to complete your project.

Step1
Make this change in your Tools==Options dialog:

Place a check in "Require Variable Declaration"


Step2
Replace everything in your code module with...

Option Explicit

Const sCurrencyFormat$ = "$#,##0.00_);($#,##0.00)"

Sub RoundCurrencyValues()
Dim wks As Worksheet, rng As Range, crng
On Error Resume Next 'in case no Range("pctChange")
Set rng = ActiveSheet.Range("pctChange")
If Not rng Is Nothing Then
For Each crng In wks.UsedRange.Cells
If crng.NumberFormat = sCurrencyFormat Then _
crng.Value = WorksheetFunction.Round(crng * (1 + rng), 2)
Next 'crng
End If 'Not rng Is Nothing
Set rng = Nothing
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #8   Report Post  
Old January 6th 18, 02:56 PM posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
First recorded activity by ExcelBanter: Apr 2015
Posts: 959
Default Excel Macro: Type mismatch (Error 13)

Oops.., didn't fully cleanup after testing! The working code follows...

Sub RoundCurrencyValues()
Dim rng As Range, crng
On Error Resume Next 'in case no Range("pctChange")
Set rng = ActiveSheet.Range("pctChange")
If Not rng Is Nothing Then
For Each crng In ActiveSheet.UsedRange.Cells
If crng.NumberFormat = sCurrencyFormat Then crng.Value =
WorksheetFunction.Round(crng * (1 + rng), 2)
Next 'crng
End If 'Not rng Is Nothing
Set rng = Nothing
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #9   Report Post  
Old January 6th 18, 07:25 PM posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
First recorded activity by ExcelBanter: Apr 2011
Posts: 3,625
Default Excel Macro: Type mismatch (Error 13)

Hi,

Am Fri, 5 Jan 2018 14:13:57 +0000 (UTC) schrieb tb:

What I am looking for is a macro that parses each worksheet
_separately_ and for each cell that is formatted as currency ($ - U.S.
Dollars) applies the "pctChange" value specified in that worksheet.


try:

Sub Test()
Dim wsh As Worksheet
Dim rngc As Range, c As Range
Dim factor As Double
Dim strFormat As String

strFormat = """$""#,##0.00_);(""$""#,##0.00)"

For Each wsh In Worksheets
With wsh
Set c = .UsedRange.Find("pctChange", LookIn:=xlValues)
If Not c Is Nothing Then factor = 1 + c.Offset(1, 0)
If c Is Nothing Or factor = 1 Then GoTo Skip
For Each rngc In .UsedRange
If rngc.NumberFormat = strFormat And IsNumeric(rngc) Then
rngc = Round(rngc * factor, 2)
End If
Next
End With
Skip:
Next
End Sub


Regards
Claus B.
--
Windows10
Office 2016
  #10   Report Post  
Old January 8th 18, 07:11 PM posted to microsoft.public.excel.worksheet.functions
tb tb is offline
external usenet poster
 
First recorded activity by ExcelBanter: Jul 2011
Posts: 75
Default Excel Macro: Type mismatch (Error 13)

On 1/6/2018 at 7:56:51 AM GS wrote:

Oops.., didn't fully cleanup after testing! The working code
follows...

Sub RoundCurrencyValues()
Dim rng As Range, crng
On Error Resume Next 'in case no Range("pctChange")
Set rng = ActiveSheet.Range("pctChange")
If Not rng Is Nothing Then
For Each crng In ActiveSheet.UsedRange.Cells
If crng.NumberFormat = sCurrencyFormat Then crng.Value =
WorksheetFunction.Round(crng * (1 + rng), 2) Next 'crng
End If 'Not rng Is Nothing
Set rng = Nothing
End Sub


Thanks I will try the code out. I'm having problems understanding how
to declare "pctChange" with local scope. I've never created a macro
myself and cannot find an example of such declaration with Google.

The best example that I could find about scope declaration is this:
<http://www.cpearson.com/excel/scope.aspx
but it does not seem to address the local scope...

--
tb


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
runtime error 13 - type mismatch error in Excel 97 on Citrix Kevin Maher Excel Programming 7 March 8th 08 12:48 PM
Error type mismatch in writing macro ub Excel Worksheet Functions 2 September 26th 07 12:48 PM
Macro error type mismatch Jurassien Excel Discussion (Misc queries) 3 February 23rd 07 09:14 PM
type mismatch error in Benford's law macro Sody Excel Programming 1 May 24th 06 11:07 PM
Visual Basic macro run time error(13) type mismatch Paul Excel Discussion (Misc queries) 0 October 25th 05 07:28 AM


All times are GMT +1. The time now is 11:13 AM.

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

About Us

"It's about Microsoft Excel"

 

Copyright © 2017