轉到VB-編輯器(按Alt + F11),插入新的模塊並粘貼此代碼:
Function RangeSelectionPrompt(txt As String) As Range
Dim rng As Range
Set rng = Application.InputBox(txt, "Select a range", Type:=8)
Set RangeSelectionPrompt = rng
End Function
Sub FormatAs()
Dim Sh As Worksheet
Dim r1 As Range, r2 As Range
Dim txt1 As String, txt2 As String
Dim cel As Range, Col As Range
Dim Val, check As Long, pos As Long
Set Sh = ActiveSheet
txt1 = "Enter Range #1 - sourse"
txt2 = "Enter Range #2 - to paste formats"
Set r1 = Intersect(RangeSelectionPrompt(txt1), Sh.UsedRange)
Set r2 = Intersect(RangeSelectionPrompt(txt1), Sh.UsedRange)
For Each cel In r2.Cells
Val = cel.Value
If Val <> "" Then
For Each Col In r1.Columns
check = WorksheetFunction.CountIf(Col, Val)
If check > 0 Then
pos = WorksheetFunction.Match(Val, Col, 0)
Col.Cells(pos, 1).Copy
cel.PasteSpecial Paste:=xlPasteFormats
End If
Next Col
End If
Next cel
End Sub
運行一次以複製格式。您需要定義的兩個範圍: ![enter image description here](https://i.stack.imgur.com/ZprVM.png)
源範圍(A:B)和範圍粘貼(F:G)
最大, 感謝您的響應。它看起來並不像我正在尋找的功能。我一直在與微軟支持聯繫,似乎我所尋找的功能是不可能的。基本上,我一直在尋找一個條件函數來複制值(文本)以及格式(填充顏色和邊框)。 –