Welcome to the Invelos forums. Please read the forum rules before posting.

Read access to our public forums is open to everyone. To post messages, a free registration is required.

If you have an Invelos account, sign in to post.

    Invelos Forums->General: General Discussion Page: 1  Previous   Next
Excel (VBA) question
Author Message
DVD Profiler Desktop and Mobile RegistrantMarEll
Registered: June 9, 2007
United Kingdom Posts: 1,208
Posted:
PM this userView this user's DVD collectionDirect link to this postReply with quote
Anyone know how I can add the values one column another using VBA?

What I want to have are values in column B that never get touched manually and upon clicking a button the values in column C would be added to their adjacent cells in column B.
DVD Profiler Desktop and Mobile Registrantmediadogg
Aim high. Ride the wind.
Registered: March 18, 2007
Reputation: Highest Rating
United States Posts: 6,460
Posted:
PM this userVisit this user's homepageDirect link to this postReply with quote
Quoting MarEll:
Quote:
Anyone know how I can add the values one column another using VBA?

What I want to have are values in column B that never get touched manually and upon clicking a button the values in column C would be added to their adjacent cells in column B.

Did you try the Macro Recorder? My undersatnding is that if you turn it on, then do the steps manually, it will create the VBA for you. 
Thanks for your support.
Free Plugins available here.
Advanced plugins available here.
Hey, new product!!! BDPFrog.
DVD Profiler Unlimited Registrantxyrano
41215.reg 70320.urk
Registered: March 13, 2007
Sweden Posts: 646
Posted:
PM this userView this user's DVD collectionDirect link to this postReply with quote
Put this in a Module.
Quote:
Sub Macro1()
'
' Macro1 Macro
'

'
    Columns("C:C").Select
    Selection.Copy
    Columns("B:B").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("A1").Select
End Sub


Add a Button (Form Control) on the same sheet as the data you wish to C/P and assign Macro1.
 Last edited: by xyrano
DVD Profiler Desktop and Mobile Registrantmediadogg
Aim high. Ride the wind.
Registered: March 18, 2007
Reputation: Highest Rating
United States Posts: 6,460
Posted:
PM this userVisit this user's homepageDirect link to this postReply with quote
@xyrano: interesting. I thought by "added", he meant numerical addition (b+c = sum) ???
Thanks for your support.
Free Plugins available here.
Advanced plugins available here.
Hey, new product!!! BDPFrog.
 Last edited: by mediadogg
DVD Profiler Unlimited Registrantxyrano
41215.reg 70320.urk
Registered: March 13, 2007
Sweden Posts: 646
Posted:
PM this userView this user's DVD collectionDirect link to this postReply with quote
Quoting mediadogg:
Quote:
@xyrano: interesting. I thought by "added", he meant numerical addition (b+c = sum) ???

Hihihi, I got into Macro since you mentioned it Anyway, if (b+c = sum) is what he is looking for then he don't need macros.

Simply, select the cell in column D and hit the Sum icon in the toolbar, then put the mouse cursor in the lower right corner of the same cell and double click to copy the function downwards.

