2016-08-19 152 views
2

你好,你能幫我請VBA代碼?我想創建基於文本的細胞中的日誌文件(「C2」和「C3」 +日期和時間),當我按下按鈕「zadat」謝謝你VBA創建日誌文件

我的實現代碼:

模塊1

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 
Sub zadat() 

Dim reg, check As String 
Dim i, j, done As Integer 
reg = Cells(2, 3).Value 
check = Cells(4, 3).Value 

If check = "True" Then 

    i = 2 
    j = 1 
    done = 0 
    Do While Sheets("data").Cells(i, j) <> "" 
     If Sheets("data").Cells(i, j) = reg Then 
      vytisteno = ZkontrolovatAVytiskoutSoubor() 

      done = Sheets("data").Cells(i, j + 3) 
      done = done + 1 
      Sheets("data").Cells(i, j + 3) = done 
      Exit Do 
     End If 
     i = i + 1 

    Loop 
Else 
    MsgBox ("Opravit, špatný štítek!!!") 
End If 

Cells(3, 3) = "" 

Cells(3, 3).Select 
ActiveWindow.ScrollRow = Cells(1, 1).row 


End Sub 

模塊2:

Option Explicit 
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (_ 
ByVal hwnd As Long, _ 
ByVal lpOperation As String, _ 
ByVal lpFile As String, _ 
ByVal lpParameters As String, _ 
ByVal lpDirectory As String, _ 
ByVal nShowCmd As Long) As Long 

Public Function PrintThisDoc(formname As Long, FileName As String) 
On Error Resume Next 
Dim x As Long 
x = ShellExecute(formname, "Print", FileName, 0&, 0&, 3) 
End Function 

Public Function ZkontrolovatAVytiskoutSoubor() As Boolean 
Dim printThis 
Dim strDir As String 
Dim strFile As String 
strDir = "W:\Etikety\Štítky\Krabice\Testy" 
strFile = Range("C2").Value & ".lbe" 
If Not FileExists(strDir & "\" & strFile) Then 
    MsgBox "soubor neexistuje!" 
ZkontrolovatAVytiskoutSoubor = False 
Else 
printThis = PrintThisDoc(0, strDir & "\" & strFile) 
ZkontrolovatAVytiskoutSoubor = True 
End If 
End Function 

Private Function FileExists(fname) As Boolean 
    'Returns TRUE if the file exists 
    Dim x As String 
    x = Dir(fname) 
    If x <> "" Then FileExists = True _ 
     Else FileExists = False 
End Function 
+0

歡迎來到SO。您能否讓我們知道您分享的代碼有哪些問題?任何錯誤?或者代碼沒有給你預期的結果?請更清楚地指出 – Siva

+0

我想通過按「zadat」創建一個記錄單元格C2和C3的代碼。 (從單元格C2和C3讀取文本,並將單元格中的日期文件值寫入單元格中的日期文件值前面),我將實現這些日誌代碼以便發揮作用。 –

+0

類似的東西(日誌文件):日期 - 時間 - 值C2 - 值C3 –

回答

3

嘗試此。下面的代碼將每次

Public Function LogDetails() 
    Dim fso As Object 
    Set fso = CreateObject("Scripting.FileSystemObject") 

    Dim logFile As Object 
    Dim logFilePath As String 
    Dim logFileName As String 

    'Replace 'TestLog' with your desired file name 
    logFileName = "TestLog" & ".txt" 
    myFilePath = "C:\Users\..\Desktop\" & logFileName 'Modify the path here 

    If fso.FileExists(myFilePath) Then 
    Set logFile = fso.OpenTextFile(myFilePath, 8) 
    Else 
    ' create the file instead 
    Set logFile = fso.CreateTextFile(myFilePath, True) 
    End If 

    logFile.WriteLine "[" & Date & " " & Time & "] " & Worksheet("yoursheetnamehere").Cells(2, 3) & " " & Worksheet("yoursheetnamehere").Cells(3, 3) 

    logFile.Close ' close the file 
End Function 
+0

謝謝,一切都好但這部分代碼沒有做任何事情logFile.WriteLine「[」&Date&「」&Time&「]」&Cells(2,3)&「」&Cells(3,3)如果幫助我的工作表名稱爲「Kontrola」你和將要寫入的單元格在這張表上是C2和C3。 –

+0

@FiínekCahů您不應該期望堆棧溢出答案100%適合您的確切電子表格。你應該能夠修改這個答案以符合你的需求。堆棧溢出不是提供最終產品的免費編碼服務。 –

5

創建新的日誌文件,如果你不想使用FSO,有隻使用VBA語句的簡單解決方案:OpenPrint #Close

Sub Log2File(Filename As String, Cell1, Cell2) 
    Dim f As Integer 
    f = FreeFile 
    Open Filename For Append Access Write Lock Write As #f 
    Print #f, Now, Cell1, Cell2 
    Close #f 
End Sub 

我已經把文件名和單元格作爲可重用目的的子參數。我也使用默認(本地)格式,但可以輕鬆更改。 請注意,您不必檢查文件是否存在,如果文件不存在,將會創建該文件。