2014-04-09 3 views
0

예를 들어 하나의 열에 이름이 채워져 있고 각 이름은 하나의 셀에 있습니다.하나의 열을 두 개의 열로 나누는 VBS

First_Name_1 Last_Name_1 
    First_Name_2 Last_Name_2 
    First_Name_3 Last_Name_4 

성과 이름은 공백으로 구분됩니다. Visual Basic Script를 사용하여이 열을 두 개의 열로 나누어서 한 열에 First_Name이 있고 그 옆에 열에 Last_name이 있어야하는 방법은 무엇입니까?

이미이 코드를 사용해 보았지만 제대로 작동하지는 않습니다.

objExcel.ActiveSheet.Columns(4).Select 
    Do Until IsEmpty(ActiveCell) 
    'Search for position of space within the cell 
    SplitPoint = InStrRev(ActiveCell, " ", -1, vbTextCompare) 
    'Put the last name in the column next to the source column 
    ActiveCell.Offset(0, 1) = Trim(Left(ActiveCell, SplitPoint)) 
    'Replace the source column with the first name 
    ActiveCell.Offset(0, 0) = Trim(Mid(ActiveCell, SplitPoint)) 
    Loop 

미리 감사드립니다.

당신이 범위의 다음 셀로 이동되지 않습니다

답변

0

...

추가 :

을 ActiveCell.Offset (1, 0) .Activate

나는 확인하기 위해 뭔가를 추가 할 것

이중 공백 (하나 제거), 공백 없음 및 이중 이름 또는 이니셜 (Mary Lou Smith, John M. Lewis).

챠오

0

Excel에서 하나의 함수 호출로 찾고있는 무엇을 할 수있는 TextToColumns() 기능을 가지고 있습니다.

objExcel.ActiveSheet.Columns(4).TextToColumns , , , , , , , True 

표시된 매개 변수에 더 많은 의미가 있습니다. 데이터는 공백으로 구분되어 있으므로 8 번째 매개 변수를 True으로 설정하면됩니다. 나머지를 생략하여 기본값을 승인 할 수 있습니다.

objExcel.ActiveSheet.Columns(4).TextToColumns _ 
    , _  ' Destination (optional, defaults to source) 
    , _  ' DataType (optional, defaults to xlDelimited) 
    , _  ' TextQualifier (optional, defaults to xlTextQualifierDoubleQuote) 
    , _  ' ConsecutiveDelimiter 
    , _  ' Tab-delimited? (optional, defaults to False) 
    , _  ' Semicolon-delimited? (optional, defaults to False) 
    , _  ' Comma-delimited? (optional, defaults to False) 
    True  ' Space-delimited? 
관련 문제