PDA

View Full Version : Solved: add a word to the row have a data



parscon
12-27-2012, 08:05 AM
I need a VBA code that add this word "Model:" to the all the row have data on column A (because I have blank row also) that mean if I have data on row 1 on column A like this : 23232323, when run this VBA it will be : Model:23232323

Thank you for your help .

magelan
12-27-2012, 08:11 AM
I need a VBA code that add this word "Model:" to the all the row have data on column A (because I have blank row also) that mean if I have data on row 1 on column A like this : 23232323, when run this VBA it will be : Model:23232323

Thank you for your help .

if you are fluent in VBA at all, you just need to write a for-loop. it will look something like...

for each cell in used range
cell.value = "Model:" & cell.value
next

parscon
12-27-2012, 08:13 AM
Thank you could you please give me exact code ? and also I need this for the row have a data on their column A .

Thank you

Kenneth Hobs
12-27-2012, 08:15 AM
Sub ModelColA()
Dim c As Range, r As Range
With Worksheets("Sheet1")
Set r = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
End With
For Each c In r
If Not IsEmpty(c) Then c.Value = "Model:" & c.Value
Next c
End Sub

parscon
12-27-2012, 09:34 AM
Thank you very much , now everything is OK just small modification , now i need add Model to column B (column B is empty) that mean do not add Model to column A add this word just to column B .

Thank you .

Kenneth Hobs
12-27-2012, 10:14 AM
I am not sure that I understand. Column B could have a million rows. I would just add it to the same number of rows that has data in the last row of column A with data.

Sub ModelColB()
Dim r As Range
With Worksheets("Sheet1")
Set r = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
End With
r.Offset(0, 1).Value = "Model"
End Sub

parscon
12-27-2012, 10:19 AM
Dear Kenneth Hobs,

Thank you for your help , now just check column A , if it is empty do not write Model in column B if the column A have a Data add Model to column B on same row .

Thank you very much again for your help .

Kenneth Hobs
12-27-2012, 10:26 AM
You really have all that you need from my examples. You can combine both operations in one routine if that is what you want. You might want to explore conditional formatting or a change event method.

For doing just your last thing:
Sub ModelColB2()
Dim c As Range, r As Range
With Worksheets("Sheet1")
Set r = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
End With
For Each c In r
If Not IsEmpty(c) Then c.Offset(0, 1).Value = "Model"
Next c
End Sub

parscon
12-28-2012, 12:16 AM
Thank you very much . it is done .