2016-08-10 2 views
0

고정 열 너비로 Excel로 내보내는 방법에 대해 질문하고 싶습니다. 열의 너비는 문자 길이에 따라 달라 지므로 헤더 값을 기준으로Excel 열 너비

Sub loading() 
' 
' loading Macro 

' 
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;file-path\text.txt", _ 
     Destination:=Range("A6")) 
     .Name = "CarSumm" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .TextFilePromptOnRefresh = False 
     .TextFilePlatform = xlWindows 
     .TextFileStartRow = 1 
     .TextFileParseType = xlDelimited 
     .TextFileTextQualifier = xlTextQualifierDoubleQuote 
     .TextFileConsecutiveDelimiter = False 
     .TextFileTabDelimiter = False 
     .TextFileSemicolonDelimiter = False 
     .TextFileCommaDelimiter = False 
     .TextFileSpaceDelimiter = False 
     .TextFileOtherDelimiter = "|" 
     .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
     .Refresh BackgroundQuery:=False 
    End With 
    ActiveWindow.ScrollColumn = 3 
    ActiveWindow.ScrollColumn = 6 
    ActiveWindow.ScrollColumn = 9 
End Sub 

답변

0
U may Try below code before exporting, U can also increase or decrease 1.08 static value if not getting desire output 

intRowsCount = ActiveSheet.UsedRange.Columns.count 
Set objCells = ActiveSheet.Cells 
For c = 1 To intRowsCount 
    objCells(1, c).ColumnWidth = 1.08 * Len(objCells(1, c).Value) 
Next 
+0

감사합니다. btw. 어디에서 매크로를 추가해야합니까? 새로운 것을 매크로로. 난 그냥 우리의 소스 코드에 사용되는 것들을 복사하여 붙여 넣습니다. – noob

0

'변경 열 너비 : 엑셀 모두와 매크로

엑셀 절차 : 매크로

DEFINE VARIABLE h-excel AS COM-HANDLE NO-UNDO. 
DEFINE VARIABLE h-sheet AS COM-HANDLE. 
DEFINE VAR w-invname AS CHAR INITIAL "file-path\excel.xls". 

CREATE "Excel.Application" h-excel. 

h-sheet = h-excel:Workbooks:OPEN (w-invname,,FALSE,,,,,,,,,,FALSE) NO-ERROR. 
h-excel:visible = true. 

h-excel:Cells:Select. 

h-excel:Run("loading"). 

/*h-excel:Range("A" + STRING(5)):VALUE = "Date Covered " + STRING(fifr) + " - " + STRING(fito).*/ 

h-excel:Range("A6"):Select. 

RELEASE OBJECT h-sheet. 
RELEASE OBJECT h-excel. 

END PROCEDURE. 

엑셀 여기

내 샘플 코드 len intRowsCount = ActiveSheet.UsedRange.Columns.Count 세트 objCells = ActiveSheet.Cells 들어 C = 1에 intRowsCount objCells (1, c) = 1.08 * .ColumnWidth 렌 (objCells (1, c) .Value) 다음 '수출 활성 시트 시트 (ActiveSheet.Name) .Copy 통합 문서 (2) .Activate 세트 objWS = 통합 문서 (2) .Worksheets (1) 응답에 대한 objWS.Select

'Save new worksheet with new workbookname 
ActiveWorkbook.SaveAs Filename:="FilePath.xlsx" _ 
    , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False 
Workbooks(2).Save 
Workbooks(2).Close