本文主要是介绍Excel VBA 小试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近论文告一阶段,看到杨胖每天发日报需要用Excel做一些重复工作,无聊至极,就想用VBA给他减轻一些工作量。
VBA我第一次用,大概总用时不到一天,所以代码什么的可能不是很好,随便用用那种。
根据客户(杨胖)需求,共开发了两个模块,一个是格式调整,一个是数据分析和处理。
代码1:
Sub Format()
'
' Format 宏
'
' 快捷键: Ctrl+Shift+F
'Columns("F:F").SelectSelection.Delete Shift:=xlToLeftDim allcase As IntegerDim Sht As Worksheet'Set Sht = Worksheets("Sheet1")allcase = 0Doallcase = allcase + 1Loop Until Cells(allcase + 2, 1) = ""Range(Cells(1, 1), Cells(allcase + 1, 11)).SelectSelection.Borders(xlDiagonalDown).LineStyle = xlNoneSelection.Borders(xlDiagonalUp).LineStyle = xlNoneWith Selection.Borders(xlEdgeLeft).LineStyle = xlContinuous.ColorIndex = 0.TintAndShade = 0.Weight = xlThinEnd WithWith Selection.Borders(xlEdgeTop).LineStyle = xlContinuous.ColorIndex = 0.TintAndShade = 0.Weight = xlThinEnd WithWith Selection.Borders(xlEdgeBottom).LineStyle = xlContinuous.ColorIndex = 0.TintAndShade = 0.Weight = xlThinEnd WithWith Selection.Borders(xlEdgeRight).LineStyle = xlContinuous.ColorIndex = 0.TintAndShade = 0.Weight = xlThinEnd WithWith Selection.Borders(xlInsideVertical).LineStyle = xlContinuous.ColorIndex = 0.TintAndShade = 0.Weight = xlThinEnd WithWith Selection.Borders(xlInsideHorizontal).LineStyle = xlContinuous.ColorIndex = 0.TintAndShade = 0.Weight = xlThinEnd WithColumns("A:A").SelectSelection.ColumnWidth = 10Columns("B:B").SelectSelection.ColumnWidth = 22Columns("C:C").SelectSelection.ColumnWidth = 9Columns("D:D").SelectSelection.ColumnWidth = 9Columns("E:E").SelectSelection.ColumnWidth = 9Columns("F:F").SelectSelection.ColumnWidth = 22Columns("G:G").SelectSelection.ColumnWidth = 15Columns("H:H").SelectSelection.ColumnWidth = 35Columns("I:I").SelectSelection.ColumnWidth = 15Columns("J:J").SelectSelection.ColumnWidth = 35Columns("K:K").SelectSelection.ColumnWidth = 35
End Sub
代码2:
Sub dataprocess()''dataprocessing'Dim newcase As IntegerDim oldcase As IntegerDim clscase As IntegerDim yescase As IntegerDim todcase As IntegerDim Sht1 As WorksheetSet Sht1 = Worksheets("today")Dim Sht2 As WorksheetSet Sht2 = Worksheets("yesteday")yescase = 0Doyescase = yescase + 1Loop Until Sht2.Cells(yescase + 2, 1) = ""todcase = 0Dotodcase = todcase + 1Loop Until Sht1.Cells(todcase + 2, 1) = ""oldcase = 0i = 2Doj = 2DoIf Sht1.Cells(i, 1) = Sht2.Cells(j, 1) Thenoldcase = oldcase + 1'dataprocessingSht1.Cells(i, 8) = Sht2.Cells(j, 8)Sht1.Cells(i, 11) = Sht2.Cells(j, 11)Sht1.Cells(i, 1).Interior.ColorIndex = 39'end_dataprocessingExit DoEnd Ifj = j + 1Loop Until Sht2.Cells(j, 1) = ""i = i + 1Loop Until Sht1.Cells(i, 1) = ""newcase = todcase - oldcaseclscase = yescase - oldcaseMsgBox "Dataprocessing Completed" & vbCrLf & _"colse case num:" & clscase & vbCrLf & _"old case num:" & oldcase & vbCrLf & _"new case num:" & newcaseEnd Sub
过程心得:
1,VBA的语言还是很简单的,跟其他语言一样,需要了解其中的一些固定用法;
2,宏录制有时候会给自己写代码一些启发,但是代码的可控和灵活性要更高一些,可以两者结合;
3,格式调整这个,录制宏更方便。
其他,待补充。
这篇关于Excel VBA 小试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!