I want to put the text in the cell of column B in front of the the text in column D.

B

bartman1980

I want to put the text in the cell of column B in front of the the
text in column D.
But only if there is a text filled in in column D.

Does anybody have a VBA code for it?
 
D

Dave Peterson

I'd use a formula in a helper cell like:

=if(d1="","",b1&d1)
or
=if(d1="","",b1&" "&d1)

And drag down the column.

Then edit|copy that helper column
then select column D
and edit|paste special|values
 
B

bartman1980

I'd use a formula in a helper cell like:

=if(d1="","",b1&d1)
or
=if(d1="","",b1&" "&d1)

And drag down the column.

Then edit|copy that helper column
then select column D
and edit|paste special|values

Hi Dave,
I can do this, but I want it in a VBA code.
I'm sure it can be made in a VBA code.
 
D

Dave Peterson

Option Explicit
Sub testme()

Dim FirstRow As Long
Dim LastRow As Long
Dim iRow As Long

With Worksheets("sheet1")
FirstRow = 2 'headers in row 1???
LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row

For iRow = FirstRow To LastRow
If IsEmpty(.Cells(iRow, "D").Value) Then
'skip it
Else
.Cells(iRow, "D").Value _
= .Cells(iRow, "B").Text & " " & .Cells(iRow, "D").Text
End If
Next iRow
End With
End Sub

I put a space character between the values. Remove it or change it to what you
want.
 
B

bartman1980

Option Explicit
Sub testme()

Dim FirstRow As Long
Dim LastRow As Long
Dim iRow As Long

With Worksheets("sheet1")
FirstRow = 2 'headers in row 1???
LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row

For iRow = FirstRow To LastRow
If IsEmpty(.Cells(iRow, "D").Value) Then
'skip it
Else
.Cells(iRow, "D").Value _
= .Cells(iRow, "B").Text & " " & .Cells(iRow, "D").Text
End If
Next iRow
End With
End Sub

I put a space character between the values. Remove it or change it to what you
want.

Thank you dave!
This works!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top