Macro is not working for the right column

Shweta

Well-known Member
Joined
Jun 5, 2011
Messages
514
Hi All,

I have created a macro for column C that is based on column A and column B. It is working properly.

But when I am inserting a column between column B & C then the coding of column C should be moved to column D because I have inserted this column for something else but it is remaining for the same column.

For example:
.range("C"&i).value = .range("A"&i).value + .range("B"&i).value

After inserting the column it should be work like this
.range("D"&i).value = .range("A"&i).value + .range("B"&i).value

without doing any change in coding.

Please help!

Regards,
Shweta
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
without doing any change in coding.

Sorry, but if you want the code to do something different than what it currently does, then you have to change the coding...

There's a few approaches...

Do you have headers in each column?
Column A has "Name" in A1
Column B has "Age" in B1

or similar..
Just something to identify which columns to work with.
We can then do a search for those values in Row 1 to determine the columns.
And it will dynamically adjust when columns are inserted or deleted.
 
Upvote 0
Thanks for the quick response!

Yes, I have headings for columns. Column A is SALARY & Column B is INCENTIVE.
In column C I have to add the both. In this case coding is working fine.

coding : .range("C"&i).value = .range("A"&i).value + .range("B"&i).value

Now I insert a column before column A to enter the NAME of the employees. In this case it should be
.range("D"&i).value = .range("B"&i).value + .range("C"&i).value

I think now I am clear.
I need dynamic coding because when I do it with excel it is dynamic, formula automatically moves with the column. But it is not being possible with VBA.

Instead of entering the column name("A","B",...) in the coding, can we use HEADINGS so that we can match the coding with the headings & then when I move columns then coding automatically moves.

Some like that...??

Shweta
 
Upvote 0
Yes, Excellent.
But we also need the heading for column C.
for this example, we will say the heading in column C is RESULT

Rich (BB code):
Dim Slry As Long, Inctv As Long, Rslt As Long, HdrRw As Long
 
HdrRw = 1 'Assuming headers are in Row 1
Slry = Application.Match("Salary",Rows(HdrRw),0)
Inctv = Application.Match("Incentive",Rows(HdrRw),0)
Rslt = Application.Match("Result",Rows(HdrRw),0) 'change Result to Header value of Col C
 
Cells(i, Rslt).Value = Cells(i,Slry).Value + Cells(i, Inctv).Value

Hope that helps.
 
Upvote 0
Thanks Jonmo1,

Let me give it a try then I will get back to you.

Thanks again.

Shweta.
 
Upvote 0
Hi Jonmo1,

Sorry to say but this coding is not working
Before running the coding, my data is like:

Col1 Col2 Col3
SALARY INCENTIVE RESULT
20156 1500

After running the coding

Col1 Col2 Col3
SALARY INCENTIVE SALARYINCENTIVE
20156 1500

Please help me out

Shweta
 
Upvote 0
Post the code you used. The whole code.

What I posted was just meant as an example to show you how to do it.
Wasn't meant to fit your situation exactly..
 
Upvote 0
I am sorry Jonmo1, I tried it again & its working now

Thanks a lot!

Regards,
Shweta
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top