Excel 文件复制操作vba代码

  1. worksheet 的代码  
  1. Const SourceFiledConfigStart As Integer = 2  
  2. Const SourceFiledConfigEnd As Integer = 27  
  3. Const SourceFiledDefaultStart As Integer = 41  
  4. Const SourceFiledDefaultEnd As Integer = 46  
  5.   
  6.   
  7.   
  8. Const usersheetname As String = "数据"  
  9. Const RefSheetname As String = "配置"  
  10.   
  11. Public sourcefilename As String  
  12. Public sourcefilepath As String  
  13. Dim index_Col As Integer  
  14. Dim SUBPATH As Object  
  15. Dim FieldExists As Boolean  
  16. Dim TargetColumnArray() As String  
  17. Dim SourceColumnArray() As String  
  18.   
  19.   
  20. Private Function CopyField(index_Object As Integer, index_Source As Integer)  
  21.     Dim iColumnIndex As Integer  
  22.     iColumnIndex = index_Source - 1  
  23.       
  24.      ActiveWorkbook.Sheets(1).Range("A2").Select  
  25.      Selection.Offset(0, iColumnIndex).Select  
  26.      ActiveWorkbook.Sheets(1).Range(Selection, Selection.End(xlDown)).Select  
  27.       
  28.      Selection.Copy  
  29.      ThisWorkbook.Activate  
  30.      Sheets(usersheetname).Select  
  31.      Sheets(usersheetname).Range("A2").Select  
  32.     iColumnIndex = index_Object - 1  
  33.     Selection.Offset(0, iColumnIndex).Select  
  34.     ActiveSheet.Paste  
  35. End Function  
  36.       
  37.   
  38. '用于记录源表列名所在的列  
  39. Private Function CopyField2(index_Object As Integer, index_Source As Integer)  
  40.   
  41.     Dim row As Integer  
  42.     Dim iRows As Integer  
  43.   
  44.     iRows = ActiveWorkbook.Sheets(1).Cells(1, index_Source).CurrentRegion.Rows.Count  
  45.     '列拷贝  
  46.       
  47.     For row = 2 To iRows  
  48.          
  49.         ThisWorkbook.Sheets(usersheetname).Cells(row, index_Object) = ActiveWorkbook.Sheets(1).Cells(row, index_Source).Value()  
  50.           
  51.     Next row  
  52.       
  53. End Function  
  54.   
  55. Private Function IndexCol(colName As String)  
  56.     Dim iColumns As Integer  
  57.     Dim column As Integer  
  58.     iColumns = ActiveWorkbook.Sheets(1).UsedRange.Columns.Count  
  59.       
  60.     For column = 1 To iColumns  
  61.         If ActiveWorkbook.Sheets(1).Cells(1, column) = colName Then  
  62.             index_Col = column  
  63.             Exit For  
  64.         End If  
  65.     Next column  
  66.       
  67. End Function  
  68.   
  69. Private Function DataSheetClear()  
  70.     '清除第二行开始的数据  
  71.     Dim rowstart As Integer  
  72.     Dim iColumns As Integer  
  73.     rowend = Sheets(usersheetname).UsedRange.Rows.Count  
  74.     rowstart = 2  
  75.     If rowend < 2 Then  
  76.         Exit Function  
  77.     End If  
  78.     Sheets(usersheetname).Range("A" & rowstart, "A" & rowend).EntireRow.Delete  
  79. End Function  
  80.   
  81. Function ExcelColumnNameConvert(ByVal r)  
  82. If r Like "[A-Z]*" Then ExcelColumnNameConvert = Range(r & 1).column  
  83. If r Like "#*" And r > 0 And r <= 256 Then ExcelColumnNameConvert = Split(Cells(1, r).Address, "$")(1)  
  84. End Function  
  85.   
  86. Private Function SetField(index_Object As Integer, defaultValue As Integer)  
  87.     Dim iRows As Integer  
  88.     Dim AString As String  
  89.     Dim AString2 As String  
  90.     Dim AString3 As String  
  91.     Dim AStartRow As Integer  
  92.        
  93.     iRows = ThisWorkbook.Sheets(usersheetname).Cells(1, index_Object).CurrentRegion.Rows.Count  
  94.     AString = ExcelColumnNameConvert(index_Object)  
  95.     AStartRow = 2  
  96.     AString2 = AString & AStartRow  
  97.     AString3 = AString & iRows  
  98.     AString = AString2 & ":" & AString3  
  99.   
  100.        
  101.      ThisWorkbook.Activate  
  102.      Sheets(usersheetname).Select  
  103.      Sheets(usersheetname).Range(AString2).Select  
  104.      ActiveCell.FormulaR1C1 = defaultValue  
  105.     Selection.AutoFill Destination:=Sheets(usersheetname).Range(AString), Type:=xlFillDefault  
  106. End Function  
  107.   
  108.   
  109. Private Function SetField2(index_Object As Integer, defaultValue As Integer)  
  110.   
  111.     Dim row As Integer  
  112.     Dim iRows As Integer  
  113.   
  114.     iRows = ThisWorkbook.Sheets(usersheetname).Cells(1, index_Object).CurrentRegion.Rows.Count  
  115.     '列拷贝  
  116.       
  117.     For row = 2 To iRows  
  118.          
  119.         ThisWorkbook.Sheets(usersheetname).Cells(row, index_Object) = defaultValue  
  120.           
  121.     Next row  
  122.       
  123. End Function  
  124.   
  125.   
  126. Private Function SetDefaultFieldData()  
  127.     For iRow = SourceFiledDefaultStart To SourceFiledDefaultEnd  
  128.         Call SetField(ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 9), ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 2))  
  129.     Next  
  130. End Function  
  131.   
  132.   
  133.   
  134.   
  135. Private Function CopyFieldData()  
  136.     Set SUBPATH = CreateObject("vbscript.regexp")  
  137.     With SUBPATH  
  138.         .Global = True  
  139.         .Pattern = ".*\\"  
  140.     End With  
  141.     sourcefilepath = ThisWorkbook.Sheets(RefSheetname).Cells(1, 5)  
  142.     soucefilename = SUBPATH.Replace(sourcefilepath, "")  
  143.     FileExists = Exist(soucefilename)  
  144.     If Not FileExists Then  
  145.         Workbooks.Open sourcefilepath  
  146.     End If  
  147.     Windows(soucefilename).Activate  
  148.       
  149.     For iRow = SourceFiledConfigStart To SourceFiledConfigEnd  
  150.         Windows(soucefilename).Activate  
  151.         Call CopyField(ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 9), ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 3))  
  152.     Next  
  153. End Function  
  154.   
  155. Private Function GetTargetColumns()  
  156.     Dim iCount As Integer  
  157.     Dim strValue As String  
  158.     Dim str As String  
  159.     Set SUBPATH = CreateObject("vbscript.regexp")  
  160.     With SUBPATH  
  161.         .Global = True  
  162.         .Pattern = ".*\\"  
  163.     End With  
  164.     ThisWorkbook.Activate  
  165.     If ThisWorkbook.Sheets(RefSheetname).Cells(1, 5) = "" Then  
  166.         MsgBox "没有选择Excel文件", vbOKOnly, "配置错误"  
  167.         Exit Function  
  168.     End If  
  169.     sourcefilepath = ThisWorkbook.Sheets(RefSheetname).Cells(1, 5)  
  170.     soucefilename = SUBPATH.Replace(sourcefilepath, "")  
  171.     FileExists = Exist(soucefilename)  
  172.     If Not FileExists Then  
  173.         Workbooks.Open sourcefilepath  
  174.     End If  
  175.     Windows(soucefilename).Activate  
  176.       
  177.     iCount = ActiveWorkbook.Sheets(1).Cells(1, 1).CurrentRegion.Columns.Count  
  178.     strValue = ""  
  179.     str = ""  
  180.     For iColumn = 1 To iCount - 1  
  181.         str = Trim(ActiveWorkbook.Sheets(1).Cells(1, iColumn).Value)  
  182.         strValue = strValue + str + ","  
  183.     Next  
  184.     strValue = strValue + Trim(ActiveWorkbook.Sheets(1).Cells(1, iCount).Value)  
  185.         
  186.     InitCandidateValue (strValue)  
  187.     TargetColumnArray = Split(strValue, ",")  
  188. End Function  
  189.   
  190.   
  191. Private Sub CommandButton1_Click()  
  192.     Dim FileExists As Boolean  
  193.     FieldExists = Flase  
  194.     '清除基站信息检测模板数据和检测报告  
  195.     Call DataSheetClear  
  196.     Call GetTargetColumns  
  197.     '处理数据  
  198.     FieldExists = CheckTargetFields  
  199.     If Not FieldExists Then  
  200.         MsgBox "目标文件检查失败"  
  201.         Exit Sub  
  202.     End If  
  203.     FieldExists = CheckSourceFields  
  204.     If Not FieldExists Then  
  205.         MsgBox "本文件数据sheet检查失败"  
  206.         Exit Sub  
  207.     End If  
  208.     Sheets(RefSheetname).Select  
  209.     MsgBox "检查完成"  
  210. End Sub  
  211.   
  212. Private Function CheckSourceFields() As Boolean  
  213.   
  214.     Dim iRow As Integer  
  215.     Dim iFieldIndex As Integer  
  216.     Dim sFieldName As String  
  217.       
  218.     Dim iCols As Integer  
  219.     Dim iCol As Integer  
  220.     Dim errFields As String  
  221.     Dim errMSG As String  
  222.       
  223.     errMSG = ""  
  224.     errFields = ""  
  225.     getProjectDColumn  
  226.       
  227.     For iRow = SourceFiledConfigStart To SourceFiledConfigEnd  
  228.         For iFieldIndex = 0 To UBound(SourceColumnArray)  
  229.             sFieldName = SourceColumnArray(iFieldIndex)  
  230.              If sFieldName = Trim(ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 1)) Then  
  231.                 ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 9) = iFieldIndex + 1  
  232.                 Exit For  
  233.              End If  
  234.         Next  
  235.         If Trim(ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 9)) = "" Then  
  236.             errFields = errFields & ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 1) & " "  
  237.         End If  
  238.     Next  
  239.          
  240.   
  241.     For iRow = SourceFiledDefaultStart To SourceFiledDefaultEnd  
  242.         For iFieldIndex = 0 To UBound(SourceColumnArray)  
  243.             sFieldName = SourceColumnArray(iFieldIndex)  
  244.              If sFieldName = Trim(ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 1)) Then  
  245.                 ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 9) = iFieldIndex + 1  
  246.                 Exit For  
  247.              End If  
  248.         Next  
  249.         If Trim(ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 9)) = "" Then  
  250.             errFields = errFields & ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 1) & " "  
  251.         End If  
  252.     Next  
  253.       
  254.          
  255.     If errFields <> "" Then  
  256.         ThisWorkbook.Activate  
  257.         errMSG = "工程参数表对应列名: " & errFields & "不存在,请检查输入是否正确!"  
  258.         MsgBox errMSG, vbOKOnly, "字段配置错误"  
  259.         CheckSourceFields = False  
  260.   
  261.     Else  
  262.       CheckSourceFields = True  
  263.     End If  
  264.         
  265.         
  266. End Function  
  267.   
  268. Private Sub CommandButton2_Click()  
  269.     Dim filename As Variant  
  270.      '打开文件对话框返回的文件名,是一个全路径文件名,其值也可能是False,因此类型为Variant  
  271.     Dim sFileName As String                         '从FileName中提取的文件名  
  272.     Dim sPathName As String                         '从FileName中提取的路径名  
  273.     Dim aFile As Variant  
  274.     Dim values As String  
  275.     filename = Application.GetOpenFilename("Excel 文件,*.xls;*.xlsx")  
  276.     Call DataSheetClear  
  277.     '调用Windows打开文件对话框  
  278.     If filename <> False Then                       '如果未按“取消”键  
  279.         aFile = Split(filename, "\")                '在全路径中,以“\”为分隔符,分成数据  
  280.         sPathName = aFile(0)                        '取盘符  
  281.         For i = 1 To UBound(aFile) - 1              '循环合成路径名  
  282.             sPathName = sPathName & "\" & aFile(i)  
  283.         Next  
  284.         sFileName = aFile(UBound(aFile))            '数组的最后一个元素为文件名  
  285.         Cells(1, 5).Value = sPathName & "\" & sFileName '保存路径名  
  286.       
  287.       
  288.         FileExists = Exist(sFileName)  
  289.         If Not FileExists Then  
  290.             Workbooks.Open filename  
  291.         End If  
  292.         Windows(sFileName).Activate  
  293.         values = getColumnValue(sFileName, filename)  
  294.         InitCandidateValue (values)  
  295.       
  296.         ThisWorkbook.Activate  
  297.         MsgBox "文件选择完成"  
  298.     Else  
  299.         MsgBox "文件选择失败"  
  300.         Exit Sub  
  301.     End If  
  302. End Sub  
  303. Private Function CheckTargetFields() As Boolean  
  304.   
  305.     Dim iRow As Integer  
  306.     Dim iFieldIndex As Integer  
  307.     Dim sFieldName As String  
  308.       
  309.     Dim iCols As Integer  
  310.     Dim iCol As Integer  
  311.     Dim errFields As String  
  312.     Dim errMSG As String  
  313.       
  314.     errMSG = ""  
  315.     errFields = ""  
  316.       
  317.     For iRow = SourceFiledConfigStart To SourceFiledConfigEnd  
  318.         If Trim(ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 2)) = "" Then  
  319.             errFields = errFields & ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 1) & " "  
  320.         End If  
  321.         For iFieldIndex = 0 To UBound(TargetColumnArray)  
  322.             sFieldName = TargetColumnArray(iFieldIndex)  
  323.              If sFieldName = Trim(ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 2)) Then  
  324.                 ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 3) = iFieldIndex + 1  
  325.                 Exit For  
  326.              End If  
  327.         Next  
  328.         If Trim(ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 3)) = "" Then  
  329.             errFields = errFields & ThisWorkbook.Sheets(RefSheetname).Cells(iRow, 1) & " "  
  330.         End If  
  331.     Next  
  332.          
  333.     If errFields <> "" Then  
  334.         ThisWorkbook.Activate  
  335.         errMSG = "源表对应列名: " & errFields & "不存在,请检查输入是否正确!"  
  336.         MsgBox errMSG, vbOKOnly, "字段配置错误"  
  337.         CheckTargetFields = False  
  338.   
  339.     Else  
  340.       CheckTargetFields = True  
  341.     End If  
  342.         
  343.         
  344. End Function  
  345.   
  346. Private Function Exist(ByVal filename As StringAs Boolean  
  347.   
  348.     Dim iCount As Integer  
  349.     Dim i As Integer  
  350.     iCount = Workbooks.Count  
  351.     For i = 1 To iCount  
  352.      If Workbooks.Item(i).Name = filename Then  
  353.         Exist = True  
  354.         Exit For  
  355.      End If  
  356.     Next  
  357.     If i > iCount Then  
  358.         Exist = False  
  359.      End If  
  360.        
  361. End Function  
  362.   
  363.   
  364. Private Function getProjectDColumn() As String  
  365.   
  366.     Dim strValue As String  
  367.     Dim str As String  
  368.     Dim iCount As Integer  
  369.     Dim iColumn As Integer  
  370.           
  371.           
  372.     'Windows(sFileName).Activate  
  373.       
  374.     iCount = ThisWorkbook.Sheets(usersheetname).Cells(1, 1).CurrentRegion.Columns.Count  
  375.     strValue = ""  
  376.     str = ""  
  377.     For iColumn = 1 To iCount - 1  
  378.         str = Trim(ThisWorkbook.Sheets(usersheetname).Cells(1, iColumn).Value)  
  379.         strValue = strValue + str + ","  
  380.     Next  
  381.     str = Trim(ThisWorkbook.Sheets(usersheetname).Cells(1, iCount).Value)  
  382.     strValue = strValue + str  
  383.       
  384.     SourceColumnArray = Split(strValue, ",")  
  385.     iCount = UBound(TargetColumnArray)  
  386.     getProjectDColumn = strValue  
  387. End Function  
  388.   
  389. Private Function getColumnValue(ByVal sFileName As StringByVal filename As StringAs String  
  390.   
  391.     Dim strValue As String  
  392.     Dim str As String  
  393.     Dim iCount As Integer  
  394.     Dim iColumn As Integer  
  395.           
  396.     FileExists = Exist(sFileName)  
  397.     If Not FileExists Then  
  398.         Workbooks.Open filename  
  399.     End If  
  400.     Windows(sFileName).Activate  
  401.           
  402.           
  403.     'Windows(sFileName).Activate  
  404.       
  405.     iCount = ActiveWorkbook.Sheets(1).Cells(1, 1).CurrentRegion.Columns.Count  
  406.     strValue = ""  
  407.     str = ""  
  408.     For iColumn = 1 To iCount - 1  
  409.         str = Trim(ActiveWorkbook.Sheets(1).Cells(1, iColumn).Value)  
  410.         strValue = strValue + str + ","  
  411.     Next  
  412.     str = Trim(ActiveWorkbook.Sheets(1).Cells(1, iCount).Value)  
  413.     strValue = strValue + str  
  414.       
  415.     TargetColumnArray = Split(strValue, ",")  
  416.     iCount = UBound(TargetColumnArray)  
  417.     getColumnValue = strValue  
  418.       
  419.   
  420.       
  421. End Function  
  422.   
  423. Public Function InitCandidateValue(ByVal values As String)  
  424.     ThisWorkbook.Activate  
  425.       
  426.     Sheets(RefSheetname).Select  
  427.     Range("D2:D100").Select  
  428.     With Selection.Validation  
  429.         .Delete  
  430.         .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _  
  431.         xlBetween, Formula1:=values  
  432.         .IgnoreBlank = True  
  433.         .InCellDropdown = True  
  434.         .InputTitle = ""  
  435.         .ErrorTitle = ""  
  436.         .InputMessage = ""  
  437.         .ErrorMessage = ""  
  438.         .IMEMode = xlIMEModeNoControl  
  439.         .ShowInput = True  
  440.         .ShowError = True  
  441.     End With  
  442.       
  443. End Function  
  444.   
  445.   
  446.   
  447.   
  448.   
  449. Private Sub CommandButton3_Click()  
  450.     ' 复制数据开始了  
  451.     Call DataSheetClear  
  452.     Call CopyFieldData  
  453.     Call SetDefaultFieldData  
  454. End Sub  
  1.   
  1. ThisWorkBook的代码  
  1. Const usersheetname As String = "数据"  
  2. Const RefSheetname As String = "配置"  
  3.   
  4.   
  5.   
  6.   
  7.   
  8.   
  9. Private Sub Workbook_Open()  
  10.     If Sheets(RefSheetname).Cells(1, 5).Value = "" Then  
  11.         Sheets(RefSheetname).InitCandidateValue (" ")  
  12.         Sheets(RefSheetname).Range("D2:D100").ClearContents  
  13.         Sheets(RefSheetname).Select  
  14.           
  15.     End If  
  16. End Sub 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值