1. 程式人生 > >excel中vb 巨集指令碼的簡單使用

excel中vb 巨集指令碼的簡單使用

1.編輯快捷鍵去工作,如下:

Sub 顏色()
'
' 顏色 巨集
'
' 快捷鍵: Ctrl+b
'
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 5287936
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End Sub
Sub 欄位查詢()
'
' 欄位查詢 巨集
'
' 快捷鍵: Ctrl+n
'
    Dim a
    a = ActiveCell.Value
    Sheets("欄位級配置資訊").Select
    ActiveSheet.Range("$A$1:$T$38207").AutoFilter Field:=3, Criteria1:=Array(Right(a, Len(a) - 4), a), Operator:=xlFilterValues
    
End Sub

Sub 否()
'
' 否 巨集
'
' 快捷鍵: Ctrl+e
'
    ActiveCell.Value = "否"
    ActiveCell.Offset(0, 4).Value = "否"

End Sub

2.新增一批

Sub 巨集2()
'
' 巨集2 巨集
'
' 快捷鍵: Ctrl+n
' 功能:指定單元格值之前增加兩行,並將指定值賦值到新增的行的指定列

    Dim tableid, columnsid, flagid
    tableid = 2
    columnsid = 3
    flagid = 4
    
    For r = 1 To Sheets("Sheet1").UsedRange.Rows.Count
    If Worksheets(1).Cells(r, flagid).Value = 3 Then
        Worksheets(1).Cells(r, flagid).Select
        Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
        
        ' 表名
        Worksheets(1).Cells(r, tableid).Value = Worksheets(1).Cells(r + 2, tableid).Value
        Worksheets(1).Cells(r + 1, tableid).Value = Worksheets(1).Cells(r + 2, tableid).Value
        
        ' 欄位命名
        Worksheets(1).Cells(r, columnsid).Value = Worksheets(1).Cells(r + 2, columnsid).Value & "_TM"
        Worksheets(1).Cells(r + 1, columnsid).Value = Worksheets(1).Cells(r + 2, columnsid).Value & "_JOIN"
        r = r + 2
    End If
    Next
    
    
End Sub