Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > Increment Formula With If Statement


Reply
 
Thread Tools Display Modes
  #1  
Old 08-07-2012, 06:28 AM
Spaggiari Spaggiari is offline
Newcomer
 
Join Date: Aug 2012
Posts: 9
Default Increment Formula With If Statement


Hey everyone, I'd like to know, if its possible, how to increment a formula with an If statement that transforms 2 values in time format and then as long as these two values keep beeing added in the cells the formula keeps active in the column.

I tried to apply the code for all the but this makes my file huge like 20MB!

You can check this on the file, go the "records" sheet, add the time in the "shift start" and "shift end" fields, then the difference value goes to "hours" in the sheet.

https://www.dropbox.com/s/ouda3vdcu0buefh/project.xlsm

Thx in advance!
Reply With Quote
  #2  
Old 08-09-2012, 04:15 AM
Spaggiari Spaggiari is offline
Newcomer
 
Join Date: Aug 2012
Posts: 9
Default

Anyone?
Reply With Quote
  #3  
Old 08-09-2012, 10:03 AM
Cerian Knight's Avatar
Cerian Knight Cerian Knight is offline
Multi-Technologist

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 3,740
Default

Do you mean VBA code to change a cell formula? It is not entirely clear from your description exactly what you are trying to do. Also, the dropbox link is not working. Post some example VBA code and any relevant cell formulas you have tried here.
__________________
"May the code that you write never work in ways that you didn't expect; and may the code that you didn't write never require you to maintain it". - Ancient Chinese Proverb
Reply With Quote
  #4  
Old 08-10-2012, 03:06 AM
Spaggiari Spaggiari is offline
Newcomer
 
Join Date: Aug 2012
Posts: 9
Default

Quote:
Originally Posted by Cerian Knight View Post
Do you mean VBA code to change a cell formula? It is not entirely clear from your description exactly what you are trying to do. Also, the dropbox link is not working. Post some example VBA code and any relevant cell formulas you have tried here.
The formula i'm using is this one:

=IF(R10<>"";MOD(R10-Q10;1);"")

If you open the file below you'll understand, just go to the records sheet and you'll see. I don't want to apply the formula to the whole column, I just want to add the formula to one more cell if there is a value start time and end time.

Thx in advance

This new link is working > https://www.dropbox.com/s/qb2m9a7a6at7e68/project.xlsm
Reply With Quote
  #5  
Old 08-10-2012, 09:38 AM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,883
Default

Spaggiari,

1) Not everyone can open an xlsm file. Many have earlier versions of Excel.
This is why CK asked you to post the code you are using as text.

2) You still did not answer Cerian Knight's VBA code question.

If you want to run a program on static data then you do not need to put a formula in the cells. VBA can do the calculations and put a hard value in the cells.
Things will change only when you run the program and the resullting file should be much smaller.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #6  
Old 08-10-2012, 12:14 PM
Cerian Knight's Avatar
Cerian Knight Cerian Knight is offline
Multi-Technologist

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 3,740
Default

I did finally manage to convert the xlsm to look at it.

I think you need to enumerate by UsedRange. Something like this (not fully tested):
Code:
Dim I As Long
For I = 6 To Worksheets("records").UsedRange.Rows.count
  If Cells(I, 18).Value - Cells(I, 17).Value > 0 Then
    'by formula:
    Cells(I, 2).Formula = "=IF(R" & CStr(I) & "<>"""",MOD(R" & CStr(I) & "-Q" & CStr(I) & ",1),"""")"
    'or by value
    Cells(I,2).Value = Cells(I, 18).Value - Cells(I, 17).Value
  Else
    'by formula
    Cells(I, 2).Formula = ""
    'or by value
    Cells(I,2).Value = ""
  End If
Next
Another, possibly much better, idea is to alter the .Value of the cell based on parsing the Worksheet_Change event Range values to see if any cells in the 'Range' match the column criteria. This would automate the process so it is not tied to any button... if that is advantageous.
__________________
"May the code that you write never work in ways that you didn't expect; and may the code that you didn't write never require you to maintain it". - Ancient Chinese Proverb

Last edited by Cerian Knight; 08-10-2012 at 03:55 PM.
Reply With Quote
  #7  
Old 08-13-2012, 04:17 AM
Spaggiari Spaggiari is offline
Newcomer
 
Join Date: Aug 2012
Posts: 9
Default

Quote:
Originally Posted by Gruff View Post
Spaggiari,

1) Not everyone can open an xlsm file. Many have earlier versions of Excel.
This is why CK asked you to post the code you are using as text.

2) You still did not answer Cerian Knight's VBA code question.

If you want to run a program on static data then you do not need to put a formula in the cells. VBA can do the calculations and put a hard value in the cells.
Things will change only when you run the program and the resullting file should be much smaller.
Hey Gruff I didn't post any code because I didn't have any, I'm really stuck at this and I asked for help in that way. I know that not everyone can open a xlsm file and I know that people may have earlier versions I just uploaded the file so it would be easier to others if they saw the app don't take me wrong.

Anyways I'll work on the code CK posted and I'll keep this thread updated.

Ty
Reply With Quote
  #8  
Old 08-17-2012, 03:38 AM
Spaggiari Spaggiari is offline
Newcomer
 
Join Date: Aug 2012
Posts: 9
Default

This is what I came up with, created a table and applied a formula to the fields and its working fine.

You can take a look here https://www.dropbox.com/s/qb2m9a7a6a...project.xlsm?m

Ty
Reply With Quote
  #9  
Old 08-17-2012, 07:29 PM
Cerian Knight's Avatar
Cerian Knight Cerian Knight is offline
Multi-Technologist

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 3,740
Default

Glad to hear you got it working. I was actually kind of impressed with what you had done (e.g., UserForms, etc.). One of the stumbling blocks to creating some of the code in my example, is the frustrating recognition that the IntelliSense is somewhat broken in VBA (2003, anyway)... so it is not easy to find some of the useful syntax (unless you already know it exists).
__________________
"May the code that you write never work in ways that you didn't expect; and may the code that you didn't write never require you to maintain it". - Ancient Chinese Proverb
Reply With Quote
  #10  
Old 08-20-2012, 03:30 AM
Spaggiari Spaggiari is offline
Newcomer
 
Join Date: Aug 2012
Posts: 9
Default

Quote:
Originally Posted by Cerian Knight View Post
Glad to hear you got it working. I was actually kind of impressed with what you had done (e.g., UserForms, etc.). One of the stumbling blocks to creating some of the code in my example, is the frustrating recognition that the IntelliSense is somewhat broken in VBA (2003, anyway)... so it is not easy to find some of the useful syntax (unless you already know it exists).
Got it working with some help from other forums too, and yes its not easy to find the syntax unless you already know it like you said.

Cheers
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->