Export data based on column name

swartzfeger

New Member
Joined
May 23, 2022
Messages
17
Platform
  1. Windows
  2. MacOS
Hi all, I have (very) limited programming skills and need a simple way to export data based on column name (the column will always be named "Missing #"). I found some VBA that would do the trick with one caveat -- there will always be a different number of columns in this worksheet, so the range of A1:?? will always be different. In the example below, the range is A1:P1... I need a way to determine the actual range for a given sheet. Would something like Range.End or Range.Find be useful?

Also, in the procedure below it simply selects the columns named "Missing #"... is there a way to export the data from the Missing # columns without writing another procedure?

Thanks!

VBA Code:
Sub FindMissing()
'11/14/2022
    Dim xRg As Range
    Dim xRgUni As Range
    Dim xFirstAddress As String
    Dim xStr As String
    On Error Resume Next
    xStr = "Missing #"
    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
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try changing:
VBA Code:
Set xRg = Range("A1:P1").FindNext(xRg)
to:
VBA Code:
Set xRg = Rows(1).FindNext(xRg)
What do you mean by:

Sorry, by export I mean -- select all columns named "Missing #" and export them as a .csv

Also, do I change the row

VBA Code:
Set xRg = Range("A1:P1").Find(xStr, , xlValues, xlWhole, , , True)

to

VBA Code:
Set xRg = Rows(1).FindNext(xRg)

?

Thanks so much for your help!
 
Upvote 0
Change the file name (in red) to suit your needs. The cvs file will be saved in the same folder as the workbook containing the macro.
Rich (BB code):
Sub FindMissing()
    Application.ScreenUpdating = False
    Dim xRg As Range, xRgUni As Range, xFirstAddress As String, xStr As String
    xStr = "Missing #"
    Set xRg = Rows(1).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.Copy
    Workbooks.Add
    ActiveSheet.Paste
    fName = ActiveWorkbook.Path & "\FileNameHere" & ".csv"
    With ActiveWorkbook
        .SaveAs Filename:=fName, FileFormat:=xlCSV, CreateBackup:=False
        .Close False
    End With
    With Application
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
End Sub
 
Upvote 0
Change the file name (in red) to suit your needs. The cvs file will be saved in the same folder as the workbook containing the macro.
Rich (BB code):
Sub FindMissing()
    Application.ScreenUpdating = False
    Dim xRg As Range, xRgUni As Range, xFirstAddress As String, xStr As String
    xStr = "Missing #"
    Set xRg = Rows(1).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.Copy
    Workbooks.Add
    ActiveSheet.Paste
    fName = ActiveWorkbook.Path & "\FileNameHere" & ".csv"
    With ActiveWorkbook
        .SaveAs Filename:=fName, FileFormat:=xlCSV, CreateBackup:=False
        .Close False
    End With
    With Application
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
End Sub
Mumps, this *almost* works. But I see the following error:

1668454089011.png


It successfully opens a second sheet with only the exported columns, but does not save it as a .csv due to this run-time error. I've checked Trust Center settings and can't seem to resolve this.

Thanks for your help so far!
 
Upvote 0
When I debug, it's highlighting the following line:

VBA Code:
.SaveAs Filename:=fName, FileFormat:=xlCSV, CreateBackup:=False
 
Upvote 0
Try:
VBA Code:
Sub FindMissing()
    Application.ScreenUpdating = False
    Dim xRg As Range, xRgUni As Range, xFirstAddress As String, xStr As String, srcWB As Workbook
    Set srcWB = ThisWorkbook
    xStr = "Missing #"
    Set xRg = Rows(1).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.Copy
    Workbooks.Add
    ActiveSheet.Paste
    fName = srcWB.Path & "\FileNameHere" & ".csv"
    With ActiveWorkbook
        .SaveAs Filename:=fName, FileFormat:=xlCSV, CreateBackup:=False
        .Close False
    End With
    With Application
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,650
Messages
6,126,021
Members
449,281
Latest member
redwine77

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