常用的几个VBA代码

2025-06-16 22:24:45

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

常用的几个VBA代码

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

常用的几个VBA代码

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

常用的几个VBA代码
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