找回密码
 注册

QQ登录

只需一步,快速开始

查看: 423|回复: 1

Some sample for VBA

[复制链接]
发表于 2012-5-4 15:47:21 | 显示全部楼层 |阅读模式
  1. Sub Remove_Range()
  2. '
  3. ' Remove_Range Macro
  4. '

  5. '
  6.     Application.Goto Reference:="Table_Query_from_SQL_Query"
  7.     Selection.ListObject.ListColumns(1).Delete
  8.     Selection.ListObject.ListColumns(1).Delete
  9.     Selection.ListObject.ListColumns(1).Delete
  10.     Selection.ListObject.ListColumns(1).Delete
  11.     Selection.ListObject.ListColumns(1).Delete
  12.     Selection.ListObject.ListColumns(1).Delete
  13.     Selection.ListObject.ListColumns(1).Delete
  14.     Selection.ListObject.ListColumns(1).Delete
  15.     Selection.ListObject.ListColumns(1).Delete
  16.     Selection.ListObject.ListColumns(1).Delete
  17.     Selection.ListObject.ListColumns(1).Delete
  18.     Selection.ListObject.ListColumns(1).Delete
  19.     Selection.ListObject.ListColumns(1).Delete
  20.     Selection.ListObject.ListColumns(1).Delete
  21.     Range("B3").Select
  22. End Sub
  23. Sub SQL_Query_Demo()
  24. '
  25. ' Macro2 Macro
  26. '

  27. '
  28.     Sheets.Add After:=Sheets(Sheets.Count)
  29.     Range("B2").Select
  30.     With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
  31.         "OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=acorntest;Use Procedure for Prepare=1;Auto T" _
  32.         , _
  33.         "ranslate=True;Packet Size=4096;Workstation ID=JZHANG-WIN7;Use Encryption for Data=False;Tag with column collation when possible=" _
  34.         , "False;Initial Catalog=tmpData"), Destination:=Range("$B$2")).QueryTable
  35.         .CommandType = xlCmdTable
  36.         .CommandText = Array("""tmpData"".""dbo"".""branch""")
  37.         .RowNumbers = False
  38.         .FillAdjacentFormulas = False
  39.         .PreserveFormatting = True
  40.         .RefreshOnFileOpen = False
  41.         .BackgroundQuery = True
  42.         .RefreshStyle = xlInsertDeleteCells
  43.         .SavePassword = False
  44.         .SaveData = True
  45.         .AdjustColumnWidth = True
  46.         .RefreshPeriod = 0
  47.         .PreserveColumnInfo = True
  48.         .SourceConnectionFile = _
  49.         "C:\Users\jzhang\Documents\My Data Sources\acorntest tmpData branch.odc"
  50.         .ListObject.DisplayName = "Table_acorntest_tmpData_branch"
  51.         .Refresh BackgroundQuery:=False
  52.     End With
  53.     Selection.AutoFilter
  54.     Selection.ClearFormats
  55.     ActiveSheet.ListObjects("Table_acorntest_tmpData_branch").TableStyle = ""
  56.     Selection.ListObject.QueryTable.Delete
  57. End Sub
  58. Sub Macro3()
  59. '
  60. ' Macro3 Copy data from selected Range
  61. '

  62. '
  63.     Application.Goto Reference:="Customer_Edit_Range"
  64.     Selection.Copy
  65.     Sheets("RecordControl").Select
  66.     Range("A5").Select
  67.     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
  68.         :=False, Transpose:=False
  69.     Range("A5").Select
  70. End Sub
  71. Sub Macro4()
  72. '
  73. ' Macro4 Name a Range
  74. '

  75. '
  76.     Sheets("RecordControl").Select
  77.     Application.CutCopyMode = False
  78.     ActiveWorkbook.Names.Add Name:="TestData", RefersToR1C1:= _
  79.         "=RecordControl!R5C1:R6C13"
  80.     Application.Goto Reference:="vDataRangeTL"
  81.     Application.Goto Reference:="TestData"
  82. End Sub
  83. Sub Macro5()
  84. '
  85. ' Macro5 Copy, Paste and Rename the range
  86. '

  87. '
  88.     Sheets("DataSource").Select
  89.     Range("B3:G6").Select
  90.     Selection.Copy
  91.     Sheets("RecordControl").Select
  92.     Range("A5").Select
  93.     ActiveSheet.Paste
  94.     Application.CutCopyMode = False
  95.     ActiveWorkbook.Names.Add Name:="TestData", RefersToR1C1:= _
  96.         "=RecordControl!R5C1:R8C6"
  97.     Range("F19").Select
  98.     Application.Goto Reference:="TestData"
  99.     Selection.Delete Shift:=xlUp
  100.     Range("A5").Select
  101. End Sub

  102. Sub Macro_copy()
  103. '
  104. ' Macro1 Macro
  105. ' Copy from one sheet in another workfile to another sheet Rename the Tag
  106. '

  107. '
  108.     Windows("Book1").Activate
  109.     Sheets("Sheet1").Name = "Source"
  110.     Sheets("Sheet2").Name = "Dest"
  111.     Application.CutCopyMode = False
  112.     Sheets("Source").Range("B2").Copy
  113.     Sheets("Dest").Select
  114.     ActiveSheet.Paste Destination:=Worksheets("Dest").Range("A2")
  115.     Sheets("Dest").Columns("A:A").EntireColumn.AutoFit
  116.     Application.CutCopyMode = False
  117.     Sheets("Source").Range("C2:N2").Copy
  118.     Sheets("Dest").Paste Destination:=Sheets("Dest").Range("B2")
  119.     'Sheets("Dest").Range("B2").Select
  120.     'ActiveSheet.Paste
  121.     Columns("B:B").EntireColumn.AutoFit
  122.     Columns("C:C").EntireColumn.AutoFit
  123.     Columns("D:D").EntireColumn.AutoFit
  124.     Columns("M:M").EntireColumn.AutoFit
  125.     Columns("L:L").EntireColumn.AutoFit
  126.     Columns("K:K").EntireColumn.AutoFit
  127.     Columns("J:J").EntireColumn.AutoFit
  128.     Columns("I:I").EntireColumn.AutoFit
  129.     Application.CutCopyMode = False
  130.     Sheets("Source").Range("B3:B4").Copy
  131.     'Application.CutCopyMode = False
  132.     'Selection.Copy
  133.     'Sheets("Dest").Range("A3").Select
  134.     Sheets("Dest").Paste Destination:=Sheets("Dest").Range("A3")
  135.     'Sheets("Source").Select
  136.     'Range("C3:N4").Select
  137.     Application.CutCopyMode = False
  138.     Sheets("Source").Range("C3:N4").Copy
  139.     Sheets("Dest").Range("B3").Select
  140.     Sheets("Dest").Paste Destination:=Sheets("Dest").Range("B3")
  141.     Dim myList, myCell, i
  142.     i = 1
  143.     Sheets("Dest").Range("A3").Select
  144.     'myList = Sheets("Dest").Range(Selection.End(xlToLeft), Selection.End(xlToRight)).Select
  145.     myList = Sheets("Dest").Range(Selection.End(xlUp), Selection.End(xlDown)).Select
  146.     For Each myCell In Selection
  147.         'myCell.Value = "i"
  148.         Windows("PERSONAL").Activate
  149.         
  150.         Sheets("Sheet1").Cells(i, 1).Value = myCell.Value
  151.         Windows("Book1").Activate
  152.         i = i + 1
  153.     Next
  154.     Sheets("Source").Select
  155. End Sub

  156. Sub Macro_Item_Process()
  157. '
  158. ' Micro to update report according to another reference list
  159. ' Copy data from one sheet in another another sheet and loop li to the end
  160. '

  161. '
  162.     Dim i, j, op_Code, s_Cust, s_Cls, s_Alloy, s_Form, s_Date, s_Rep, s_Inv, s_Ton, s_Moh
  163.     Windows("NV_Customer_Specific_Items").Activate
  164.     Dim myList, myCell
  165.     i = 1
  166.     Sheets("Inv").Select
  167.     Sheets("Inv").Range("B2").Select
  168.     'myList = Sheets("Dest").Range(Selection.End(xlToLeft), Selection.End(xlToRight)).Select
  169.     myList = Sheets("Inv").Range("B2", Selection.End(xlDown)).Select
  170.     For Each myCell In Selection
  171.         s_Cust = myCell.Value: s_Cls = myCell.Offset(0, 1).Value: s_Alloy = myCell.Offset(0, 2).Value
  172.         s_Form = myCell.Offset(0, 3).Value
  173.         s_Date = myCell.Offset(0, 4).Value: s_Inv = myCell.Offset(0, 5).Value
  174.         s_Ton = myCell.Offset(0, 6).Value
  175.         Sheets("Detail").Select
  176.         j = 2
  177.         Do
  178.            
  179.            j = j + 1
  180.         Loop While (j < 356) And (Sheets("Detail").Cells(j, 3).Value <> s_Cust Or _
  181.         Sheets("Detail").Cells(j, 5).Value <> s_Cls Or Sheets("Detail").Cells(j, 6).Value <> s_Alloy Or _
  182.         Sheets("Detail").Cells(j, 7).Value <> s_Form)
  183.         If j > 355 Then
  184.            op_Code = MsgBox("Customer: " + s_Cust + vbCrLf + "Class:   " + s_Cls + vbCrLf + _
  185.            "Alloy:    " + s_Alloy + vbCrLf + "Form:     " + s_Form + vbCrLf + "Continue ?", vbYesNo, "No Exist!")
  186.            If op_Code = vbNo Then
  187.               GoTo EndPro
  188.            End If
  189.         Else
  190.            MsgBox "Line:" & j & " found the item: " & Sheets("Detail").Cells(j, 3).Value & " |" & _
  191.                   Sheets("Detail").Cells(j, 5).Value & "|Alloy:" & Sheets("Detail").Cells(j, 6).Value & "|Form:" & _
  192.                   Sheets("Detail").Cells(j, 7).Value & vbCrLf & _
  193.                   "Data into:" & vbCrLf & _
  194.                   "|" & s_Cust & "|" & s_Cls & "|" & s_Alloy & "|" & s_Form & _
  195.                   "|" & s_Date & "|" & s_Inv & "|" & s_Ton
  196.         End If
  197.         
  198.         'Sheets("Sheet1").Cells(i, 1).Value = myCell.Value
  199.         Sheets("Inv").Select
  200.         i = i + 1
  201.     Next
  202.     'Sheets("Source").Select
  203.     'Windows("PERSONAL").Activate
  204. EndPro:
  205. End Sub

