Delete column which starts with a value

xlPart really should be xlWhole otherwise the code would change a heading like "Awaken" to "ATRUE", even though it would not delete the column.

Both codes would be more robust with something, say, On Error Resume Next in case there were no headers with "Wake*" (eg the code accidentally gets run a second time)

Two very good points Peter.
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
xlPart really should be xlWhole otherwise the code would change a heading like "Awaken" to "ATRUE", even though it would not delete the column.

Also needs xlWhole specified in case another Find has previously been done with xlPart in which case a similar issue to the above would arise.
This code would of course also incorrectly delete any column that happened to be missing a heading in row 3.

Both codes would be more robust with something, say, On Error Resume Next in case there were no headers with "Wake*" (eg the code accidentally gets run a second time)
I was under the impression that using xlWhole would cause it to ignore anything that exceeded "Wake", but maybe not "Wake*". Haven't tried that yet, but will. tx Peter.
 
Upvote 0
something else to be considered in future use. Since the Range.Replace method uses the Find function as part of its execution, setting the LookAt,, SearchOder and MatchCase parameters in either the Find function or the Replace statement will determine the default setting for the next use of either the Find or the Replace. MS recommentds specifying these parameters on each use to avoid inadvertant results. I was aware of this on the Find statement, but did not realize the the two were linked within the vba application software. Learn something every day.?
 
Upvote 0
LookAt,, SearchOder and MatchCase ... MS recommentds specifying these parameters on each use to avoid inadvertant results.
Exactly, that's why I mentioned it
Also needs xlWhole specified in case another Find has previously been done with xlPart in which case a similar issue to the above would arise.
I din't mention MatchCase since the OP hasn't specified whether the match has to be case-specific or not & I din't mention SearchOrder since that is irrelevant to this question.
 
Upvote 0
xlPart really should be xlWhole otherwise the code would change a heading like "Awaken" to "ATRUE", even though it would not delete the column.

Also needs xlWhole specified in case another Find has previously been done with xlPart in which case a similar issue to the above would arise.
This code would of course also incorrectly delete any column that happened to be missing a heading in row 3.

Both codes would be more robust with something, say, On Error Resume Next in case there were no headers with "Wake*" (eg the code accidentally gets run a second time)
How to handle that issue... Can we have try catch to it
 
Upvote 0
How to handle that issue... Can we have try catch to it
Which issue?

If you mean the "Awaken" example I gave and you want a vba approach, try Joe4's code or Fluff or JLGWhiz codes with xlPart changed to xlWhole.

If you meant the issue of no "Wake*' headers then again try Joe4's code or add the blue lines below (as well as the red change) to Fluff's code or similar to JLGWhiz' code.

Rich (BB code):
Sub ExcelMaker()
   With Range("3:3")
      .Replace "Wake*", True, xlWhole, , False, , False, False
      On Error Resume Next
      .SpecialCells(xlConstants, xlLogical).EntireColumn.Delete
      On Error GoTo 0
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,848
Messages
6,121,917
Members
449,055
Latest member
KB13

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