常用的几个VBA代码
1、将选定区域的单元格的列宽和行高调整为最合适的值,可使用代码:Sub SetColumnAndRow() With ActiveWindow.RangeSelection .Columns.AutoFit .Rows.AutoFit End With End Sub

3、在工作簿新建一个名称为“数值汇总”的工作表,代码:Sub AddWorksheet() On Error Resume Next Worksheets.Add().Name = "数值汇总" End Sub

5、在A1:B100中生成10位长度的随机字符串,代码:Sub MakeRandomString() Dim J As Integer D足毂忍珩im K As Integer Dim iTemp As Integer Dim sNumber As String Dim RandomStr(1 To 100, 1 To 1) As String Dim bOK As Boolean Randomize For J = 1 To 100 sNumber = "" For K = 1 To 10 Do iTemp = Int((122 - 48 + 1) * Rnd + 48) Select Case iTemp Case 48 To 57, 65 To 90, 97 To 122 bOK = True Case Else bOK = False End Select Loop Until bOK bOK = False sNumber = sNumber & Chr(iTemp) Next K RandomStr(J, 1) = sNumber Next J Range("A1:B100").Value = RandomStrEnd Sub
