2011-03-15 2 views
0

VBA를 사용하여 가져 오기 헤더를 제거합니다.VBA를 사용하여 셀 서식을 복사 할 수 있습니까? 즉 값을 숫자가 아닌 문자열로 처리 할 수 ​​있습니까?

제가 지금 겪고있는 문제는 프로세스가 0에서 시작하는 부분을 자르는 것입니다. 셀 (#, #) 객체에 사용할 수있는 모든 메서드에 대한 설명으로 나를 가리키는 간단한 것조차 도움이됩니다.


' VBScript source code 
Option Explicit 
REM We use "Option Explicit" to help us check for coding mistakes 

REM the Excel Application 
Dim objExcel 
REM the path to the excel file 
Dim excelPath 
REM how many worksheets are in the current excel file 
Dim worksheetCount 
Dim counter 
REM the worksheet we are currently getting data from 
Dim currentWorkSheet 
REM the number of columns in the current worksheet that have data in them 
Dim usedColumnsCount 
REM the number of rows in the current worksheet that have data in them 
Dim usedRowsCount 
Dim row 
Dim column 
REM the topmost row in the current worksheet that has data in it 
Dim top 
REM the leftmost row in the current worksheet that has data in it 
Dim left 
Dim Cells 
REM the current row and column of the current worksheet we are reading 
Dim curCol 
Dim curRow 
REM the value of the current row and column of the current worksheet we are reading 
Dim word 

REM the text file writer that I will output to to see what this process is creating 
Dim objFSOExcel 
REM the output worksheet 
Dim outputWorksheet 
Dim objWorkbook 

REM where is the Excel file located? 
excelPath = "C:\Documents and Settings\kitchenjt\My Documents\Professional Development\input\11TRVL.xls" 

REM Create an invisible version of Excel 
Set objExcel = CreateObject("Excel.Application") 

REM create the Output Excel file 
Set objFSOExcel = CreateObject("Excel.Application") 
Set objWorkBook = ObjFSOExcel.Workbooks.Add 
Set outputWorksheet = objWorkBook.Worksheets(1) 
'outputWorksheet.Name = "SafeTRVL" 

REM don't display any messages about documents needing to be converted 
REM from old Excel file formats 
objExcel.DisplayAlerts = 0 
objFSOExcel.DisplayAlerts = 0 


REM open the excel document as read-only 
REM open (path, confirmconversions, readonly) 
objExcel.Workbooks.open excelPath, false, true 


REM How many worksheets are in this Excel documents 
REM changed to only get the first worksheet 
workSheetCount = 1'objExcel.Worksheets.Count 

Dim outrow 
outrow = 1 

REM Loop through each worksheet 
For counter = 1 to workSheetCount 
    'txtFile.writeLine("-----------------------------------------------") 
    'txtFile.writeLine("Reading data from worksheet " & counter & vbCRLF) 

    Set currentWorkSheet = objExcel.ActiveWorkbook.Worksheets(counter) 
    REM how many columns are used in the current worksheet 
    usedColumnsCount = currentWorkSheet.UsedRange.Columns.Count 
    REM how many rows are used in the current worksheet 
    usedRowsCount = currentWorkSheet.UsedRange.Rows.Count 

    REM What is the topmost row in the spreadsheet that has data in it 
    top = currentWorksheet.UsedRange.Row 
    REM What is the leftmost column in the spreadsheet that has data in it 
    left = currentWorksheet.UsedRange.Column 


    Set Cells = currentWorksheet.Cells 
    REM Loop through each row in the worksheet 
    For row = 0 to (usedRowsCount-1) 

     REM Loop through each column that has an intial value that starts with T in the worksheet 
     REM only look at rows that are in the "used" range 
     curRow = row+top 
     word = Cells(curRow,left).Value 
     REM display the column on the screen 
     if word <> "" and InStr(1,word,"T",1) > 0 and Cells(curRow, left+1).value <> "" then 
      For column = 0 to usedColumnsCount-1 
       REM only look at columns that are in the "used" range 
       curCol = column+left 
       REM get the value/word that is in the cell 
       word = Cells(curRow,curCol).Value 
       REM add the column to the new excel file 
       outputWorksheet.Cells(outrow, curCol).Value = word' = Cells(curRow,curCol) 
      Next 
      outrow = outrow + 1 
     end if 
    Next 

    REM We are done with the current worksheet, release the memory 
    Set currentWorkSheet = Nothing 
Next 

objWorkbook.SaveAs("C:\Documents and Settings\kitchenjt\My Documents\Professional Development\input\CleanExcel.xls") 
objWorkbook.Close 
objFSOExcel.Quit 
objExcel.Workbooks(1).Close 
objExcel.Quit 

Set currentWorkSheet = Nothing 
REM We are done with the Excel object, release it from memory 
Set objExcel = Nothing 
+0

이 VBA에주의를 자동으로 얻을의 A와 입력 효율성이 떨어지는 변형입니다. –

+0

이 VBA 또는 VBScript입니까? 그들은 동일하지 않습니다 ... –

+0

VBScript는 내가 사용하고있는 것입니다. 대부분 내가 가져올 필요가있는 엑셀 파일이 끔찍한 헤더를 가지고있어 데이터를 올바르게 가져올 수 없다는 사실 때문에 필사적으로 행동합니다. – Jeremy

답변

2

그것을 기록하기 전에 텍스트로 출력 세포의 NumberFormat를 설정 : 당신은 DIM 문을 유형을 제공하지 않는 경우

Cells(curRow, curCol).NumberFormat = "@" 
REM get the value/word that is in the cell 
word = Cells(curRow,curCol).Value 
+0

셀에 대한 모든 메소드를 보여주는 참조가 있습니까? – Jeremy

+0

@ 제레미, Excel과 함께 제공되는 Excel 용 VBA 도움말 파일에 있습니다. 셀 서식을 선택한 다음 사용자 지정 탭을 보면 얻을 수있는 NumberFormat 값 (일부는 어쨌든)입니다. –

관련 문제