標題:
[發問]
如何對特定欄進行篩選和替代資料
[打印本頁]
作者:
luke
時間:
2013-5-25 22:27
標題:
如何對特定欄進行篩選和替代資料
本帖最後由 luke 於 2013-5-25 22:28 編輯
各位先進
1. sheet1表的A:C欄為資料區, 約有500列(含空白列), 其中B欄資料(含有黃底色的儲存格), 當按下『篩選』
按鍵就會抓取B欄有黃底的儲存格至L:N欄(PS: L欄=N欄, M欄取C欄)
2.完成篩選後, B欄有黃底的儲存格想要利用L欄與N欄[N欄是由人工輸入資料,視情況會做修改]相對應關係,
當按下『替代』按鍵, 只需對B欄進行字母的替代(其他欄位不需做替代),
即取N欄相對應至L欄的儲存格資料, 去對B欄所有黃底的儲存格做文字替代.
3.若N欄資料中, 有與B欄非黃底儲存格的資料相同時就停止執行VBA動作並顯示該重複值.
煩請先進 大大指導
[attach]15080[/attach]
作者:
GBKEE
時間:
2013-5-26 13:35
回復
1#
luke
Option Explicit
Dim D As Object, DRng As Range
Sub 篩選()
資料篩選
With Sheet1.Range("L1")
.CurrentRegion = ""
If D.Count = 0 Then Exit Sub
.Resize(D.Count, 3) = Application.Transpose(Application.Transpose(D.ITEMS))
.CurrentRegion.Sort .Cells(1)
End With
End Sub
Sub 替代()
Dim Rng As Range, R As Range, E As Range, C As Range
資料篩選
Application.ScreenUpdating = False
With Sheet1
.Range("C:C").Value = .Range("C:C").Value '去除"'"字串'L1 多一個
Set Rng = .Range("L1").CurrentRegion
For Each R In Rng.Columns(2).Cells 'M欄的儲存格
If DRng.Find(R.Offset(, 1), lookat:=xlWhole) Is Nothing And Application.CountIf(.[B:B], R.Offset(, 1)) > 0 Then
'N欄資料中, 有與B欄非黃底儲存格的資料相同時就停止執行VBA-> 'N欄的字串在[篩選資料的儲存格]中比對不到, 且[B:B]中有此字串
With .Range("B:B")
.Replace R.Offset(, 1), "=XXX", xlWhole
With .SpecialCells(xlCellTypeFormulas, xlErrors)
.Value = R.Offset(, 1)
.Select
End With
End With
MsgBox R.Offset(, 1) & " 有重複值."
End
End If
With .Range("C:C")
.Replace R.Value, "=XXX", xlWhole
With .SpecialCells(xlCellTypeFormulas, xlErrors)
.Value = R
For Each E In .Areas
For Each C In E.Cells
If C.Offset(, -1) = R.Offset(, -1) Then C.Offset(, -1) = R.Offset(, 1)
Next
Next
End With
End With
Next
End With
Application.ScreenUpdating = True
End Sub
Private Sub 資料篩選()
Dim R As Range
Set D = CreateObject("SCRIPTING.DICTIONARY")
With Sheet1
For Each R In .Range("B1", .Cells(.Rows.Count, "B").End(xlUp))
If R <> "" And R.Cells(1, 2) <> "" Then
D(R & R.Cells(1, 2)) = Array(R, R.Cells(1, 2), R) '存入字典物件: 篩選的資料
If DRng Is Nothing Then '篩選資料的儲存格
Set DRng = R
Else
Set DRng = Union(R, DRng)
End If
End If
Next
End With
End Sub
複製代碼
作者:
luke
時間:
2013-5-26 20:46
回復
2#
GBKEE
謝謝超版回覆
1.當按下『替代』按鍵去對B欄進行字母的替代時會出現1004錯誤, 是否因執行子VBA即 "資料篩選" 所造成?
[attach]15085[/attach]
2.如果直接改用下列"替代"語法也許較容易, 但要如何兼顧[N欄資料中與B欄非黃底儲存格的資料相同時就停止執行VBA], 應如何修改VBA?
Columns("B:B").Select
Selection.Replace What:=a, Replacement:=a.Offset(, 2), lookat:=xlPart, ReplaceFormat:=False
(a=L欄)
以上
煩請指導.
[attach]15086[/attach]
作者:
GBKEE
時間:
2013-5-26 21:40
回復
3#
luke
在附檔上的字串 L1 實際上是 'L1 ->文字格式
用這行除去 ' 在2003版本可除去,但2007版本為何無法除去尚請有2007試試看
.Range("C:C").Value = .Range("C:C").Value '去除"'"字串'L1 多一個
複製代碼
[attach]15087[/attach]
作者:
Hsieh
時間:
2013-5-26 23:07
回復
1#
luke
Sub 篩選()
Set d = CreateObject("Scripting.Dictionary")
With sheet1
For Each a In .Range(.[B1], .Cells(.Rows.Count, 2).End(xlUp))
If a.Interior.ColorIndex = 6 Then d(a.Value) = Array(a, a.Offset(, 1), a)
Next
With .Range("L1")
.CurrentRegion = ""
.Resize(d.Count, 3) = Application.Transpose(Application.Transpose(d.items))
.Resize(d.Count, 3).Sort .Cells(1, 1), xlAscending, Header:=xlNo
End With
End With
End Sub
Sub 取代()
Set d = CreateObject("Scripting.Dictionary")
Set d1 = CreateObject("Scripting.Dictionary")
With sheet1
With .Range("L1").CurrentRegion
For Each a In .Columns(1).Cells
d(a.Value) = a.Offset(, 2)
d1(a.Offset(, 2).Value) = a.Value
Next
End With
For Each a In .Range(.[B1], .Cells(.Rows.Count, 2).End(xlUp))
If d1.exists(a.Value) And a.Interior.ColorIndex <> 6 Then MsgBox "資料重複": Exit Sub
Next
For Each a In .Range(.[B1], .Cells(.Rows.Count, 2).End(xlUp))
If d.exists(a.Value) And a.Interior.ColorIndex = 6 Then a.Value = d(a.Value)
Next
MsgBox "取代完成"
End With
End Sub
複製代碼
作者:
luke
時間:
2013-5-27 07:11
回復
5#
Hsieh
回覆H超版
謝謝協助賀幫忙修改
再次感謝
以上
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)