VBA code to fill values and text format

Chefsohail

Board Regular
Joined
Oct 3, 2020
Messages
90
Office Version
  1. 365
Platform
  1. Windows
Hi @Fluff and Team,

Please help me with the below vba code. I am able to achieve the following - 1. Add formula to the cell and 2. Fill Formula down... What i achieved is that the formula is filled down as required. But i do not want the formula here. I want its corresponding value to appear and the cell to be in text format as there are preceeding zeros and i dont want to loose them. The vlookup in the code below looks up those values from the other sheet. Please help

VBA Code:
wsDS.Range("B2").Formula = "=LEFT(A2,8)": wsDS.Range("B2:B" & wsDS.UsedRange.Rows.Count).FillDown
wsDS.Range("D2").Formula = "=RIGHT(C2,5)": wsDS.Range("D2:D" & wsDS.UsedRange.Rows.Count).FillDown
wsDS.Range("E2").Formula = "=VLOOKUP(D2,'UOM Comm'!D:R,2,0)": wsDS.Range("E2:E" & wsDS.UsedRange.Rows.Count).FillDown
wsDS.Range("F2").Formula = "=VLOOKUP(D2,'UOM Comm'!D:R,7,0)": wsDS.Range("F2:F" & wsDS.UsedRange.Rows.Count).FillDown
wsDS.Range("i2").Formula = "=LEN(H2)": wsDS.Range("i2:i" & wsDS.UsedRange.Rows.Count).FillDown
wsDS.Range("k2").Formula = "=LEN(J2)": wsDS.Range("k2:k" & wsDS.UsedRange.Rows.Count).FillDown
wsDS.Range("P2").Value = Format(Date, Format:="mm/dd/yy"): wsDS.Range("P2:P" & wsDS.UsedRange.Rows.Count).FillDown
wsDS.Range("R2").Formula = "=IF(M2="""",""Failure"", IF(LEFT(J2,1)=CHAR(41),""Na"",""Auto Approve""))": wsDS.Range("R2:R" & wsDS.UsedRange.Rows.Count).FillDown
wsDS.Range("O2").Value = "Description New Task": wsDS.Range("O2:O" & wsDS.UsedRange.Rows.Count).FillDown


I want the following to stay as formulae and not change..

VBA Code:
wsDS.Range("i2").Formula = "=LEN(H2)": wsDS.Range("i2:i" & wsDS.UsedRange.Rows.Count).FillDown
wsDS.Range("k2").Formula = "=LEN(J2)": wsDS.Range("k2:k" & wsDS.UsedRange.Rows.Count).FillDown

Just the others to paste Values and text format.

Fluff had given a resolution as below for the first line of code.

VBA Code:
wsDS.Range("B2").Formula = "=LEFT(A2,8)": wsDS.Range("B2:B" & wsDS.UsedRange.Rows.Count).FillDown

to be replaced with

VBA Code:
With wsDS.Range("A2", wsDS.Range("A" & Rows.Count).End(xlUp))
      .Offset(, 1).Value = Evaluate("if({1},left(" & .Address & ",8))")
   End With

This worked. But it changes the format and the preceeding zero's are lost. I am OK with this one... But for the other line of codes, i need them. Thanks.
 

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"
Hi,​
after the Range.Formula = … and the Range.FillDown - as FillDown could be avoided with a formula directly on all cells of the range … -
just add a Range.Formula = Range.Value2 codeline …​
 
Upvote 0
Hey @Marc L ,

Thanx for your assistance.

I tried to replace -
VBA Code:
wsDS.Range("D2").Formula = "=RIGHT(C2,5)": wsDS.Range("D2:D" & wsDS.UsedRange.Rows.Count).FillDown

with

VBA Code:
wsDS.Range("D2").Formula = "=RIGHT(C2,5)": wsDS.Range("D2:D" & wsDS.UsedRange.Rows.Count).FillDown
wsDS.Range("D2:D" & wsDS.UsedRange.Rows.Count).Formula = wsDS.Range("D2:D" & wsDS.UsedRange.Rows.Count).Value2

However the first value i.e D2 is copied untill the last row.

When i tried replacing with
VBA Code:
wsDS.Range("D2").Formula = "=RIGHT(C2,5)": wsDS.Range("D2:D" & wsDS.UsedRange.Rows.Count).Value2

received Complie error: Invalid use of Property.

I am sure I must have made a mistake. Please advice.
 
Upvote 0
VBA Code:
    With wsDS.Range("D2:D" & wsDS.UsedRange.Rows.Count)
        .Formula = "=RIGHT(C2,5)"
        .Formula = .Value2
    End With
 
Upvote 0
Try adding the line below before the .Value2 line.
This will format the cells as text.

VBA Code:
        .NumberFormat = "@"

Alternatively if you know its always going to be 5 characters and you want to keep it as a number just use:-
VBA Code:
        .NumberFormat = "00000"
 
Upvote 0

Forum statistics

Threads
1,214,383
Messages
6,119,198
Members
448,874
Latest member
Lancelots

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