Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 60
Default range division

I am programming in VB. I want to divide a range of cells by one number, i.e.

the code i have is:

Range("C16:V16") = (Sheets(gstrCMCMCost).Range("B4:U4") / 1000)

This produces an error - A Type mismatch error.

This way works when I do one cell at a time. However I need to be able to do
all ranges at once. Is this possible?

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default range division

Hi Minique,

How about this:

With Range("C16:V16")
.Value = Sheets(gstrCMCMCost).Range("B4:U4")
With Cells(65536,256)
.Value=1000
.Copy
End With
.PasteSpecial Paste:=xlValues, Operation:=xlDivide
End With
Cells(65536,256).ClearContents
Regard,
KL


"Monique" wrote in message
...
I am programming in VB. I want to divide a range of cells by one number,
i.e.

the code i have is:

Range("C16:V16") = (Sheets(gstrCMCMCost).Range("B4:U4") / 1000)

This produces an error - A Type mismatch error.

This way works when I do one cell at a time. However I need to be able to
do
all ranges at once. Is this possible?

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default range division

Hi,

The error is occuring because the code is trying to divide a range object by
1000 and not the values in that range. Using a single cell is OK because VBA
automatically uses the value when a single cell range is used.

If you want to use VBA here you would have to loop through each of the
values of the range. The following code works but not sure if it's THE best
method.

Dim rngStart As Range

Set rngStart = Range("C16:V16")


For Each rng In rngStart

rng.Offset(-12, 0) = (rng.Value) / 1000

Next rng

HTH,
Matt

"Monique" wrote:

I am programming in VB. I want to divide a range of cells by one number, i.e.

the code i have is:

Range("C16:V16") = (Sheets(gstrCMCMCost).Range("B4:U4") / 1000)

This produces an error - A Type mismatch error.

This way works when I do one cell at a time. However I need to be able to do
all ranges at once. Is this possible?

Thanks

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 60
Default range division

How would i go about creating a function that could do this for any range?

"KL" wrote:

Hi Minique,

How about this:

With Range("C16:V16")
.Value = Sheets(gstrCMCMCost).Range("B4:U4")
With Cells(65536,256)
.Value=1000
.Copy
End With
.PasteSpecial Paste:=xlValues, Operation:=xlDivide
End With
Cells(65536,256).ClearContents
Regard,
KL


"Monique" wrote in message
...
I am programming in VB. I want to divide a range of cells by one number,
i.e.

the code i have is:

Range("C16:V16") = (Sheets(gstrCMCMCost).Range("B4:U4") / 1000)

This produces an error - A Type mismatch error.

This way works when I do one cell at a time. However I need to be able to
do
all ranges at once. Is this possible?

Thanks




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default range division

Function SetValue(rng As Range, num)
With rng
.Value = num
With Range("IV65536")
.Value=1000
.Copy
End With
.PasteSpecial Paste:=xlValues, Operation:=xlDivide
Range("IV65536").ClearContents
End With
End Function

and call with

SetValue Range("C16:V16"), Sheets(gstrCMCMCost).Range("B4:U4")

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Monique" wrote in message
...
How would i go about creating a function that could do this for any range?

"KL" wrote:

Hi Minique,

How about this:

With Range("C16:V16")
.Value = Sheets(gstrCMCMCost).Range("B4:U4")
With Cells(65536,256)
.Value=1000
.Copy
End With
.PasteSpecial Paste:=xlValues, Operation:=xlDivide
End With
Cells(65536,256).ClearContents
Regard,
KL


"Monique" wrote in message
...
I am programming in VB. I want to divide a range of cells by one

number,
i.e.

the code i have is:

Range("C16:V16") = (Sheets(gstrCMCMCost).Range("B4:U4") / 1000)

This produces an error - A Type mismatch error.

This way works when I do one cell at a time. However I need to be able

to
do
all ranges at once. Is this possible?

Thanks








  #6   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default range division

Another option (much slower on large ranges, but OK for the ones in the
example):

Function CopyArray(rngCopy As Range, _
cellDest As Range, Divider As Double)
myarr = rngCopy.Value
For i = 1 To UBound(myarr, 1)
For j = 1 To UBound(myarr, 2)
myarr(i, j) = myarr(i, j) / Divider
Next j
Next i
cellDest.Resize(UBound(myarr, 1), _
UBound(myarr, 2)).Value = myarr
End Function

'And call it like this:

Sub test()
CopyArray _
Sheets("Sheet1").Range("B4:U4"), _
Sheets("Sheet2").Range("C16"), _
1000
End Sub

Regards,
KL


"Bob Phillips" wrote in message
...
Function SetValue(rng As Range, num)
With rng
.Value = num
With Range("IV65536")
.Value=1000
.Copy
End With
.PasteSpecial Paste:=xlValues, Operation:=xlDivide
Range("IV65536").ClearContents
End With
End Function

and call with

SetValue Range("C16:V16"), Sheets(gstrCMCMCost).Range("B4:U4")

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Monique" wrote in message
...
How would i go about creating a function that could do this for any
range?

"KL" wrote:

Hi Minique,

How about this:

With Range("C16:V16")
.Value = Sheets(gstrCMCMCost).Range("B4:U4")
With Cells(65536,256)
.Value=1000
.Copy
End With
.PasteSpecial Paste:=xlValues, Operation:=xlDivide
End With
Cells(65536,256).ClearContents
Regard,
KL


"Monique" wrote in message
...
I am programming in VB. I want to divide a range of cells by one

number,
i.e.

the code i have is:

Range("C16:V16") = (Sheets(gstrCMCMCost).Range("B4:U4") / 1000)

This produces an error - A Type mismatch error.

This way works when I do one cell at a time. However I need to be
able

to
do
all ranges at once. Is this possible?

Thanks








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
Division EK Excel Worksheet Functions 2 May 3rd 07 04:05 PM
Need help with division, please!!! swag143 Excel Worksheet Functions 1 August 16th 06 05:01 PM
Division David New Users to Excel 5 July 26th 06 11:06 AM
Range of cells and division formula tdg119 Excel Discussion (Misc queries) 3 February 23rd 06 06:33 PM
Division help? clayblay Excel Discussion (Misc queries) 6 January 13th 06 02:51 PM


All times are GMT +1. The time now is 10:26 AM.

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

About Us

"It's about Microsoft Excel"