使用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