VBA經常使用語句

ThisWorkbook.Path數組

獲取當前目錄spa

sheetXXX.UsedRange.Rows.Countexcel

獲取當前sheet頁使用的行數code

 

 

MsgBox("hello world")orm

彈窗事件

Dim data As Worksheet
    Set data = Sheets("錄入表")
定義 

Debug.printci

當即窗口get

Dim targetarray() As Stringit

定義數組
 io

IsNumeric(mtotalcharge)

判斷是數字

originArray = Split(origin, Chr(10))

用回車來分割

UBound(originArray)

數組最大下標

LBound(originArray)

數組最小下標

 

Dim i As Integer

For i = LBound(originArray) To UBound(originArray)

    If i = 2 Then Exit For

    Debug.Print originArray(i)

    Debug.Print "-----------"

Next

循環數組

跳出數組

Cstr

轉換成文本

Dim date1 As Date

date1 = sheetSource.Range("A1").Value  

datestr = UCase(Format(date1, "ddMMM"))

 

日期格式化

UCase

大寫

Cells(24, 4)

根據行號列號選中單元格

sheet_FHL.Range("b2", "c2").Clear

清理數據

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Address = "$B$4" Then
Dim barcode
    barcode = Range("B3").Value
    Range("B9") = barcode
    Range("B3").Select
End If
End Sub

單元格內回車觸發事件

Sub createFile()
Application.ScreenUpdating = False
    '目錄處理
    Dim folder As String
    folder = "c:\test"
    If Dir(folder, vbDirectory) = "" Then
        MkDir (folder)
    End If
    '輸出當前Excel的名字
    Debug.Print ThisWorkbook.Name
    
    '生成新文件
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim fileName As String
    Set wb = Workbooks.Add
    Set ws = wb.Worksheets.Add
    'sheet 名字
    ws.Name = "xxxxxxxxxxxxx"
    '單元格填充
    ws.Cells(1, 1).Value = ThisWorkbook.Name
    fileName = Format(Date, "yyyymmdd") & Hour(Time) & Minute(Time) & Second(Time) & ".xls"
    'Application.DisplayAlerts = False
    ws.SaveAs (folder & "\" & fileName), FileFormat:=xlExcel8
    Workbooks(fileName).Close (False)
    'ThisWorkbook.Activate
    'Application.DisplayAlerts = True
    Debug.Print ("文件路徑:" & folder & "\" & fileName)
    
Application.ScreenUpdating = True
End Sub

生成excel文件
相關文章
相關標籤/搜索