VBA Code find specific column header& keep first 4 characters in each cell.

Coder119

New Member
Joined
Oct 14, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I need to write a code that looks for a specific column header and then keeps only the first 4 characters in each cell in that column. I have looked at differed pieces of code and nothing works. I don't want to enter Left function manually into to each cell on the sheet because I have multiple formatting that I need to do, and I want to have that in a VBA format. So far, I am writing individual code for each format and then calling them together into one macro. However, I am stuck on this one. Any suggestions? Below, I have just included a dummy example as my data is not sharable. Using the example below let's say I want the column "Cheese" to only show the first 4 characters in each cell, but that column may not always fall in column D, so the position of that header could change from time to time. What VBA code could I use to accomplish this?

1665746800157.png
 
So, I will ask yet again.
Have you tried the code I provided you up in post 3?

I created your data in a sheet on my side, and tried it out, and it worked for me.
If it doesn't work for you, then something is different from what you originally posted, and you need to tell us what that difference is.
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I received an error on the. .Address stating invalid or unqualified reference.

Hi, did you try the code as posted or did you try to amend it or add it to some existing code?
 
Upvote 0
I will try the second code block tomorrow. Sorry, I have been so busy the last few days I've had little time to test this out. I will try it tomorrow and see if it works and will post my results. Thank you!
 
Upvote 0
You are welcome.
Glad we were able to help.
 
Upvote 0
Any chance either of you knows how I can get leading zeros to stay when concatenating 2 columns using VBA? I already used the number format "000000000" to get the one column to add the preceding zero, but when I do the VBA code to concatenate them it drops the zero. Text won't work cause then my value does not concatenate anything but the formula.
 
Upvote 0
Any chance either of you knows how I can get leading zeros to stay when concatenating 2 columns using VBA? I already used the number format "000000000" to get the one column to add the preceding zero, but when I do the VBA code to concatenate them it drops the zero. Text won't work cause then my value does not concatenate anything but the formula.
That is an entirely different/separate question, and therefore should be posted to its own new thread.
Also, be sure to show some examples of your data.
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,476
Members
452,915
Latest member
hannnahheileen

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