現有 Excel 文件中的多個條目

現有 Excel 文件中的多個條目

我們的客戶向我們提供了一個 Excel 文件,其中列出了車手姓名以及他們獲得免費抽獎的門票數量,例如“bill smith(下一欄)17 張門票”

有沒有辦法將其分為「bill Smith」的 17 個單獨條目,以便我們可以為所有 286 名司機列印總計 5000 張以上的罰單?有的得到1,有的得到5,還有的更多......

目前,我們根據需要手動重複驅動程式名稱多次。

我們透過郵件合併到 2x3 avery 標籤範本來列印它們,然後將它們分開。

答案1

  1. 修改該文件,使其具有以下屬性:
    • A 列:標題為「名稱」。所有儲存格都包含要簽發門票的人員姓名。
    • B 列:標題為「數字」。所有儲存格都包含要指派給 A 列同一行中列出的人員的工單數量。
    • 工作表上不包含包含「姓名」和「號碼」資訊的其他資料。
  2. 選擇包含「名稱」和「編號」資訊的工作表後,將檔案另存為 CSV(逗號分隔)。在本範例中,我們將使用 OrigCSV.csv 作為檔案名稱。
  3. 開啟 PowerShell 會話,然後導覽至包含剛剛儲存的 CSV 的資料夾。
  4. 運行以下命令:
    • $x=ipcsv .\OrigCSV.csv;$x|%{for($y=1;$y-le$_.Number;$y++){$_.Name}}|Out-File NewCSV.csv
  5. 開啟 NewCSV.csv 並驗證名稱是否以您想要的方式和編號列出。

如果您需要的不僅僅是重複的名稱,那麼仍然可以使用 PowerShell - 只是更「有趣」一點。

這是上面提供的命令列的擴充和註解版本:

<#
    First set $x so that it contains everything in OrigCSV.csv.
    Each line of the CSV will be an array element within $x, with "Name" and "Number" properties according to their entry in the CSV.

    ipcsv is a built-in alias for Import-Csv
#>
$x=ipcsv .\OrigCSV.csv;
<#
    Next step is to put all the objects in $x through a ForEach-Object loop.

    % is a built-in alias for ForEach-Object.
#>
$x|%{
    <#
        Within ForEach-Object, we're starting a For loop.
        The loop definition starts with setting a counter, $y, to 1.
        Then, if $y is less than or equal to the current line item's "Number" property, the script block will execute.
        After the script block executes, it will increment $y by 1 and check the loop condition again.
        Once $y becomes greater than the current line item's "Number" property, the For loop will exit.
    #>
    for($y=1;$y-le$_.Number;$y++)
    {
        # This next line simply outputs the "Name" property of the current line item.
        $_.Name
    # After the For loop exits, the script will return to the ForEach-Object loop and proceed to put the next item into the For loop.
    }
# After ForEach-Object is done with its work, we pipe all of the output to Out-File so that the list gets written to a new CSV file.
}|Out-File NewCSV.csv

答案2

這是一個VBA解決方案。首先,選擇兩列中的資料。如果存在列標題,請勿選擇。

接下來,將此程式碼放入模組中並執行它。 (有關執行此操作的說明,請參閱這個帖子.)

Sub TicketList()
'Two columns of drivers and ticket counts should be selected (no headers) before running this Sub.
Dim drivers() As Variant, output() As Variant, shtOut As Worksheet
Dim i As Long, j As Long, k As Long, scount As Integer
drivers = Selection.Value
'Set size of output array to match total number of tickets
ReDim output(1 To Application.WorksheetFunction.Sum(Selection), 1 To 1) As Variant
For i = LBound(drivers, 1) To UBound(drivers, 1)
    For j = 1 To drivers(i, 2)
        k = k + 1
        output(k, 1) = drivers(i, 1)
    Next j
Next i
'Place tickets on new sheet named "Driver Tickets #"
For Each sht In ThisWorkbook.Sheets
    If InStr(sht.Name, "Driver Tickets") > 0 Then scount = scount + 1
Next sht
Set shtOut = Sheets.Add
If scount = 0 Then
    shtOut.Name = "Driver Tickets"
Else
    shtOut.Name = "Driver Tickets " & CStr(scount + 1)
End If
'Print output on the new sheet
shtOut.Range("A1").Resize(UBound(output, 1), 1).Value = output
End Sub

這將在名為「Driver Tickets」的新工作表上建立票證名稱清單。

相關內容