VBA check length of a cell and if conditions met, copy to another cell with additional string

excel01noob

Board Regular
Joined
Aug 5, 2019
Messages
93
Office Version
  1. 365
  2. 2016
Hi
I have a dataset that contains under column H a numeric value (user account).
This numeric value is usually 5 digits.
I want to create a macro that checks if the length is 5 and if yes copy the numeric value and paste it under column K but adding a character before the numeric value like this

12345 - column H
X12345 - column K

Have created the below but it does not work, can you help me out?

Sub LengthCheck()

Dim i As Long
Dim Lastrow As Long
Dim MyStr1 As String
Dim MyStr2 As String

Application.ScreenUpdating = False

Lastrow = Cells(Rows.Count, "H").End(xlUp).Row
MyStr1 = "X"
MyStr2 = ("H2:H" & Lastrow)


For i = 2 To Lastrow
If Len(Cells(i, 8)) = 5 Then
'Cells(i, 11).Value = MyStr1 + MyStr2
End If
Next

Application.ScreenUpdating = True

End Sub



In case the numeric account value is more than 5 digits, I would need to refer to another excel file dataset, look the value in that dataset (I'm using a manual VLOOKUP) and take the format from column G, would love to develop the macro for this part as well
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi.
For the first part of your issue try replacing ~~~> Cells(i, 11).Value = MyStr1 + MyStr2 by ~~~> Cells(i, 11).Value = MyStr1 & Cells(i, 8)

For the second part we'll need to see more info from you.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,829
Messages
6,121,826
Members
449,051
Latest member
excelquestion515

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