VBA code to fill values and text format

Chefsohail

Board Regular
Joined
Oct 3, 2020
Messages
81
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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Marc L

Well-known Member
Joined
Apr 5, 2021
Messages
1,621
Office Version
  1. 2010
Platform
  1. Windows
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 …​
 

Chefsohail

Board Regular
Joined
Oct 3, 2020
Messages
81
Office Version
  1. 365
Platform
  1. Windows
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.
 

Marc L

Well-known Member
Joined
Apr 5, 2021
Messages
1,621
Office Version
  1. 2010
Platform
  1. Windows
VBA Code:
    With wsDS.Range("D2:D" & wsDS.UsedRange.Rows.Count)
        .Formula = "=RIGHT(C2,5)"
        .Formula = .Value2
    End With
 

Chefsohail

Board Regular
Joined
Oct 3, 2020
Messages
81
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Thanx @Marc L ,

With this the value is in place now. But the preceeding zeros are lost.
 

Chefsohail

Board Regular
Joined
Oct 3, 2020
Messages
81
Office Version
  1. 365
Platform
  1. Windows
Any idea how to ensure that preceding zero's are not lost
 

Alex Blakenburg

Well-known Member
Joined
Feb 23, 2021
Messages
2,064
Office Version
  1. 365
Platform
  1. Windows
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"
 

Forum statistics

Threads
1,144,523
Messages
5,724,836
Members
422,582
Latest member
Vaibhav Wadhwa

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
Top