Consulting

Results 1 to 18 of 18

Thread: Help needed with excel macro problem!

  1. #1
    VBAX Regular
    Joined
    Oct 2007
    Location
    Sheffield
    Posts
    63
    Location

    Help needed with excel macro problem!

    Hello All
    My first post here so here goes...I'm really new to VBA and need some help to write a macro to do a simple calculation (I tried to attach a screen-grab of the file but moderators won't let me do this until my post count hits 5, nor will they allow me to include links... ). Can I get round them? [Apologies to mods in advance!] Here goes...replace UPPER-CASE letters with approriate symbols:

    httpCOLONSLASHSLASHi122.photobucket.comSLASHalbumsSLASHo250SLASHmaz1889SLAS Hscreen-grab-excel.jpg


    In the excel file, I want to create a macro that will perform a simple multiplication whenever 'Name=Barry' and 'Category=High', such that the value in the 'Hours' column is multiplied by (85/65). This value will be populated in the adjacent column 'Adjusted Hours' (which can be empty prior to the macro running...actually it could be created BY the macro, if need be...it could even REPLACE the value in the 'Hours' column, so I'm flexible about that).

    Can anyone help me do this? The only exprience I've had with macros is with the 'record macro' facility in excel but have never done anything that involves things like conditional 'IF-statements'.

    Thanks for your help. It's much appreciated.

    Lester
    Last edited by Lester; 10-22-2007 at 03:45 PM. Reason: typos

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Public Sub ProcessData()
    Dim i As Long
    Dim iLastRow As Long

    With ActiveSheet

    iLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    iStart = 1
    For i = 2 To iLastRow
    If .Cells(i, "A").Value = "Barry" And _
    .Cells(i, "B").Value = "High" Then
    .Cells(i, "D").Value = .Cells(i, "C").Value * 85 / 65
    Else
    .Cells(i, "D").Value = .Cells(i, "C").Value
    End If
    Next i
    End With

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Oct 2007
    Location
    Sheffield
    Posts
    63
    Location
    Muchas gracias por su ayuda, xld. Me alegra de verdad que me haya ayudado. Voy a probar el c?digo de arriba - esperemos que me sirva. Ud es muy amable.
    ---
    Thanks for your help, xld. I'm really pleased that you've helped me. I'm going to try the above code - let's hope it works ok. You're very kind. (...and, yes, I speak reasonable Spanish!)

    Un saludo
    Lester
    Last edited by Lester; 10-23-2007 at 01:41 AM.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It may not work exactly as you want as it only seems to cater for one condition, but when you try it you should be able to be clear on what else it needs to do (which I couldn't get from the jpg)>

    BTW, why did you munge the URL, it's only a share site?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Oct 2007
    Location
    Sheffield
    Posts
    63
    Location
    Quote Originally Posted by xld
    It may not work exactly as you want as it only seems to cater for one condition, but when you try it you should be able to be clear on what else it needs to do (which I couldn't get from the jpg)>

    BTW, why did you munge the URL, it's only a share site?
    The moderators of this forum would not allow me to include a 'link' in my post, hence I 'disguised' it.
    Many thanks.

  6. #6
    VBAX Regular
    Joined
    Oct 2007
    Location
    Sheffield
    Posts
    63
    Location
    Another newbie question...which VBA window do I paste the code into? Do I go (in excel) to Tools|Macros|Visual Basic Editor, then paste it in the window to the right? I ask because there is already the code of a recorded macro in this window.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    They only stop you adding a link because you are new, you need 5 posts, not because links are forbidden. Strange rule, but there you are.

    If there is recorded code in that code pane, that is the place to put it. You then need to run it, in excel use Alt-F8 and select it from the list, and hit the Run button.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Regular
    Joined
    Oct 2007
    Location
    Sheffield
    Posts
    63
    Location
    Many thanks again.

  9. #9
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Quote Originally Posted by xld
    They only stop you adding a link because you are new, you need 5 posts, not because links are forbidden. Strange rule, but there you are.
    I presume ... it stops a lot of automated spam by one-time users with links to all manner of dodgy sites.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  10. #10
    VBAX Regular
    Joined
    Oct 2007
    Location
    Sheffield
    Posts
    63
    Location
    xld...Genial! Funciona muy bien. Tuve que modificarlo un poquito, pero me sirve de mucha ayuda. Cheers.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by TonyJollans
    I presume ... it stops a lot of automated spam by one-time users with links to all manner of dodgy sites.
    Most spammers would hardly be deterred by that, 5 posts is nothing when you send millions.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    They don't post to forums in quite the same way they send spam e-mail and they don't expect to get the chance to make multiple posts. They have automated programs that sign up, respond to emails, and send a post. Next time they just use the same process all over again with a new id. It's automated - sending 5 posts I suspect needs manual intervention which they have neither time nor inclination for - they just go for easier targets. I'm sure it's not 100% or foolproof but it is a deterrent.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I'll take your word for it Tony. I would hope that the forums have Bayesian filters as well to trap the junk that does hit. Luckily it seems to work. Have you noticed all the KEYGEN posts hitting the forums recently, including on it shouldn't do?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    VBAX Regular
    Joined
    Oct 2007
    Location
    Sheffield
    Posts
    63
    Location
    Continuing with this same excerise...
    I have now created the macro plus I have also created a PivotTable for displaying some of the data.

    Now, does anyone know what is the best way (if at all possible) to be able to use this new macro/pivotTable on a different (source) data file?

    What would be the best way to do this? Would it be possible to 'import' the new source data into the existing file containing the macro, then re-run the macro against the new data? How about the PivotTable? Would that also be regenerated with the new set of input data?

    I'm not sure the best way of utilising the macro/table for other sets of data that have the same 'structure' as my original set of data.

    Any ideas? Please let me know.
    Many thanks
    Lester.

  15. #15
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Quote Originally Posted by xld
    Have you noticed all the KEYGEN posts hitting the forums recently, including on it shouldn't do?
    Getting well OT now but yes, interesting, isn't it?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  16. #16
    VBAX Regular
    Joined
    Oct 2007
    Location
    Sheffield
    Posts
    63
    Location
    Quote Originally Posted by Lester
    Continuing with this same excerise...
    I have now created the macro plus I have also created a PivotTable for displaying some of the data.

    Now, does anyone know what is the best way (if at all possible) to be able to use this new macro/pivotTable on a different (source) data file?

    What would be the best way to do this? Would it be possible to 'import' the new source data into the existing file containing the macro, then re-run the macro against the new data? How about the PivotTable? Would that also be regenerated with the new set of input data?

    I'm not sure the best way of utilising the macro/table for other sets of data that have the same 'structure' as my original set of data.

    Any ideas? Please let me know.
    Many thanks
    Lester.
    Anyone? Please?

  17. #17
    Quote Originally Posted by Lester
    xld...Genial! Funciona muy bien. Tuve que modificarlo un poquito, pero me sirve de mucha ayuda. Cheers.
    XLD, (Bob) I think he thinks you are a Chilean, Spanish speaker. I wonder where he picked that up ???

    I was in the Prada Museum in Madrid and heard a woman order "Dos Beerios" in the Caf? there. I should imagine your spanish is better than that as you have worked in spanish speaking countries ... but how good ?
    2+2=9 ... (My Arithmetic Is Mental)

  18. #18
    VBAX Regular
    Joined
    Oct 2007
    Location
    Sheffield
    Posts
    63
    Location
    Quote Originally Posted by unmarkedhelicopter
    XLD, (Bob) I think he thinks you are a Chilean, Spanish speaker. I wonder where he picked that up ???
    It was the Chilean flag and his profile...you guys, what are you like, eh?!
    Anyhow, can anyone help me with my question (see my last entry above).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •