Macro or VBA to select first empty cell in a column invoked from a pop up with 3 options, Macro 1, Macro 2 or cancel

macrosmicrosoft excelvba

Windows 7

Excel 2016

Previous questions have been asked while I was using Excel 2007 but I finally managed to
bring home a Laptop from work that has Excel 2016 as this is the version this workbook would be used with.

I am trying with out success to adapt a macro that I found while googling my issue but I am unable to get it to work

VBA and/or Macros are very new to me so I wanted to ask for help so I may learn.

I have a workbook with a number of sheets that pull data from strings in Column A of a worksheet named Data_Import to which over time I will be adding new strings starting at the first empty cell in the A Column.

I already have a working macro that imports the new strings for me but that relies on the user manually checking and then selecting the first empty cell in A Column of the Data_Import page, or the user can clear all of the existing data and manually select cell A1 then run the import data macro.

As I will not be the only person using this working with this workbook it s makes sense to automate some of the tasks where possible using macros and pop up forms.

I have already created a tab on the ribbon bar called Data_Import which has a single button that a user can click from anywhere in the workbook and it runs the import macro but I need it to be more flexible with a couple of user selected options that I hope can work as follows:

  1. User clicks on the button on the Ribbon bar.
  2. Pop up option box appears with the following buttons for the user to click together with short caption above or in each button.
    Button 1 = Clear all existing data and import new data
    Button 2 = Add new data to the end of existing data
    Button 3 = Cancel

If a user clicks Button 1 a warning message pops up "WARNING – You are about to over write existing data are you sure? and then asks for a yes / no decision…

If yes button is selected the my existing Macro runs and clears all data in Column A of the Data_Import worksheet, selects Al and imports the new strings.

If no button is selected the option box is displayed and the user can choose button 2 or cancel all together.

If a user clicks Button 2 a warning message pops up "WARNING – You are about to add new data to the existing data are you sure? and then asks for a yes / no decision.

If yes button is selected a macro runs that would look at the range A:A in the Data_Import worksheet and select the first empty cell and import the new data starting at that cell.
So if there is data in Cells A1 - A100 then it should start to add the new strings at cell A101

If a user clicks Button 3 a warning message pops up "You have not imported any new data are you sure you want to cancel? yes / no.
If Yes is clicked the option box closes
If no is clicked the warning box closes thus allowing the user to make a new selection.

It seems very complicated but it is essentially 3 steps and (hopefully) two macros.

At the moment I have three macros called in succession to import the data.

1st Macro.
When you click the ribbon button, this is a pop up message prompt which then sets the Data_Import Page, selects all of column A, clears any data, selects cell A1. When finished it calls the 2nd macro named GetFolderNames

Sub Import_Data()
'Imports folder names into the Data_Import Sheet
'MsgBox function help:
'https://msdn.microsoft.com/en-us/library/office/gg251821%28v=office.15%29.aspx?f=255&MSPPError=-2147217396

Dim Answer As VbMsgBoxResult

    Answer = MsgBox("Are you sure you want to run the macro - Import Folder Names", vbYesNo, "Run Import Folder Names Macro")

    If Answer = vbYes Then
        Sheets("Data_Import").Select
        Columns("A:A").Select
        Selection.ClearContents
        Range("A1").Select

        Call GetFolderNames
    End If

End Sub

2nd Macro (not my code and most of it is lost on me, for now).
This does the data import once the user has selected the folder from which the data is to be imported. Once completed it calls the third macro named Column_Autofit

Sub GetFolderNames()

    Dim xRow&, vSF
    Dim xDirect$, InitialFoldr$

    InitialFoldr$ = "F:\" '<<< Startup folder to begin searching from

    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder to list Files from"
        .InitialFileName = InitialFoldr$
        .Show
        If .SelectedItems.Count <> 0 Then
            xDirect$ = .SelectedItems(1) & "\"
        End If
    End With

    If xDirect$ <> "" Then
        With CreateObject("Scripting.FileSystemObject").GetFolder(xDirect$)
            For Each vSF In .subfolders
                ActiveCell.Offset(xRow) = Mid(vSF, InStrRev(vSF, "\") + 1)
                xRow = xRow + 1
            Next vSF
        End With

        Call Column_Autofit

    End If
End Sub

3rd Macro.
This sets Column A to autofit width

Sub Column_Autofit()

    Columns("A:A").AutoFit

End Sub

As far as a Macro looking for the first empty cell in column A of the Data_Import Sheet the best thing that I could find is the code below (not mine) but I am unable to get it to work as my skill level is frankly no where near good enough, but I am determined to learn.

Sub SelectFirstBlankCell()
    Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
    Dim currentRowValue As String

    Sheets("Data_Import").Select
        Columns("A:A").Select

    sourceCol = 1
    rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row

    'for every row, find the first blank cell and select it
    For currentRow = 1 To rowCount
        currentRowValue = Cells(currentRow, sourceCol).Value
        If IsEmpty(currentRowValue) Or currentRowValue = "" Then
            Cells(currentRow, sourceCol).Select

    Call GetFolderNames

        End If
    Next
End Sub

I feel as if the answer is just there in front of me but I am unable to see it…
Any suggestions would be very much appreciated.

Best Answer

Try this code - note it is a combination of all your macros:

Sub GetFolderNames()
    Dim Answer As VbMsgBoxResult
    Dim xRow As Long
    Dim vSF As Object
    Dim xDirect$
    Dim InitialFoldr$
    Dim ws As Worksheet: Set ws = Sheets("Data_Import")
    Answer = MsgBox("Are you sure you want to run the macro - Import Folder Names", vbYesNo, "Run Import Folder Names Macro")
    If Answer = vbYes Then
        Application.ScreenUpdating = False
        xRow = ws.Cells(Rows.Count, 1).End(xlUp).Row + 1
        InitialFoldr$ = "F:\" '<<< Startup folder to begin searching from
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = Application.DefaultFilePath & "\"
            .Title = "Please select a folder to list Files from"
            .InitialFileName = InitialFoldr$
            .Show
            If .SelectedItems.Count <> 0 Then
                xDirect$ = .SelectedItems(1) & "\"
            End If
        End With
        If xDirect$ <> "" Then
            With CreateObject("Scripting.FileSystemObject").GetFolder(xDirect$)
                For Each vSF In .subfolders
                    ws.Cells(xRow, 1) = Mid(vSF, InStrRev(vSF, "\") + 1)
                    xRow = xRow + 1
                Next vSF
            End With
            ws.Columns("A:A").AutoFit
        End If
    End If
End Sub

Change Sheets("Data_Import") on line 7 to be whatever sheet you are working with. Let me know how it goes.

I recommend if you want quicker responses in future to be a little more succinct in your question, that was a lot of text.

Regards Justin

Related Question