VBAl only works for a small amount time before failing

gint32

Board Regular
Joined
Oct 8, 2014
Messages
139
Hi Everyone. Having an issue with my vba below. Our developers every now and then (as per normal) update our system (crystal reports) by adding or deleting extra columns, I export these reports to a CSV Excel file then run my macro on the csv to delete and manipulate the columns and data within.

I trying to figure a way of amending my macro to prevent them from failing every time they insert a few extra columns, FYI the raw csv columns have headers which I eventually rename to something more meaningful and relevant.

Its been suggested that I try using advanced filtering with vba, but I am really not up on this method to take it any further so would need help with this route if this is a valid option.

Otherwise, here's a simple sample of what I have so far.

Code: <code> ' I really need the VBA to "FIND" the column header rather than test a specific column header value and data within.


If Range("O1").Value = "Some_HeaderInfo" And Range("O2").Value = "DATA_001" <code>And Range("O3").Value = "DATA_003"</code> Then

Application.CutCopyMode = False
Columns("O:P").Select
Selection.Delete Shift:=xlToLeft

end if</code>
 
Last edited:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi Everyone, I have had a search and I came across the following vba to search, its pretty close to suit my needs, Although, I just need a little help with amending the vba to get last row instead of selecting the entire column..Can anyone help me with this

Thanks
Sub FindAddressColumn()

' From https://www.extendoffice.com/documents/excel/4879-excel-select-column-by-header-name.html

Dim xRg As Range
Dim xRgUni As Range
Dim xFirstAddress As String
Dim xStr As String
Dim LastRow as Long

On Error Resume Next
xStr = "Name"
Set xRg = Range("A1:P1").Find(xStr, , xlValues, xlWhole, , , True)
If Not xRg Is Nothing Then
xFirstAddress = xRg.Address
''''Do
Set xRg = Range("A1:P1").FindNext(xRg)
If xRgUni Is Nothing Then
Set xRgUni = xRg
Else
Set xRgUni = Application.Union(xRgUni, xRg)
End If

'''Loop While (Not xRg Is Nothing) And (xRg.Address <> xFirstAddress)

End If

xRgUni.EntireColumn.Select ' need to select only down to lastrow used

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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