PC Review


Reply
Thread Tools Rate Thread

change cellvalue based on value in adjacent cell

 
 
Sane
Guest
Posts: n/a
 
      16th Dec 2009
In my account data i have amount in column h and Debit, Credit (DR/
CR) in next column. To modify the values in column h the code is
........
For Each c In Range("H2:h" & t)
If c.Offset(0, 1) = "DR" Then
c.Value = c
Else
c.Value = -c
End If
Next
........
This part of macro takes abnormally high time. Please suggest any
alternative to make this faster.
 
Reply With Quote
 
 
 
 
Sam Wilson
Guest
Posts: n/a
 
      16th Dec 2009

How many rows?

One quick thing - you don't need both if conditions. The first one does
nothing. Try this instead:

for each c in range("H2:h" & t)
if c.offset(0,1).value = "CR" then c.value = -c.value
next c


Sam

"Sane" wrote:

> In my account data i have amount in column h and Debit, Credit (DR/
> CR) in next column. To modify the values in column h the code is
> ........
> For Each c In Range("H2:h" & t)
> If c.Offset(0, 1) = "DR" Then
> c.Value = c
> Else
> c.Value = -c
> End If
> Next
> ........
> This part of macro takes abnormally high time. Please suggest any
> alternative to make this faster.
> .
>

 
Reply With Quote
 
Sam Wilson
Guest
Posts: n/a
 
      16th Dec 2009
Another thing that may help, put this at the start of the macro:

application.Calculation = xlCalculationManual

and this at the end:
application.Calculation = xlCalculationAutomatic

"Sane" wrote:

> In my account data i have amount in column h and Debit, Credit (DR/
> CR) in next column. To modify the values in column h the code is
> ........
> For Each c In Range("H2:h" & t)
> If c.Offset(0, 1) = "DR" Then
> c.Value = c
> Else
> c.Value = -c
> End If
> Next
> ........
> This part of macro takes abnormally high time. Please suggest any
> alternative to make this faster.
> .
>

 
Reply With Quote
 
sali
Guest
Posts: n/a
 
      16th Dec 2009
"Sane" <(E-Mail Removed)> je napisao u poruci interesnoj
grupi:f9a2f0b1-ceb8-4956-a35d-(E-Mail Removed)...
> In my account data i have amount in column h and Debit, Credit (DR/
> CR) in next column. To modify the values in column h the code is
> .......
> For Each c In Range("H2:h" & t)
> If c.Offset(0, 1) = "DR" Then
> c.Value = c
> Else
> c.Value = -c
> End If
> Next


it is not a good idea to use "c.value=-c", because it just *inverts* the
value, called [mistakenly] twice, you have reverted to original value.
better is to have *new* calculted column [f.e at offset=2], like:

If c.Offset(0, 1) = "DR" Then
c.offset(0,2).Value = c
Else
c.offset(0,2).Value = -c
End If

so your original column "h" is preserved, and multiple execution doesn't
harm

and how many rows [t] you have, and what is meaning "long execution" for
you, 5 seconds, or more?
when lacking memory, excel becomes fragmented, and sudenly execution slows
down very much. can you trace the memory usage at task-manager?


 
Reply With Quote
 
Sane
Guest
Posts: n/a
 
      16th Dec 2009
On Dec 16, 4:55*pm, Sam Wilson <SamWil...@discussions.microsoft.com>
wrote:
> Another thing that may help, put this at the start of the macro:
>
> application.Calculation = xlCalculationManual
>
> and this at the end:
> application.Calculation = xlCalculationAutomatic
>
>
>
> "Sane" wrote:
> > In my account data i have amount in column h and Debit, Credit (DR/
> > CR) *in next column. To modify the values in column h the code is
> > ........
> > For Each c In Range("H2:h" & t)
> > * * If c.Offset(0, 1) = "DR" Then
> > * * c.Value = c
> > * * Else
> > * * c.Value = -c
> > * * End If
> > * * Next
> > ........
> > This part of macro takes abnormally high time. Please suggest any
> > alternative to make this faster.
> > .- Hide quoted text -

>
> - Show quoted text -


Thanks sam. both put together worked great.
 
Reply With Quote
 
fisch4bill
Guest
Posts: n/a
 
      16th Dec 2009
In addition to Sam's suggestion, You might try adding the following line
before any execution lines:

Application.ScreenUpdating = False

This will prevent Excel from "re-drawing" the screen each time it evaluates
a cell. The manual calculation toggle should do just about the same thing. If
you're in fact having memory issues, using both frees up quite a bit of
resources especially if you're evaluating many cells.


"Sane" wrote:

> In my account data i have amount in column h and Debit, Credit (DR/
> CR) in next column. To modify the values in column h the code is
> ........
> For Each c In Range("H2:h" & t)
> If c.Offset(0, 1) = "DR" Then
> c.Value = c
> Else
> c.Value = -c
> End If
> Next
> ........
> This part of macro takes abnormally high time. Please suggest any
> alternative to make this faster.
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Force entry into cell, based on validation selection in adjacent cell Richhall Microsoft Excel Worksheet Functions 3 18th Jun 2009 10:28 AM
How to populate a cell with numeric value based on textselected from pull down in adjacent cell? Garth Rodericks Microsoft Excel Worksheet Functions 1 5th Sep 2008 02:03 AM
Excel automation issue---enter date into cell based on other cellvalue HHROhminn@gmail.com Microsoft Excel Programming 0 15th Jan 2008 09:28 PM
change current cell colour based on the value of adjacent cell on other worksheet Rits Microsoft Excel Programming 2 23rd Nov 2006 11:57 AM
Counting cell values based on adjacent cell value over multiple columns h2oskier Microsoft Excel Worksheet Functions 3 19th Feb 2004 05:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:37 PM.