我有一個 Excel 資料庫,用于通過 VBA 在 Word 中創建一個序列號。現在有以下問題:
- 當我手動在 Word 中匯入資料時(Excel 作業表已關閉),一切正常。格式化的值(例如“ - 1K”、“1000 °C”、“36:00”、“17.11.2021”)作為文本匯入并顯示在序列號中。 Excel 資料庫和Word 串行字母格式
- 當我手動在 Word 中匯入資料(打開 Excel 表)時,只匯入值并且格式被破壞(例如“1”、“1000”、“1.5”、“44517”)。此外,它們作為文本匯入,因此無法通過郵件合并功能(例如“@“dd.MM.yyyy””)進行格式化。 Word 連續字母未格式化
- 當我通過VBA自動將Excel中的資料推送到Word中時(Excel-sheet顯然是打開的),問題是相似的。(以下代碼可能不相關)
'die Anzahl der Positionen defininieren
Dim n As Long
n = Worksheets("Eingabemaske").Range("BX2").Value 1 'Zeilen aus der Anzahl der Positionen bestimmen
'Nach Sachbearbeiter den LS (x bzw. y ausw?hlen)
Dim Bearbeiterstandort As String
Bearbeiterstandort = Application.International(xlDecimalSeparator)
If InStr(Bearbeiterstandort, ".") > 0 Then
strSerienbrief1 = "Sinterauftrag PUNKT"
ElseIf InStr(Bearbeiterstandort, ",") > 0 Then
strSerienbrief1 = "Sinterauftrag KOMMA"
Else
MsgBox "Support kontaktieren"
End If
'Serienbrief1
strLaufwerkDateiname = ThisWorkbook.Path & "\Serienbriefvorlagen\" & strSerienbrief1 & ".docx" 'Pfad und Dateinamen zusammenfügen
Set oWord = CreateObject("word.application")
Set oDoc = oWord.Documents.Open(strLaufwerkDateiname)
oWord.Visible = True
oWord.Application.Activate 'Dokument wird in den Vordergrund geholt
oDoc.MailMerge.MainDocumentType = 0 'wdFormLetters = 0. Gibt einen Typ von Seriendruckdokument an.
oDoc.MailMerge.OpenDataSource Name:= _
ThisWorkbook.FullName _
, ConfirmConversions:=False, LinkToSource:=True, Connection:= _
"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=" & _
ThisWorkbook.FullName & _
";Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Je" _
, SQLStatement:="SELECT * FROM `Eingabemaske$`", SQLStatement1:="", SubType:=1 'hier das Blatt ?ndern
With oDoc.MailMerge
.Destination = 0 'wdSendToNewDocument = 0. Die Ergebnisse werden aus dem Serienbrief in ein neues Dokument übertragen.
.SuppressBlankLines = True 'Wenn Seriendruckfelder leer sind, werden die leere Zeilen im Seriendruckdokument unterdrückt.
With .DataSource
.FirstRecord = 1 'wdDefaultFirstRecord = 1. Aus dem Hauptdokument mit den Datens?tzen 1 bis
.LastRecord = n 'wdDefaultLastRecord = -16. Zum letzten Datensatz zusammengeführt
End With
.Execute Pause:=False
End With
oDoc.Close SaveChanges:=0 'Das Seriendruckdokument wird ohne Speichern geschlossen
'Word Datei abspeichern
Dim dateiname As String
strLaufwerkDateiname2 = ThisWorkbook.Path & "\" 'Speicherpfad
teil1 = oWord.ActiveDocument.Words(1).Start 'Dateiname Start
teil2 = oWord.ActiveDocument.Words(2).End 'Dateiname Ende
dateiname = oWord.ActiveDocument.Range(teil1, teil2).Text 'Dateiname zusammensetzen
oWord.ActiveDocument.ExportAsFixedFormat outputfilename:=strLaufwerkDateiname2 & dateiname & ".pdf", exportformat:=wdExportFormatPDF 'PDF abspeichern
oWord.ActiveDocument.SaveAs2 Filename:=strLaufwerkDateiname2 & dateiname & ".docx" 'Word abspeichern
oWord.ActiveDocument.Close 'Datei schliessen
oWord.Application.Quit 'Word schliessen
Set oDoc = Nothing 'Dimension zurücksetzen
Set oWord = Nothing
- 當值以文本形式寫入時(例如“' - 1K”、“'1000 °C”、“'36:00”、“'17.11.2021”),問題就解決了。
現在我想問你一種快速的方法來從單元格 A1 復制格式化的值(所以“ - 1 K”而不僅僅是“1”),并將格式化的值作為文本插入到 A2。我試過
Sheets("Eingabemaske").Range("b2:DA51") = Range("c52:Db102").Worksheet.Evaluate("index(text(B2:DA51,""'""),)")
但結果只是插入了“'”的空單元格。
功能
Range("K2").Value = Format(Range("J2"), "&")
只復制值但保持目標單元格的格式。
有沒有人知道如何在打開 excel 時將格式化的值作為文本匯入到串行字母中,或者如何替換具有相同格式的文本的所有格式化值?
uj5u.com熱心網友回復:
您可以按如下方式完成您需要的操作:
- 制作一個回傳數字格式的 VBA 函式:
Public Function GetCellNumberFormat(R As Range) As String
GetCellNumberFormat = R.NumberFormat
End Function
- 對于要檢索的每個值,使用公式創建第二個單元格,該公式使用相同的格式將值顯式轉換為文本:
=TEXT(B3;PERSONAL.XLSB!GetCellNumberFormat(B3))
新單元格中的結果看起來與B3本示例中的單元格相同,但它將是文本而不僅僅是數字。
- 匯入 Word 字母時使用新單元格中的值。
您還可以直接在 VBA 中使用新函式,并通過使用作業表函式來獲取其格式化文本,如下所示:
Application.WorksheetFunction.Text(R.Value, GetCellNumberFormat(R))
其中R是Range單元格的變數。
uj5u.com熱心網友回復:
將數字另存為文本的幾十年前的技巧非常簡單。我想它可以節省長回圈中的處理時間,因為實際上沒有呼叫任何函式。
范圍(“A2”)=“'”&范圍(“B2”)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/361284.html
上一篇:復制檔案名
下一篇:您可以使用輸入值旋轉元素嗎?
