使用VBA似乎是要走我的路。這可以讓你編寫一個處理所有各種格式化選項的宏,並且希望對於你的財務人員來說可以很簡單。
你說你需要一些在Excel中使用表格或範圍的東西。第一列從不改變,所以我們可以將其存儲在宏中,第3-7列來自電子表格,而第8列只是空白。這留下第2列(季度/年爲QYY)作爲一個問題。如果在工作簿中某處指定了季度/年份(例如存儲在單元格中,作爲工作表名稱,作爲工作簿標題的一部分),那麼我們可以將其讀入。否則,您需要找到一些方法來指定季度/年,當宏運行(例如彈出一個對話框,要求用戶輸入的話)
一些簡單的代碼(我們會擔心如何調用這個版本):
Sub ProduceStatePayrollReportFile(rngPayrollData As Range, strCompanyNo As String, _
strQuarterYear As String, strRecordCode As String, strOutputFile As String)
參數是相當明顯的:保存數據的範圍,列1的公司編號,列2的季度/年,列7的固定代碼和我們想要輸出結果的文件到
' Store the file handle for the output file
Dim fnOutPayrollReport As Integer
' Store each line of the output file
Dim strPayrollReportLine As String
' Use to work through each row in the range
Dim indexRow As Integer
要在VBA中輸出到文件,我們需要獲取文件句柄,因此我們需要一個變量來存儲它。我們將在報告行字符串中構建報告的每一行,並使用行索引在範圍內工作
' Store the raw SSN, last name, first name and wages data
Dim strRawSSN As String
Dim strRawLastName As String
Dim strRawFirstName As String
Dim strRawWages As String
Dim currencyRawWages As Currency
' Store the corrected SSN, last name, first name and wages data
Dim strCleanSSN As String
Dim strCleanLastName As String
Dim strCleanFirstName As String
Dim strCleanWages As String
這些變量集分別存儲工作表的原始數據和要輸出到文件的清理數據。將它們命名爲「原始」和「乾淨」,可以更容易地發現意外輸出原始數據而非清理數據的錯誤。我們需要改變從一個字符串值的原始工資爲一個數值,以幫助格式化
' Open up the output file
fnOutPayrollReport = FreeFile()
Open strOutputFile For Output As #fnOutPayrollReport
FreeFile()獲取下一個可用的文件句柄,我們用它來鏈接到文件
' Work through each row in the range
For indexRow = 1 To rngPayrollData.Rows.Count
' Reset the output report line to be empty
strPayrollReportLine = ""
' Add the company number to the report line (assumption: already correctly formatted)
strPayrollReportLine = strPayrollReportLine & strCompanyNo
' Add in the quarter/year (assumption: already correctly formatted)
strPayrollReportLine = strPayrollReportLine & strQuarterYear
在我們遍歷各行的工作,我們開始通過清除輸出字符串,然後添加在值列1和2
' Get the raw SSN data, clean it and append to the report line
strRawSSN = rngPayrollData.Cells(indexRow, 1)
strCleanSSN = cleanFromRawSSN(strRawSSN)
strPayrollReportLine = strPayrollReportLine & strCleanSSN
的.Cells(indexRow, 1)
部分只是表示indexRow指定的行範圍中最左邊的一列。如果範圍在A列開始(這並不一定是這種情況),那麼這也就意味着A.我們需要寫cleanFromRawSSN
功能以後自己
' Get the raw last and first names, clean them and append them
strRawLastName = rngPayrollData.Cells(indexRow, 2)
strCleanLastName = Format(Left$(strRawLastName, 10), "[email protected]@@@@@@@@@")
strPayrollReportLine = strPayrollReportLine & strCleanLastName
strRawFirstName = rngPayrollData.Cells(indexRow, 3)
strCleanFirstName = Format(Left$(strRawFirstName, 8), "[email protected]@@@@@@@")
strPayrollReportLine = strPayrollReportLine & strCleanFirstName
Left$(string, length)
截斷字符串指定的長度。該格式圖片[email protected]@@@@@@@@@
格式的字符串作爲正好十個字符,左對齊(的!表示左對齊),並用空格填充
' Read in the wages data, convert to numeric data, lose the decimal, clean it and append it
strRawWages = rngPayrollData.Cells(indexRow, 4)
currencyRawWages = CCur(strRawWages)
currencyRawWages = currencyRawWages * 100
strCleanWages = Format(currencyRawWages, "000000000")
strPayrollReportLine = strPayrollReportLine & strCleanWages
我們將其轉換爲貨幣,使我們可以乘以100移動美分值在小數點左側。這使得使用Format
來生成正確的值變得更容易。這不會產生超過1000萬美元工資的正確輸出,但這是用於報告的文件格式的限制。在以0的格式的圖片墊令人驚訝的是
' Append the fixed code for column 7 and the spaces for column 8
strPayrollReportLine = strPayrollReportLine & strRecordCode
strPayrollReportLine = strPayrollReportLine & CStr(String(29, " "))
' Output the line to the file
Print #fnOutPayrollReport, strPayrollReportLine
的String(number, char)
功能0
產生變體,與指定的char
number
的序列。 CStr
將Variant變成一個字符串。該Print #
聲明輸出到文件,無需任何額外的格式
Next indexRow
' Close the file
Close #fnOutPayrollReport
End Sub
循環輪的範圍和重複下一行。當我們處理了所有行時,關閉文件並結束宏我們仍然需要兩件事:一個cleanFromRawSSN函數和一個用相關數據調用宏的方法。
Function cleanFromRawSSN(strRawSSN As String) As String
' Used to index the raw SSN so we can process it one character at a time
Dim indexRawChar As Integer
' Set the return string to be empty
cleanFromRawSSN = ""
' Loop through the raw data and extract the correct characters
For indexRawChar = 1 To Len(strRawSSN)
' Check for hyphen
If (Mid$(strRawSSN, indexRawChar, 1) = "-") Then
' do nothing
' Check for space
ElseIf (Mid$(strRawSSN, indexRawChar, 1) = " ") Then
' do nothing
Else
' Output character
cleanFromRawSSN = cleanFromRawSSN & Mid$(strRawSSN, indexRawChar, 1)
End If
Next indexRawChar
' Check for correct length and return empty string if incorrect
If (Len(cleanFromRawSSN) <> 9) Then
cleanFromRawSSN = ""
End If
End Function
Len
返回字符串和從string
Mid$(string, start, length)
返回length
字符在start
開始的長度。此功能可以改善,因爲它目前不檢查非數字數據
要調用宏:
Sub CallPayrollReport()
ProduceStatePayrollReportFile Application.Selection, "1234560007", "109", "01", "C:\payroll109.txt"
End Sub
這是調用它的最簡單方法。範圍是用戶在活動工作簿中的活動工作表上選擇的任何值,其他值是硬編碼的。用戶應該選擇他們想輸出到文件的範圍,然後進入工具>宏>運行,然後選擇CallPayrollReport
。爲此,宏需要成爲包含數據的工作簿的一部分,或者需要在用戶調用宏之前加載的不同工作簿中。
有人需要在每季度報告生成之前更改季度/年份的硬編碼值。如前所述,如果季度/年已經存儲在工作簿,然後某處這是更好的閱讀,在而不是硬編碼
希望是有道理的,是一些使用的
優秀的問題和答案。 – 2009-01-27 01:53:03