PDA

View Full Version : Solved: COPY/PASTE OR JOIN



trivrain
12-21-2010, 09:00 AM
Hi all,

I am looking for codes that would do the following, but I have no knowledge of VBA language:

For columns A, B, C in worksheet 3
- if cell Ax, Bx, and Cx are empty do nothing
- if only one cell has data (Ax or Bx or Cx) simply copy into cell Lx of worksheet 1
- if cell Ax has data, Bx has data and Cx is empty join content like this in Lx of worksheet 1: Ax // Bx
- if cell Ax has data, Bx is empty and Cx has data join content like this in Lx of worksheet 1: Ax // Cx
- if cell Ax is empty, Bx has data and Cx has data join content like this in Lx of worksheet 1: Bx // Cx
- If all have data, join like this in Lx of worksheet 1: Ax // Bx // Cx

For columns D and E in spreadsheet 3
- If cell Dx has data, and Ex is empty, simply copy into Mx in worksheet 1
- If cell Dx is empty, and Ex has data, prefix Ex content with "2" and copy to Mx in worksheet 1
- If both cell have data, prefixe Dx with "1" and Ex with "2" and join content like this in Mx of worksheet 2: "1" Dx / "2" Ex"

Anybody feeling up to it ?
Thank you, t

Bob Phillips
12-21-2010, 10:07 AM
Public Sub ProcessData()
Const TEST_COLUMN As String = "A" '<<<< change to suit
Dim sh As Worksheet
Dim Lastrow As Long
Dim i As Long
Dim cell As Range

Application.ScreenUpdating = False

Set sh = Worksheets("Sheet3")

With Worksheets("Sheet3")

Lastrow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
For i = 2 To Lastrow

With Worksheets("Sheet3")

sh.Cells(i, "L").Value = .Cells(i, "A").Value & " // " & _
.Cells(i, "B").Value & " // " & _
.Cells(i, "C").Value
sh.Cells(i, "L").Value = Replace(sh.Cells(i, "L").Value, " // // ", " // ")
If Left$(sh.Cells(i, "L").Value, 4) = " // " Then _
sh.Cells(i, "L").Value = Right$(sh.Cells(i, "L").Value, Len(sh.Cells(i, "L").Value) - 4)
If Right$(sh.Cells(i, "L").Value, 4) = " // " Then _
sh.Cells(i, "L").Value = Left$(sh.Cells(i, "L").Value, Len(sh.Cells(i, "L").Value) - 4)

If .Cells(i, "D").Value <> "" Then

If .Cells(i, "E").Value <> "" Then

sh.Cells(i, "M").Value = "1 " & .Cells(i, "D").Value & _
"2 " & .Cells(i, "E").Value
Else

sh.Cells(i, "M").Value = .Cells(i, "D").Value
End If
ElseIf .Cells(i, "E").Value <> "" Then

sh.Cells(i, "M").Value = "2 " & .Cells(i, "E").Value
End If
End With
Next i
End With

Application.ScreenUpdating = True
End Sub

Bob Phillips
12-21-2010, 10:07 AM
Public Sub ProcessData()
Const TEST_COLUMN As String = "A" '<<<< change to suit
Dim sh As Worksheet
Dim Lastrow As Long
Dim i As Long
Dim cell As Range

Application.ScreenUpdating = False

Set sh = Worksheets("Sheet3")

With Worksheets("Sheet3")

Lastrow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
For i = 2 To Lastrow

With Worksheets("Sheet3")

sh.Cells(i, "L").Value = .Cells(i, "A").Value & " // " & _
.Cells(i, "B").Value & " // " & _
.Cells(i, "C").Value
sh.Cells(i, "L").Value = Replace(sh.Cells(i, "L").Value, " // // ", " // ")
If Left$(sh.Cells(i, "L").Value, 4) = " // " Then _
sh.Cells(i, "L").Value = Right$(sh.Cells(i, "L").Value, Len(sh.Cells(i, "L").Value) - 4)
If Right$(sh.Cells(i, "L").Value, 4) = " // " Then _
sh.Cells(i, "L").Value = Left$(sh.Cells(i, "L").Value, Len(sh.Cells(i, "L").Value) - 4)

If .Cells(i, "D").Value <> "" Then

If .Cells(i, "E").Value <> "" Then

sh.Cells(i, "M").Value = "1 " & .Cells(i, "D").Value & _
"2 " & .Cells(i, "E").Value
Else

sh.Cells(i, "M").Value = .Cells(i, "D").Value
End If
ElseIf .Cells(i, "E").Value <> "" Then

sh.Cells(i, "M").Value = "2 " & .Cells(i, "E").Value
End If
End With
Next i
End With

Application.ScreenUpdating = True
End Sub