#1
by seky2 » Mon Aug 12, 2013 5:08 pm
เรียนอาจารย์ที่เคารพทุกท่านครับ
มาโครนี้ เป็นการนำเข้า text file ครับ ซึ่ง file ต้นฉบับ จะมี ชื่อไฟล์ที่ลงท้ายด้วยรหัสจังหวัด เช่น UC7600.txt, UC7500.txt ตอนเขียนนำเข้าไม่มีปัญหาครับ ปัญหาอยู่ตรงที่เมื่อนำเข้าแล้ว excel ใช้ชื่อ sheet ตามชื่อ text file
เวลาอ้าง sheet เพื่อทำการ sort data จะทำอย่างไรให้เข้าถึง sheet ได้ครับ เช่น ActiveWorkbook.Worksheets("UC7600").Sort.SortFields.Clear เวลานำเข้า text file จากจังหวัดรหัส 7500 ตรง.Worksheets("UC7600") ควรจะอ้างอย่างไรครับ
ขอบพระคุณมากครับ
Sub Macro1()
Application.ScreenUpdating = False
Application.DisplayFullScreen = False
Application.DisplayAlerts = False
ChDir "D:\UC_TXT"
Workbooks.OpenText Filename:="D:\UC_TXT\UC7*.txt", Origin:=874, StartRow _
:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=True, Comma:=False, _
Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array( _
3, 1), Array(4, 4), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 4), Array(10 _
, 4), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array(16, 1), _
Array(17, 1)), TrailingMinusNumbers:=True
Application.DisplayAlerts = False
'à»ÅÕ蹪×èÍ Sheet
Sheets("UC7600").Select
Sheets("UC7600").Name = "UC7-XXX"
ActiveWorkbook.SaveAs Filename:="D:\UC_TXT\UC7-XXX.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "PID"
Range("B1").Select
ActiveCell.FormulaR1C1 = "¤Ó¹Ó"
Range("C1").Select
ActiveCell.FormulaR1C1 = "ª×èÍ"
Range("D1").Select
ActiveCell.FormulaR1C1 = "¹ÒÁÊ¡ØÅ"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Çѹà¡Ô´"
Range("F1").Select
ActiveCell.FormulaR1C1 = "à¾È"
Range("G1").Select
ActiveCell.FormulaR1C1 = "ÃËÑÊ·ÕèÍÂÙè"
Range("H1").Select
ActiveCell.FormulaR1C1 = "ÊÔ·¸Ôì"
Range("I1").Select
ActiveCell.FormulaR1C1 = "ÇѹàÃÔèÁ"
Range("J1").Select
ActiveCell.FormulaR1C1 = "ÇѹËÁ´"
Range("K1").Select
ActiveCell.FormulaR1C1 = "þËÅÑ¡"
Range("L1").Select
ActiveCell.FormulaR1C1 = "þÃͧ"
Range("M1").Select
ActiveCell.FormulaR1C1 = "ÊÔ·¸ÔìÂèÍÂ"
Range("N1").Select
ActiveCell.FormulaR1C1 = "¨Ç"
Range("O1").Select
ActiveCell.FormulaR1C1 = "àÅ¢ºÑµÃ"
Range("P1").Select
ActiveCell.FormulaR1C1 = "ÍÒÂØ"
Range("Q1").Select
ActiveCell.FormulaR1C1 = "þËÅÑ¡2"
Range("R1").Select
ActiveCell.FormulaR1C1 = "y"
Range("S1").Select
ActiveCell.FormulaR1C1 = "m"
Range("T1").Select
ActiveCell.FormulaR1C1 = "d"
Range("U1").Select
ActiveCell.FormulaR1C1 = "dmy"
Range("V1").Select
ActiveCell.FormulaR1C1 = "dmy543"
Range("W1").Select
ActiveCell.FormulaR1C1 = "9/30/2013"
Range("A:A,O:O").Select
Selection.NumberFormat = "0"
Range("A1:W1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
'àÅ×Í¡á¶ÇÊØ´·éÒÂ
'á¡»Õà¡Ô´
Range("Q1").Select
Range([R2], [Q1048576].End(xlUp).Offset(0, 1)).Select
Selection.FormulaR1C1 = "=LEFT(RC[-13],4)"
'á¡à´×͹à¡Ô´
Range("R1").Select
Range([S2], [R1048576].End(xlUp).Offset(0, 1)).Select
Selection.FormulaR1C1 = "=MID(RC[-14],5,2)"
'á¡Çѹà¡Ô´
Range("S1").Select
Range([T2], [S1048576].End(xlUp).Offset(0, 1)).Select
Selection.FormulaR1C1 = "=RIGHT(RC[-15],2)"
Columns("T:T").Select
'á·¹¤èÒ 00
Selection.Replace What:="00", Replacement:="1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Columns("S:S").Select
Range("S2").Activate
Selection.Replace What:="00", Replacement:="07", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("T1").Select
Range([U2], [T1048576].End(xlUp).Offset(0, 1)).Select
Selection.FormulaR1C1 = "=RC[-1]&""/""&RC[-2]&""/""&RC[-3]"
Range([U2], [U1048576].End(xlUp).Offset(0, 0)).Select
Selection.Copy
Range("U2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("T1").Select
Range([U2], [T1048576].End(xlUp).Offset(0, 1)).Select
Selection.Replace What:="00/00/", Replacement:="01/07/", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="00/", Replacement:="01/", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("Q1").Select
Range([R2], [Q1048576].End(xlUp).Offset(0, 3)).Select
Selection.Copy
Range("R2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("U1").Select
Range([V2], [U1048576].End(xlUp).Offset(0, 1)).Select
Selection.FormulaR1C1 = "=DATE(YEAR(RC[-1])-543,MONTH(RC[-1]),DAY(RC[-1]))"
Range("V1").Select
Range([W2], [V1048576].End(xlUp).Offset(0, 1)).Select
Selection.FormulaR1C1 = _
"=DATEDIF(RC[-1],R1C23,""Y"")&"" »Õ ""&DATEDIF(RC[-1],R1C23,""YM"")&"" à´×͹ ""&DATEDIF(RC[-1],R1C23+1,""MD"")&"" Çѹ"""
Range("A1:W1").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
'á»Å§ÃËÑÊ˹èǺÃÔ¡ÒÃ
Range("K1").Select
Range([L2], [K1048576].End(xlUp).Offset(0, 1)).Select
Selection.NumberFormat = "00000"
'á·¹¤èÒ¤Ó¹Ó à¾È
Range("A1").Select
Range([B2], [A1048576].End(xlUp).Offset(0, 1)).Select
Selection.Replace What:="1", Replacement:="¹ÒÂ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="3", Replacement:="¹ÒÂ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="2", Replacement:="¹.Ê.", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="4", Replacement:="¹.Ê.", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="5", Replacement:="¹Ò§", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("E1").Select
Range([F2], [E1048576].End(xlUp).Offset(0, 1)).Select
Selection.Replace What:="1", Replacement:="ªÒÂ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="2", Replacement:="ËÔ§", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Select
ActiveWorkbook.Worksheets("UC7600").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("UC7600").Sort.SortFields.Add Key:=Range( _
"V2:V184608"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("UC7600").Sort.SortFields.Add Key:=Range( _
"K2:K184608"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("UC7600").Sort.SortFields.Add Key:=Range( _
"L2:L184608"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("UC7600").Sort.SortFields.Add Key:=Range( _
"C2:C184608"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("UC7600").Sort.SortFields.Add Key:=Range( _
"G2:G184608"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("UC7600").Sort
.SetRange Range("A1:W184608")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Columns("A:W").Select
Columns("A:W").EntireColumn.AutoFit
Range("U2").Select
ActiveWorkbook.Save
MsgBox "â»Ãá¡ÃÁàÃÕ§ÍÒÂØãËéàº×éͧµé¹áÅéÇ" & vbCrLf & _
"¡ÃسÒá¡éä¢ Error ¢Í§Çѹà¡Ô´ 㹤ÍÅÑÁ¹ì U Ẻ manual ¡è͹¨Ñ´àÃÕ§ÅӴѺµÒÁÍÒÂØãËÁèÍÕ¡¤ÃÑé§ !!!", vbInformation + vbOKOnly, "¡ÒùÓà¢éÒ¢éÍÁÙÅÊÓàÃç¨"
End Sub
เรียนอาจารย์ที่เคารพทุกท่านครับ
มาโครนี้ เป็นการนำเข้า text file ครับ ซึ่ง file ต้นฉบับ จะมี ชื่อไฟล์ที่ลงท้ายด้วยรหัสจังหวัด เช่น UC7600.txt, UC7500.txt ตอนเขียนนำเข้าไม่มีปัญหาครับ ปัญหาอยู่ตรงที่เมื่อนำเข้าแล้ว excel ใช้ชื่อ sheet ตามชื่อ text file
เวลาอ้าง sheet เพื่อทำการ sort data จะทำอย่างไรให้เข้าถึง sheet ได้ครับ เช่น ActiveWorkbook.Worksheets("UC7600").Sort.SortFields.Clear เวลานำเข้า text file จากจังหวัดรหัส 7500 ตรง.Worksheets("UC7600") ควรจะอ้างอย่างไรครับ
ขอบพระคุณมากครับ
Sub Macro1()
Application.ScreenUpdating = False
Application.DisplayFullScreen = False
Application.DisplayAlerts = False
ChDir "D:\UC_TXT"
Workbooks.OpenText Filename:="D:\UC_TXT\UC7*.txt", Origin:=874, StartRow _
:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=True, Comma:=False, _
Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array( _
3, 1), Array(4, 4), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 4), Array(10 _
, 4), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array(16, 1), _
Array(17, 1)), TrailingMinusNumbers:=True
Application.DisplayAlerts = False
'à»ÅÕ蹪×èÍ Sheet
Sheets("UC7600").Select
Sheets("UC7600").Name = "UC7-XXX"
ActiveWorkbook.SaveAs Filename:="D:\UC_TXT\UC7-XXX.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "PID"
Range("B1").Select
ActiveCell.FormulaR1C1 = "¤Ó¹Ó"
Range("C1").Select
ActiveCell.FormulaR1C1 = "ª×èÍ"
Range("D1").Select
ActiveCell.FormulaR1C1 = "¹ÒÁÊ¡ØÅ"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Çѹà¡Ô´"
Range("F1").Select
ActiveCell.FormulaR1C1 = "à¾È"
Range("G1").Select
ActiveCell.FormulaR1C1 = "ÃËÑÊ·ÕèÍÂÙè"
Range("H1").Select
ActiveCell.FormulaR1C1 = "ÊÔ·¸Ôì"
Range("I1").Select
ActiveCell.FormulaR1C1 = "ÇѹàÃÔèÁ"
Range("J1").Select
ActiveCell.FormulaR1C1 = "ÇѹËÁ´"
Range("K1").Select
ActiveCell.FormulaR1C1 = "þËÅÑ¡"
Range("L1").Select
ActiveCell.FormulaR1C1 = "þÃͧ"
Range("M1").Select
ActiveCell.FormulaR1C1 = "ÊÔ·¸ÔìÂèÍÂ"
Range("N1").Select
ActiveCell.FormulaR1C1 = "¨Ç"
Range("O1").Select
ActiveCell.FormulaR1C1 = "àÅ¢ºÑµÃ"
Range("P1").Select
ActiveCell.FormulaR1C1 = "ÍÒÂØ"
Range("Q1").Select
ActiveCell.FormulaR1C1 = "þËÅÑ¡2"
Range("R1").Select
ActiveCell.FormulaR1C1 = "y"
Range("S1").Select
ActiveCell.FormulaR1C1 = "m"
Range("T1").Select
ActiveCell.FormulaR1C1 = "d"
Range("U1").Select
ActiveCell.FormulaR1C1 = "dmy"
Range("V1").Select
ActiveCell.FormulaR1C1 = "dmy543"
Range("W1").Select
ActiveCell.FormulaR1C1 = "9/30/2013"
Range("A:A,O:O").Select
Selection.NumberFormat = "0"
Range("A1:W1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
'àÅ×Í¡á¶ÇÊØ´·éÒÂ
'á¡»Õà¡Ô´
Range("Q1").Select
Range([R2], [Q1048576].End(xlUp).Offset(0, 1)).Select
Selection.FormulaR1C1 = "=LEFT(RC[-13],4)"
'á¡à´×͹à¡Ô´
Range("R1").Select
Range([S2], [R1048576].End(xlUp).Offset(0, 1)).Select
Selection.FormulaR1C1 = "=MID(RC[-14],5,2)"
'á¡Çѹà¡Ô´
Range("S1").Select
Range([T2], [S1048576].End(xlUp).Offset(0, 1)).Select
Selection.FormulaR1C1 = "=RIGHT(RC[-15],2)"
Columns("T:T").Select
'á·¹¤èÒ 00
Selection.Replace What:="00", Replacement:="1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Columns("S:S").Select
Range("S2").Activate
Selection.Replace What:="00", Replacement:="07", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("T1").Select
Range([U2], [T1048576].End(xlUp).Offset(0, 1)).Select
Selection.FormulaR1C1 = "=RC[-1]&""/""&RC[-2]&""/""&RC[-3]"
Range([U2], [U1048576].End(xlUp).Offset(0, 0)).Select
Selection.Copy
Range("U2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("T1").Select
Range([U2], [T1048576].End(xlUp).Offset(0, 1)).Select
Selection.Replace What:="00/00/", Replacement:="01/07/", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="00/", Replacement:="01/", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("Q1").Select
Range([R2], [Q1048576].End(xlUp).Offset(0, 3)).Select
Selection.Copy
Range("R2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("U1").Select
Range([V2], [U1048576].End(xlUp).Offset(0, 1)).Select
Selection.FormulaR1C1 = "=DATE(YEAR(RC[-1])-543,MONTH(RC[-1]),DAY(RC[-1]))"
Range("V1").Select
Range([W2], [V1048576].End(xlUp).Offset(0, 1)).Select
Selection.FormulaR1C1 = _
"=DATEDIF(RC[-1],R1C23,""Y"")&"" »Õ ""&DATEDIF(RC[-1],R1C23,""YM"")&"" à´×͹ ""&DATEDIF(RC[-1],R1C23+1,""MD"")&"" Çѹ"""
Range("A1:W1").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
'á»Å§ÃËÑÊ˹èǺÃÔ¡ÒÃ
Range("K1").Select
Range([L2], [K1048576].End(xlUp).Offset(0, 1)).Select
Selection.NumberFormat = "00000"
'á·¹¤èÒ¤Ó¹Ó à¾È
Range("A1").Select
Range([B2], [A1048576].End(xlUp).Offset(0, 1)).Select
Selection.Replace What:="1", Replacement:="¹ÒÂ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="3", Replacement:="¹ÒÂ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="2", Replacement:="¹.Ê.", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="4", Replacement:="¹.Ê.", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="5", Replacement:="¹Ò§", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("E1").Select
Range([F2], [E1048576].End(xlUp).Offset(0, 1)).Select
Selection.Replace What:="1", Replacement:="ªÒÂ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="2", Replacement:="ËÔ§", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Select
ActiveWorkbook.Worksheets("UC7600").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("UC7600").Sort.SortFields.Add Key:=Range( _
"V2:V184608"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("UC7600").Sort.SortFields.Add Key:=Range( _
"K2:K184608"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("UC7600").Sort.SortFields.Add Key:=Range( _
"L2:L184608"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("UC7600").Sort.SortFields.Add Key:=Range( _
"C2:C184608"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("UC7600").Sort.SortFields.Add Key:=Range( _
"G2:G184608"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("UC7600").Sort
.SetRange Range("A1:W184608")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Columns("A:W").Select
Columns("A:W").EntireColumn.AutoFit
Range("U2").Select
ActiveWorkbook.Save
MsgBox "â»Ãá¡ÃÁàÃÕ§ÍÒÂØãËéàº×éͧµé¹áÅéÇ" & vbCrLf & _
"¡ÃسÒá¡éä¢ Error ¢Í§Çѹà¡Ô´ 㹤ÍÅÑÁ¹ì U Ẻ manual ¡è͹¨Ñ´àÃÕ§ÅӴѺµÒÁÍÒÂØãËÁèÍÕ¡¤ÃÑé§ !!!", vbInformation + vbOKOnly, "¡ÒùÓà¢éÒ¢éÍÁÙÅÊÓàÃç¨"
End Sub