Macro ให้ยีดหยุ่นตามจำนวนแถวที่มี
Posted: Thu Jun 18, 2020 11:02 am
ผมได้ลองทำ macro เพื่อใส่สูตรและ copy แต่เวลา copy แล้วมันจะจำจำนวนแถวครั้งแรก (ในตัวอย่างคือ 28 แถว) ผมต้องปรับสูตรอย่างไรเพื่อให้การ copy สูตร ได้ตามจำนวนแถวที่มีจริงครับ ในกรณีที่มี มากกว่า หรือ น้อยกว่า 28 แถว
Sub Test()
'
' Test Macro
'
'
Columns("M:M").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("M1").Select
ActiveCell.FormulaR1C1 = "GL Acct_1"
Range("M2").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-1]=102114170,620020329,IF(RC[-1]=110020010,518181010,IF(RC[-1]=110025020,620020312,IF(RC[-1]=110030050,630900050,IF(RC[-1]=149999020,630150900,IF(RC[-1]=149999030,630170010,IF(RC[-1]=249210010,516089013,RC[-1])))))))"
Range("M2").Copy
Selection.AutoFill Destination:=Range("M2:M28")
Range("M2:M28").Select
End Sub
Sub Test()
'
' Test Macro
'
'
Columns("M:M").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("M1").Select
ActiveCell.FormulaR1C1 = "GL Acct_1"
Range("M2").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-1]=102114170,620020329,IF(RC[-1]=110020010,518181010,IF(RC[-1]=110025020,620020312,IF(RC[-1]=110030050,630900050,IF(RC[-1]=149999020,630150900,IF(RC[-1]=149999030,630170010,IF(RC[-1]=249210010,516089013,RC[-1])))))))"
Range("M2").Copy
Selection.AutoFill Destination:=Range("M2:M28")
Range("M2:M28").Select
End Sub