Hi everyone, I have data that comes out of our database concatenated that our client wants presented in 2 columns. The problem is that not all of the data is separated into the two columns, and there is a lot of extraneous text that needs to be deleted. I've written code to pull out the lines that need to be separated and used text to colums to get the text into two columns, but I'm stuck as to how to get the two columns back into the original spreadsheet. Here is the code I have so far:
[vba]Sub correcttimepoint()
Dim SampName As Range
Dim Cell As Variant
Dim sh_Source As Worksheet
Dim sh_Dest As Worksheet, wks As Worksheet
Dim NextrowD As Variant
Dim LastCell As Range
Set sh_Dest = ActiveWorkbook.Worksheets(3)
Set sh_Source = ActiveWorkbook.Worksheets(1)
Set SampName = Worksheets(1).Range("C:C").SpecialCells(xlCellTypeConstants)
Application.ScreenUpdating = False
NextrowD = sh_Dest.Range("A" & Rows.Count).End(xlUp).Row
For Each Cell In SampName
If InStr(Cell.Value, "PLM") <> 0 Then
NextrowD = NextrowD + 1
With sh_Source
.Range("B" & Cell.Row).Copy
sh_Dest.Range("A" & NextrowD).PasteSpecial (xlPasteValues)
.Range("C" & Cell.Row).Copy
sh_Dest.Range("B" & NextrowD).PasteSpecial (xlPasteValues)
End With
End If
Next Cell
sh_Dest.Range("B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.TextToColumns Destination:=Range("B2"), DataType:=xlDelimited, _
TextQualifier:=xlNone, ConsecutiveDelimiter:=False, Tab:=True, Semicolon _
:=False, Comma:=False, Space:=True, Other:=True, OtherChar:="/", _
FieldInfo:=Array(Array(1, 9), Array(2, 9), Array(3, 1), Array(4, 9), Array(5, 1), Array _
(6, 1), Array(7, 1), Array(8, 1), Array(9, 9), Array(10, 9), Array(11, 9), Array(12, 9), _
Array(13, 9), Array(14, 9)), TrailingMinusNumbers:=True
Set LastCell = Range("A65536").End(xlUp).Offset(0, 6)
Range("G2").Select
ActiveCell.FormulaR1C1 = "=RC[-4]&"" ""&RC[-3]&"" ""&RC[-2]&RC[-1]"
Selection.AutoFill Destination:=Range("G2", LastCell), Type:=xlFillDefault
sh_Source.Activate
Columns("D").Select
Selection.Insert Shift:=xlToRight
Application.ScreenUpdating = True
End Sub[/vba]
Two other issues I am having. One is that the data starts pasting in row 2 and I'm not sure why. The other is that at times, the code stops at sh_dest.range("B2").select with the message "select method of range class failed". Do I need to activate the worksheet?
I've also attached the spreadsheet.
Thank you in advance for your help.
Kathyb0527