复制代码


 楼主| 发表于 2012-5-4 15:48:09 | 显示全部楼层
  1. Sub Macro_copy_all()
  2. '
  3. ' Macro_copy_all Macro
  4. '

  5. '
  6.     Range("A3:E8").Select
  7.     Selection.Copy
  8.     Range("A26").Select
  9.     ActiveSheet.Paste
  10. End Sub
  11. Sub Macro_Chart2()
  12. '
  13. ' Macro2 Macro: Apply 3 groups data to two horizen axies.
  14. '

  15. '
  16.     Range("E4").Select
  17.     ActiveCell.FormulaR1C1 = "=R4C[-2]"
  18.     Range("E4").Select
  19.     ActiveCell.FormulaR1C1 = "=SUM(R4C[-2]:R7C[-2])/4"
  20.     Selection.AutoFill Destination:=Range("E4:E7"), Type:=xlFillDefault
  21.     Range("E4:E7").Select
  22.     Range("A3:E7").Select
  23.     ActiveSheet.Shapes.AddChart.Select
  24.     ActiveChart.ChartType = xlColumnClustered
  25.     ActiveChart.SetSourceData Source:=Range("Sum!$A$3:$E$7")
  26.     ActiveChart.PlotArea.Select
  27.     ActiveChart.Axes(xlValue).MajorGridlines.Select
  28.     ActiveChart.ChartArea.Select
  29.     ActiveChart.SeriesCollection(1).Select
  30.     ActiveChart.SetSourceData
  31.     ActiveChart.SeriesCollection(4).Select
  32.     ActiveChart.SeriesCollection(4).ChartType = xlLine
  33.     ActiveChart.PlotArea.Select
  34.     ActiveChart.Axes(xlValue).MajorGridlines.Select
  35.     ActiveChart.Legend.Select
  36.     ActiveChart.SeriesCollection(1).Select
  37.     ActiveChart.SeriesCollection(1).AxisGroup = 2
  38.     ActiveSheet.ChartObjects("Chart 1").Activate
  39.     ActiveChart.SeriesCollection(1).Select
  40.     ActiveChart.PlotArea.Select
  41.     ActiveChart.SeriesCollection(1).Select
  42.     ActiveChart.SeriesCollection(1).Points(1).Select
  43.     ActiveChart.SeriesCollection(2).Select
  44.     ActiveChart.ChartGroups(1).GapWidth = 217
  45.     ActiveChart.ChartGroups(1).GapWidth = 101
  46.     ActiveChart.ChartGroups(1).GapWidth = 58
  47.     ActiveChart.ChartGroups(1).GapWidth = 46
  48.     ActiveChart.ChartGroups(1).Overlap = -41
  49.     ActiveChart.ChartGroups(1).Overlap = -54
  50.     ActiveChart.ChartGroups(1).Overlap = 22
  51.     ActiveChart.ChartGroups(1).Overlap = -14
  52.     ActiveChart.ChartGroups(1).Overlap = -43
  53.     ActiveChart.ChartGroups(1).Overlap = -62
  54.     ActiveChart.ChartGroups(1).Overlap = -75
  55.     ActiveChart.PlotArea.Select
  56.     ActiveChart.SeriesCollection(1).Select
  57.     ActiveChart.SeriesCollection(3).Select
  58.     ActiveChart.SeriesCollection(3).AxisGroup = 2
  59.     ActiveSheet.ChartObjects("Chart 1").Activate
  60.     ActiveChart.SeriesCollection(3).Select
  61.     With Selection.Format.Fill
  62.         .Visible = msoTrue
  63.         .ForeColor.ObjectThemeColor = msoThemeColorAccent1
  64.         .ForeColor.TintAndShade = 0
  65.         .ForeColor.Brightness = 0
  66.         .BackColor.ObjectThemeColor = msoThemeColorBackground1
  67.         .BackColor.TintAndShade = 0
  68.         .BackColor.Brightness = 0
  69.         .Patterned msoPattern5Percent
  70.     End With
  71.     With Selection.Format.Fill
  72.         .Visible = msoTrue
  73.         .Patterned msoPattern60Percent
  74.     End With
  75.     With Selection.Format.Fill
  76.         .Visible = msoTrue
  77.         .Patterned msoPattern60Percent
  78.     End With
  79.     With Selection.Format.Fill
  80.         .Visible = msoTrue
  81.         .ForeColor.ObjectThemeColor = msoThemeColorAccent1
  82.         .ForeColor.TintAndShade = 0
  83.         .ForeColor.Brightness = -0.25
  84.         .BackColor.ObjectThemeColor = msoThemeColorBackground1
  85.         .BackColor.TintAndShade = 0
  86.         .BackColor.Brightness = 0
  87.         .Patterned msoPattern60Percent
  88.     End With
  89.     ActiveChart.ChartGroups(2).Overlap = -47
  90.     ActiveChart.ChartGroups(2).Overlap = -69
  91.     ActiveChart.ChartGroups(2).Overlap = -80
  92.     ActiveChart.ChartGroups(2).GapWidth = 22
  93.     ActiveChart.ChartGroups(2).GapWidth = 73
  94.     ActiveChart.ChartGroups(2).GapWidth = 79
  95.     ActiveChart.ChartGroups(2).GapWidth = 113
  96.     ActiveChart.ChartGroups(2).GapWidth = 119
  97.     ActiveChart.ChartGroups(2).GapWidth = 151
  98.     ActiveChart.SeriesCollection(1).Select
  99.     ActiveChart.ChartGroups(2).GapWidth = 117
  100.     ActiveChart.ChartGroups(2).GapWidth = 73
  101.     ActiveChart.ChartGroups(2).GapWidth = 41
  102.     ActiveChart.ChartGroups(2).GapWidth = 25
  103.     ActiveChart.ChartGroups(2).Overlap = -89
  104.     ActiveChart.ChartGroups(2).Overlap = -100
  105.     ActiveChart.ChartGroups(2).Overlap = -86
  106.     ActiveChart.ChartGroups(2).Overlap = -100
  107.     ActiveChart.ChartGroups(2).GapWidth = 11
  108.     ActiveChart.ChartGroups(2).GapWidth = 51
  109.     ActiveChart.SeriesCollection(2).Select
  110.     ActiveChart.ChartGroups(1).GapWidth = 132
  111.     ActiveChart.ChartGroups(1).GapWidth = 158
  112.     ActiveChart.ChartGroups(1).GapWidth = 172
  113.     ActiveChart.ChartGroups(1).GapWidth = 180
  114.     ActiveChart.ChartGroups(1).GapWidth = 172
  115.     ActiveChart.ChartGroups(1).Overlap = -61
  116.     ActiveChart.ChartGroups(1).Overlap = -24
  117.     ActiveChart.ChartGroups(1).Overlap = -95
  118.     ActiveChart.ChartGroups(1).Overlap = -100
  119.     ActiveChart.ChartGroups(1).Overlap = 0
  120.     ActiveChart.ChartGroups(1).GapWidth = 166
  121.     ActiveChart.ChartGroups(1).GapWidth = 152
  122.     ActiveChart.ChartGroups(1).GapWidth = 140
  123.     ActiveChart.ChartGroups(1).GapWidth = 160
  124.     Range("H28").Select
  125. End Sub

  126. Sub Macro_Chart()
  127. '
  128. ' Macro_Chart Macro
  129. ' Thi sis to show to to create a Chart from an area
  130. '

  131. '
  132.     Range("A3:D7").Select
  133.     ActiveSheet.Shapes.AddChart.Select
  134.     ActiveChart.ChartType = xlColumnClustered
  135.     ActiveChart.SetSourceData Source:=Range("Sum!$A$3:$D$7")
  136.     ActiveChart.SeriesCollection(2).Select
  137.     ActiveChart.SeriesCollection(2).AxisGroup = 2
  138.     ActiveSheet.ChartObjects("Chart 4").Activate
  139.     ActiveChart.SeriesCollection(2).Select
  140.     ActiveChart.ChartGroups(2).GapWidth = 98
  141.     ActiveChart.ChartGroups(2).GapWidth = 174
  142.     ActiveChart.ChartGroups(2).GapWidth = 241
  143.     ActiveChart.ChartGroups(2).GapWidth = 268
  144.     ActiveChart.ChartGroups(2).GapWidth = 286
  145.     ActiveChart.ChartGroups(2).GapWidth = 334
  146.     ActiveChart.ChartGroups(2).Overlap = -32
  147.     ActiveChart.ChartGroups(2).Overlap = 43
  148.     ActiveChart.ChartGroups(2).Overlap = -15
  149.     With Selection.Format.Fill
  150.         .Visible = msoTrue
  151.         .ForeColor.RGB = RGB(0, 0, 0)
  152.         .BackColor.ObjectThemeColor = msoThemeColorBackground1
  153.         .BackColor.TintAndShade = 0
  154.         .BackColor.Brightness = 0
  155.         .Patterned msoPattern5Percent
  156.     End With
  157.     With Selection.Format.Fill
  158.         .Visible = msoTrue
  159.         .Patterned msoPatternNarrowHorizontal
  160.     End With
  161.     With Selection.Format.Fill
  162.         .Visible = msoTrue
  163.         .Patterned msoPatternNarrowHorizontal
  164.     End With
  165.     With Selection.Format.Fill
  166.         .Visible = msoTrue
  167.         .Patterned msoPatternDarkUpwardDiagonal
  168.     End With
  169.     With Selection.Format.Fill
  170.         .Visible = msoTrue
  171.         .Patterned msoPatternDarkUpwardDiagonal
  172.     End With
  173.     With Selection.Format.Fill
  174.         .Visible = msoTrue
  175.         .ForeColor.ObjectThemeColor = msoThemeColorAccent2
  176.         .ForeColor.TintAndShade = 0
  177.         .ForeColor.Brightness = -0.25
  178.         .BackColor.ObjectThemeColor = msoThemeColorBackground1
  179.         .BackColor.TintAndShade = 0
  180.         .BackColor.Brightness = 0
  181.         .Patterned msoPatternDarkUpwardDiagonal
  182.     End With
  183.     With Selection.Format.Fill
  184.         .Visible = msoTrue
  185.         .ForeColor.ObjectThemeColor = msoThemeColorAccent2
  186.         .ForeColor.TintAndShade = 0
  187.         .ForeColor.Brightness = -0.25
  188.         .BackColor.ObjectThemeColor = msoThemeColorAccent6
  189.         .BackColor.TintAndShade = 0
  190.         .BackColor.Brightness = 0.6000000238
  191.         .Patterned msoPatternDarkUpwardDiagonal
  192.     End With
  193.     With Selection.Format.Fill
  194.         .Visible = msoTrue
  195.         .ForeColor.ObjectThemeColor = msoThemeColorAccent5
  196.         .ForeColor.TintAndShade = 0
  197.         .ForeColor.Brightness = 0.400000006
  198.         .BackColor.ObjectThemeColor = msoThemeColorAccent6
  199.         .BackColor.TintAndShade = 0
  200.         .BackColor.Brightness = 0.6000000238
  201.         .Patterned msoPatternDarkUpwardDiagonal
  202.     End With
  203.     With Selection.Format.Fill
  204.         .Visible = msoTrue
  205.         .ForeColor.ObjectThemeColor = msoThemeColorAccent5
  206.         .ForeColor.TintAndShade = 0
  207.         .ForeColor.Brightness = 0.400000006
  208.         .BackColor.ObjectThemeColor = msoThemeColorAccent6
  209.         .BackColor.TintAndShade = 0
  210.         .BackColor.Brightness = 0
  211.         .Patterned msoPatternDarkUpwardDiagonal
  212.     End With
  213.     ActiveChart.PlotArea.Select
  214.     Range("E3").Select
  215.     ActiveCell.FormulaR1C1 = "Average"
  216.     Range("E4").Select
  217. End Sub

复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-6-14 06:07 , Processed in 0.017776 second(s), 16 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表