用代码实现自动化,界面就是一个按钮,点一下就完成了工资表的计算,或者自动生成工资条等。
这就是 *** 作界面。
Private Sub CommandButton3_Click() '检查填充
Dim skUArr(1 To 1000, 1 To 3)
Dim skUGs As Integer
Dim hH As Integer
Dim zlHH As Integer
CellsFind(What:="Weight of box", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, MatchByte:=False, SearchFormat:=False)Activate
zlHH = ActiveCellRow '重量所在行号
skmGs = 0
hH = 5
Do While Trim(Cells(hH, 1)Text) <> ""
skUGs = skUGs + 1
skUArr(skUGs, 1) = Trim(Cells(hH, 1)Text)
skUArr(skUGs, 2) = Trim(Cells(hH, 4)Text)
skUArr(skUGs, 3) = Cells(hH, 10)Value
hH = hH + 1
Loop
Dim fName As String
Dim SBook As Workbook
Call SelectFile(fName)
Set SBook = WorkbooksOpen(fName)
Dim M_sku As String, M_fnSku As String, M_qty As Integer
With SBookSheets(1)
For I = 1 To skUGs
M_sku = Trim(Cells(5 + I - 1, 1)Text)
M_fnSku = Trim(Cells(5 + I - 1, 4)Text)
M_qty = Cells(5 + I - 1, 9)Value
If skUArr(I, 1) <> M_sku Then
MsgBox ("第" & I & "条记录的SKU不一致!")
Exit Sub
End If
If skUArr(I, 2) <> M_fnSku Then
MsgBox ("第" & I & "条记录的FNSKU不一致!")
Exit Sub
End If
If skUArr(I, 3) <> M_qty Then
MsgBox ("第" & I & "条记录的QTY不一致!")
Exit Sub
End If
Next I
End With
Dim qtyArr() As Integer
Dim boxGs As Integer
Dim boxArr()
With ThisWorkbookSheets(1)
boxGs = Cells(4, 200)End(xlToLeft)Column
ReDim qtyArr(1 To skUGs, 1 To boxGs)
ReDim boxArr(1 To 4, 1 To boxGs)
'读取数量
For I = 1 To skUGs
For J = 1 To boxGs
qtyArr(I, J) = Cells(5 + I - 1, 12 + J - 1)Value
Next J
Next I
'读取box
For I = 1 To 4
For J = 1 To boxGs
boxArr(I, J) = Cells(zlHH + I - 1, 12 + J - 1)Value
Next J
Next I
End With
'填充
With SBookSheets(1)
'Cells(5, 12)Resize(skUGs, boxGs) = qtyArr
'Cells(zlHH, 12)Resize(4, boxGs) = boxArr
For I = 1 To skUGs
For J = 1 To boxGs
If qtyArr(I, J) > 0 Then
Cells(5 + I - 1, 12 + J - 1) = qtyArr(I, J)
End If
Next J
Next I
For I = 1 To 4
For J = 1 To boxGs
Cells(zlHH + I - 1, 12 + J - 1) = boxArr(I, J)
Next J
Next I
End With
SBookSave
MsgBox ("检查结果OK,填充完成!")
End Sub
这是其中一个按钮的代码,供参考。
一、创建E:\test_excel目录,下面放上几个要合并的Excel文件。
二、新打开一个Excel文件,先“开启开发工具”,如下图点击1,选择“Excel选项”,然后在“常用”选项卡中勾选“在功能区显示‘开发工具’选项卡”,保存后,就能看到了。
三、开启宏,如上图所示,开发工具—宏安全性—宏设置里面。
四、在Visual Basic里面选择"Sheet1",并编写相应的宏
相关代码:
'VBA for merge excel files,just support for Excel 2007'Excel 2003 need change the codes'Author:naiveloafer'Date:2012-04-29Sub naiveloafer()Dim fs, f, f1, fc, s, x, rowss, columnssSet fs = CreateObject("ScriptingFileSystemObject")Set f = fsGetFolder("E:\test_excel\") 'Directory of excel files will be mergeSet fc = fFilesx = 1For Each f1 In fcIf Right(f1Name, 4) = "xlsx" ThenWorkbooksOpen (f1Path)rowss = Workbooks(f1Name)Sheets(1)Range("A65536")End(xlUp)Rowcolumnss = Workbooks(f1Name)Sheets(1)ColumnsCountWorkbooks(f1Name)Sheets(1)Range("A1:z" & CStr(rowss))CopyWorkbooks(1)ActivateWorkbooks(1)Sheets(1)Range("A" & CStr(x) & ":z" & CStr(x + rowss))SelectWorkbooks(1)Sheets(1)PasteApplicationCutCopyMode = Falsex = x + rowssWorkbooks(f1Name)Close savechanges:=FalseEnd IfNextEnd Sub
五、回到刚才的Excel中,点击宏,选择刚才的宏,运行即可,如果代码没问题,那就能在当前Sheet中显示合并的结果,当然如果你有编程经验可以选择调试才定位问题,修改等。
这个需求正常情况下应该使用公式,而且就三个科目,除非你想学习VBA,代码如下:
Option ExplicitSub YgB()
Dim i, j, k, x, y
i = 2
While Cells(i, 1) <> ""
x = Cells(i, 2) '最高分
For j = 3 To 4
If Cells(i, j) > x Then x = Cells(i, j)
Next j
y = "" '最高分科目
For j = 2 To 4
If Cells(i, j) = x Then y = y & Cells(1, j) & " "
Next j
Cells(i, 5) = x
Cells(i, 6) = y
i = i + 1
Wend
End Sub
执行拷屏:
优秀那一步,如果需要判断,应该把下面的语句:
cells(i,5)=x
修改为:
Cells(i, 5) = IIf(x < 60, "不及格", IIf(x < 80, "及格", "优秀"))
具体标准你自行设定。
以上就是关于vba程序是什么全部的内容,包括:vba程序是什么、求助:运用excel vba编写自动打印分类汇总表的程序、Excel VBA编程,实现成绩级别分类并显示最好科目,如图等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)