• 2

求救excel的問題-儲存格 "互相交換"-已解決


大野狼被小紅帽吃 wrote:
因為根本無法交換儲存格


excel沒有內建這個功能要自己寫.

Google "swap cell values in excel"

然後找那些在2005年就已經有人寫好的vba 剪貼後又拿來自己用 就可以交換.
-----------------------------------------------------------
在程式設計方面的a交換b只有兩種方式
1. 用XOR bitwise operation: 這個一般人聽都沒聽過跟不會去用的.
2. 借用空白空間當暫存區 複製 貼上.
-----------------------------------------------------------
這篇應該發在電腦->軟體->文書處理比較合適.

最近在找這樣的功能
偶然翻到這篇文
自己寫了一個
請試試看吧

Sub 雙重範圍內容互換()
'
' 雙重範圍內容互換 巨集
'
Rng = Selection.Address
Rng_ = InStr(1, Rng, ",", 1)
If Rng_ = 0 Then
MsgBox ("未選取第二範圍,不執行互換")
Else
If InStr(Rng_ + 1, Rng, ",", 1) > 0 Then
MsgBox ("選取超出雙重範圍,不執行互換")
Else
Rng1 = Left(Rng, Rng_ - 1)
RngA = Range(Rng1).Value
Rng2 = Right(Rng, Len(Rng) - Rng_)
RngB = Range(Rng2).Value
For Each Cells1 In Range(Rng1)
For Each Cells2 In Range(Rng2)
If Cells1.Address = Cells2.Address Then
MsgBox ("範圍有重疊,不執行互換")
Exit Sub
End If
Next Cells2
Next Cells1
Rng1R = Range(Rng1).Rows.Count
Rng1C = Range(Rng1).Columns.Count
Rng2R = Range(Rng2).Rows.Count
Rng2C = Range(Rng2).Columns.Count
If Rng1R = Rng2R And Rng1C = Rng2C Then
Range(Rng1).Value = RngB
Range(Rng2).Value = RngA
ElseIf Rng1R = Rng2C And Rng1C = Rng2R Then
Range(Rng1).Value = Application.Transpose(RngB)
Range(Rng2).Value = Application.Transpose(RngA)
Else
MsgBox ("選取範圍不相等,不執行互換")
End If
End If
End If
'
End Sub
  • 2
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?