Excel-VBA 小技

ExcelのSheetで、同じ文字列を一気に削除するVBA
指定した文字列を一旦カラー化し、その後削除のY/Nを尋ねる方法
冗長な手法だけど、可読性は高く、改造し易い・・・・

以下が、Source

Sub SAKUJYO()
'
' SAKUJYO Macro
'

Sheets("Room").Select

Range("B5:BA33").Select
With Selection.Font

.Name = "游ゴシック"
.FontStyle = "標準"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor

End With
Range("A4").Select

Dim D_thcd As String

Dim i As Long, j As Long, k As Long

Dim Rtn As Integer

Dim Cnt As Long


D_thcd = InputBox("削除するcodeを入力", , "th")


If Left(D_thcd, 2) <> "th" Or Len(D_thcd) <> 5 Then

MsgBox ("codeが間違っています")

End

Else

'--------------------------------------------
' 指定したcodeの文字を赤にする

For k = 5 To 33

For i = 2 To 53

If Cells(k, i) = D_thcd Then

Cells(k, i).Font.ColorIndex = 3
Cnt = Cnt + 1

End If

Next i

Next k

End If

'-------------------------------------------------
' 指定した文字を削除し、背景を着色する

Rtn = MsgBox("削除しますか", vbYesNo, Cnt & "件あります")

If Rtn = vbYes Then

For k = 5 To 33

For i = 2 To 53

If Cells(k, i) = D_thcd Then

Cells(k, i) = ""
Cells(k, i).Interior.ColorIndex = 19 'セルを着色する

End If

Next i

Next k

Else

End

End If

'
End Sub




わかお かずまさ
VegaSystems

📷📷📷📷📷📷
#LAN_PRO
#Bloguru
#kuma

People Who Wowed This Post

×
  • If you are a bloguru member, please login.
    Login
  • If you are not a bloguru member, you may request a free account here:
    Request Account