Ofcourse if it realy needs to be in a macro (although it won't be dynamic), then add this to a Module
Quote:
Sub Macro2()
'
' Macro2 Macro
'

'
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "=SUM(RC[-2]:RC[-1])"
    Range("D1").Select
    Selection.AutoFill Destination:=Range("D1:D5")
    Range("D1:D5").Select
    Range("A1").Select
End Sub
 Last edited: by xyrano
DVD Profiler Desktop and Mobile Registrantmediadogg
Aim high. Ride the wind.
Registered: March 18, 2007
Reputation: Highest Rating
United States Posts: 6,460
Posted:
PM this userVisit this user's homepageDirect link to this postReply with quote
Quoting xyrano:
Quote:
Quoting mediadogg:
Quote:
@xyrano: interesting. I thought by "added", he meant numerical addition (b+c = sum) ???

Hihihi, I got into Macro since you mentioned it Anyway, if (b+c = sum) is what he is looking for then he don't need macros.

Simply, select the cell in column D and hit the Sum icon in the toolbar, then put the mouse cursor in the lower right corner of the same cell and double click to copy the function downwards.

Right, but I think he wants to do it with a button that executes VBA. My point about the Macro Editor, is that if you do the the steps you outlined, it will write the VBA code for you, which is what I think he was asking for???? (Obviously I don't know VBA or I would have done what you did - just write it for him.)
Thanks for your support.
Free Plugins available here.
Advanced plugins available here.
Hey, new product!!! BDPFrog.
 Last edited: by mediadogg
DVD Profiler Desktop and Mobile RegistrantMarEll
Registered: June 9, 2007
United Kingdom Posts: 1,208
Posted:
PM this userView this user's DVD collectionDirect link to this postReply with quote
I don't think I've explained myself well.  Numerical addition is indeed what I'm after but a SUM function is inappropriate because I need col A to retain its new value when col B is changed or cleared.

For example A1 = 5, B1 = 2.  After running the code A1 is 7 and B1 could be cleared (either manually or by the code) then when the code is run again if B1 is empty A1 stays the same. If B1 has been changed to 11 then A1 becomes 18.

I have code that will do this but it is a separate line of code for each cell:

Dim d As Double
d = Range("A1").Value
Range("A1") = d + Range("B1").Value

I just thought surely there is a more efficient way of doing it.
DVD Profiler Unlimited RegistrantStar Contributormdnitoil
Registered: March 14, 2007
United States Posts: 1,777
Posted:
PM this userView this user's DVD collectionDirect link to this postReply with quote
Generally speaking, if you know the number of rows, then you can simply write a for loop, using your counter as a double for the cell reference number.  In VBA, it might be a do...while loop, but the same principle applies.  If the number of rows will vary, then some condition up top will be required to determine the bottom of the data.  Kind of a do while not null sort of thing with an incrementing counter.

In VBA, it looks like:

Dim i As Integer

i = 1

Do While Worksheets("Sheet1").Cells(i, 1).Value <> 0

    Worksheets("Sheet1").Cells(i, 1).Value = Worksheets("Sheet1").Cells(i, 1).Value + Worksheets("Sheet1").Cells(i, 2).Value

    i = i + 1

Loop

This would add the contents of the cells in column A and the cells in column B and dump them in column A.  It ends when it encounters a 0 in column A, which is the equivalent of a blank cell.  Probably not perfect with regards to the end condition, but you can tweak it from there.  If you have a set number of rows, then it's simply do while i <> last row number+1.  Just attach the code to a button on the form.

In order to add a button to the spreadsheet, you have to check off the developer tab in excel options and then choose the insert tab which will give you a choice of controls.  Once you do that, the assign macro dialog pops up, with the choices of new or record macro.  Pick new and paste the code into the button click event.
 Last edited: by mdnitoil
DVD Profiler Desktop and Mobile RegistrantMarEll
Registered: June 9, 2007
United Kingdom Posts: 1,208
Posted:
PM this userView this user's DVD collectionDirect link to this postReply with quote
I get a type mismatch on the line starting 'worksheets'.
DVD Profiler Unlimited RegistrantStar Contributormdnitoil
Registered: March 14, 2007
United States Posts: 1,777
Posted:
PM this userView this user's DVD collectionDirect link to this postReply with quote
If you get a type mismatch, that implies that you are trying to add two different data types, an obvious problem.  Try it first on a blank spreadsheet to confirm that it works in general.  If so, then go back and see what you column formats are to make sure there isn't some obvious mismatch.  Also note that "sheet1" is explicitely naming a sheet.  If you've renamed that sheet to be something more descriptive, then you'll have to modify the code accordingly.

I just simply created a new spreadsheet and entered the data:

1      2
3      4

and then added the button, then the code and ran it.  Works just fine for me.  I should probably also add that the worksheets line is a single line and the next new line is the i = i + 1.  Sometimes the formatting on my screen may not match the formatting on your screen due to screen resolution and the way the webpage displays.
 Last edited: by mdnitoil
DVD Profiler Unlimited Registrantxyrano
41215.reg 70320.urk
Registered: March 13, 2007
Sweden Posts: 646
Posted:
PM this userView this user's DVD collectionDirect link to this postReply with quote
I've got a working solution for this particullar problem using named ranges (no sheetnames or explicit cell references in VBA, also working for an entire column) which makes it dynamic.
Apart from addition, I also made it so a numreric or currency value can be subtracted, multiplied or divided.
I made it in an Macro Enabled Workbook for 2007-2010.

PM me your email and your Excel version (if other then 2007-2010) and I'll send it to you, if you are interested.
DVD Profiler Desktop and Mobile RegistrantMarEll
Registered: June 9, 2007
United Kingdom Posts: 1,208
Posted:
PM this userView this user's DVD collectionDirect link to this postReply with quote
Quoting mdnitoil:
Quote:
If you get a type mismatch, that implies that you are trying to add two different data types, an obvious problem.  Try it first on a blank spreadsheet to confirm that it works in general.  If so, then go back and see what you column formats are to make sure there isn't some obvious mismatch.  Also note that "sheet1" is explicitely naming a sheet.  If you've renamed that sheet to be something more descriptive, then you'll have to modify the code accordingly.

I just simply created a new spreadsheet and entered the data:

1      2
3      4

and then added the button, then the code and ran it.  Works just fine for me.  I should probably also add that the worksheets line is a single line and the next new line is the i = i + 1.  Sometimes the formatting on my screen may not match the formatting on your screen due to screen resolution and the way the webpage displays.


Yep.  Works in a blank workbook, must be something I did wrong.
DVD Profiler Desktop and Mobile RegistrantMarEll
Registered: June 9, 2007
United Kingdom Posts: 1,208
Posted:
PM this userView this user's DVD collectionDirect link to this postReply with quote
Quoting xyrano:
Quote:
I've got a working solution for this particullar problem using named ranges (no sheetnames or explicit cell references in VBA, also working for an entire column) which makes it dynamic.
Apart from addition, I also made it so a numreric or currency value can be subtracted, multiplied or divided.
I made it in an Macro Enabled Workbook for 2007-2010.

PM me your email and your Excel version (if other then 2007-2010) and I'll send it to you, if you are interested.

I've actually just noticed that in the paste special window you get the option to 'add' the values to the paste destination rather than just replace them.  Can't believe I've never spotted that before 

I'm still interested to see what you've cooked up though as I could use it for other things that a simple copy paste wouldn't work for.  YGPM
DVD Profiler Unlimited Registrantxyrano
41215.reg 70320.urk
Registered: March 13, 2007
Sweden Posts: 646
Posted:
PM this userView this user's DVD collectionDirect link to this postReply with quote
Quoting MarEll:
Quote:
Quoting xyrano:
Quote:
I've got a working solution for this particullar problem using named ranges (no sheetnames or explicit cell references in VBA, also working for an entire column) which makes it dynamic.
Apart from addition, I also made it so a numreric or currency value can be subtracted, multiplied or divided.
I made it in an Macro Enabled Workbook for 2007-2010.

PM me your email and your Excel version (if other then 2007-2010) and I'll send it to you, if you are interested.

I've actually just noticed that in the paste special window you get the option to 'add' the values to the paste destination rather than just replace them.  Can't believe I've never spotted that before 

I'm still interested to see what you've cooked up though as I could use it for other things that a simple copy paste wouldn't work for.  YGPM

Yea, I've been doing stuff with it for x-years and I still find new stuff :D

YGM
    Invelos Forums->General: General Discussion Page: 1  Previous   Next