Seach for string in all colums and sheet and delete row(s) is perfect match

piddy

New Member
Joined
Feb 12, 2018
Messages
16
Hi

I hope you can help me. The following macro works but it also deletes columms that partly contains the string its searching for.


Code:
'Sub Remove_columns()
'    Dim wS As Worksheet
'    For Each wS In ThisWorkbook.Worksheets
'        With wS
'            For i = .Columns.Count To 1 Step -1
'                If InStr(1, LCase(.Cells(1, i)), LCase("Cprnr")) Then .Columns(i).EntireColumn.Delete
'                If InStr(1, LCase(.Cells(1, i)), LCase("Cvrnr")) Then .Columns(i).EntireColumn.Delete
'                If InStr(1, LCase(.Cells(1, i)), LCase("Navn")) Then .Columns(i).EntireColumn.Delete
'            Next i
'        End With 'wS
'    Next wS
'End Sub

So if a columm is called procesnavn it also deletes the columm. How can I alter the macro so it only deletes a columms if it's a 100 % match?

Thanks in advanced.

Kind regards
Thomas
 
Hi Rick

I got the following working now:

In a module:
Code:
[B][COLOR="#FF0000"]Global Has_GDPR_Run As Boolean[/COLOR][/B]

Sub Remove_GDPR()
Dim a As Long, w As Long, vdelcols As Variant, vcolndx As Variant
vdelcols = Array("Cprnr", "Cvrnr", "Navn")
[B][COLOR="#FF0000"]Has_GDPR_Run = True[/COLOR][/B]
With ThisWorkbook
    For w = 1 To .Worksheets.Count
        With Worksheets(w)
            For a = LBound(vdelcols) To UBound(vdelcols)
                vcolndx = Application.Match(vdelcols(a), .Rows(1), 0)
                If Not IsError(vcolndx) Then
                    .Columns(vcolndx).EntireColumn.Delete
                End If
            Next a
        End With
    Next w
End With
End Sub

If File is saved the following is run in Thisworkbook:


Code:
Option Explicit
 Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
     Static bolInProcess As Boolean

     Call Remove_GDPR

     [B][COLOR="#FF0000"]If Has_GDPR_Run Then [/COLOR][/B]MsgBox "File is not saved without personal information"
 End Sub

The Msgbox now comes up every time I click save. Is there a way to code it so it only comes up when the "Remove_GDPR" macro has been run?
See if making the changes highlighted in red above makes the code do what you want. Note that the "Global" line of code exists on a line by itself outside of any procedures.
 
Last edited:
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I get the following error:

"Compile error: Only comments may appear after end sub, end function or end property"

I think its caused by the
Code:
Global Has_GDPR_run as Boolean

Should that line be a specific place so it doesn't cause problems?
 
Last edited:
Upvote 0
I get the following error:

"Compile error: Only comments may appear after end sub, end function or end property"

I think its caused by the
Code:
Global Has_GDPR_run as Boolean

Should that line be a specific place so it doesn't cause problems?
It should be at the top of the module, above any and all of your procedures (functions, subs).
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top