Is it possible to implement an export/import feature for the unlocked cells in my Excel file? (Through VBA?)

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
578
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have made an Excel application, with protected worksheets and workbook, that I have updated and improved over the last several months, and it continues to evolve as I add more features and code as the need arises. One issue though is that each time I make an updated version, I have to manually copy/paste the data from all unlocked segments into the new file.

I'm hoping that it would be possible to implement an export/import feature in my application such that I would press an export button that would save the unlocked data in a file (perhaps through a "save as" window", and then I would press an import button and select the saved data (perhaps through an "open" window) and have all the data be populated in the correct cells.

If possible, it would be fabulous, and I would highly appreciate the help from the VBA experts here :) 🤗
 
Can you add the msgbox below before With rngCurrent and see if it is pointing to the right sheet and data range ?
Does what you see make sense ?

If not try going into Find. Select the format drop down. In the Protection tab clear the Locked cell check box. Hit find all.
Is it finding your unlocked cells.

VBA Code:
    MsgBox "Find Area:     " & rngCurrent.Parent.Name & " > " & rngCurrent.Address
    With rngCurrent
        Set FirstCell = .Cells.Find(What:="", After:=.Cells(1, 1), _
                            LookIn:=xlFormulas, LookAt:=xlPart, _
                            SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                            MatchCase:=False, SearchFormat:=True)
    End With

1660291707174.png
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
The second part of your answer helped figure out what was going on. My sheet was protected, and that was the reason your code wasn't working/generating the new workbook. After unprotecting the sheet, it worked like a charm, and it took 23 seconds, so not bad at all given such an enormous range of 29,691 cells. This was very exciting actually 🍻

So will you be able to tweak the code such that it works with protected sheets as well? The actual workbook application will be used by students and leaving them unprotected would be risky 😅

So whenever, you get a chance, we can move to the next phase hopefully. I'm super pumped now 😅

Thank you!
 
Upvote 0
Can you show me your protection settings ?
If I protect the sheet, it still works for me (even if I don't allow selection of unlocked cells)
 
Upvote 0
I still need an answer to the previous post but try the below.
It contains 2 macros
• ExportData
- this will create a csv file with the same filename and path but with the addition of yyyymmdd_hhmmss.csv
• ImportData
- will ask you to find the file to import

I have left the timer message box in the code for now.
You need to update this line in both macros (2 places in total)
Set wsCurrentMain = wbCurrent.Worksheets("Main Data") ' <--- Change this to your sheet with 29k unlocked cells

It works for me on a protected sheet. If it doesn't work on a protected sheet then confirm it works in principle on an unprotected sheet and we will need to troubleshoot what your protection settings are.

VBA Code:
Sub ExportData()

    Dim wbCurrent As Workbook, wbExport As Workbook
    Dim wsCurrentMain As Worksheet, wsExportMain As Worksheet
    Dim rngCurrent As Range
    Dim cellUnlock As Range
    Dim arrUnlock As Variant
    Dim i As Long
    Dim FirstCell As Range
    Dim CurrCell As Range
    Dim ExportDateTime As Date
    Dim fnameFull As String, ExportFName As String
    
    Dim startTime As Double
    startTime = Timer

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    Set wbCurrent = ThisWorkbook
    Set wsCurrentMain = wbCurrent.Worksheets("Main Data")       ' <--- Change this to your sheet with 29k unlocked cells
    Set rngCurrent = wsCurrentMain.UsedRange
    fnameFull = wbCurrent.FullName

    ReDim arrUnlock(1 To rngCurrent.Cells.Count, 1 To 1)
        
    With Application.FindFormat
        .Clear
        .Locked = False
    End With
    
    With rngCurrent
        Set FirstCell = .Cells.Find(What:="", After:=.Cells(1, 1), _
                            LookIn:=xlFormulas, LookAt:=xlPart, _
                            SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                            MatchCase:=False, SearchFormat:=True)
    End With

    If Not FirstCell Is Nothing Then
        Set CurrCell = FirstCell
        i = i + 1
        ExportDateTime = Now
        arrUnlock(i, 1) = "Export Run: " & ExportDateTime

        Do
            i = i + 1
            With CurrCell
                arrUnlock(i, 1) = .Parent.Name & "~" & .Address(0, 0, 1, 0) & "~" & .Formula
            End With

            Set CurrCell = rngCurrent.Cells.Find(What:="", After:=CurrCell, _
                LookIn:=xlFormulas, LookAt:=xlPart, _
                SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=True)
        Loop Until CurrCell.Address = FirstCell.Address
        
        arrUnlock(1, 1) = arrUnlock(1, 1) & " ~ No of Cells: " & i - 5

        Set wbExport = Workbooks.Add(xlWBATWorksheet)
        Set wsExportMain = ActiveSheet
        
        With wsExportMain
            .Range("A1").Resize(i).Value = arrUnlock
            .Columns(1).AutoFit
        End With
        
        With wbExport
            ExportFName = Left(fnameFull, InStrRev(fnameFull, ".") - 1) & Format(ExportDateTime, "_yyyymmdd_hhmmss") & ".csv"
            .SaveAs FileName:=ExportFName, FileFormat:=xlCSV
            .Saved = True
            .Close
        End With
    End If
      
    Application.FindFormat.Clear
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

    MsgBox "Duration in seconds: " & Timer - startTime
End Sub

Sub ImportData()

    Dim wbCurrent As Workbook, wbImport As Workbook
    Dim wsCurrentMain As Worksheet, wsImportMain As Worksheet
    Dim rngCurrent As Range, rngImport As Range
    Dim cellUnlock As Range
    Dim arrUnlock As Variant
    Dim i As Long
    Dim CurrCell As Range
    Dim fnameImport As String
    Dim cellImport As Variant
    Dim rCell As Variant
    
    Dim startTime As Double
    startTime = Timer
    
    Set wbCurrent = ThisWorkbook
    Set wsCurrentMain = wbCurrent.Worksheets("Main Data")       ' <--- Change this to your sheet with 29k unlocked cells
    Set rngCurrent = wsCurrentMain.UsedRange
     
    fnameImport = Application.GetOpenFilename(FileFilter:="CSV Files,*.csv", Title:="Select file to import", MultiSelect:=False)
    If fnameImport = "False" Then
        MsgBox "No file selected, exiting macro"
        GoTo CleanExit
    End If
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    Set wbImport = Workbooks.Open(FileName:=fnameImport, ReadOnly:=True)
    Set wsImportMain = ActiveSheet
    Set rngImport = wsImportMain.Range("A1").CurrentRegion.Columns(1)
    Set rngImport = rngImport.Resize(rngImport.Rows.Count - 1).Offset(1)    ' Exclude header in A1
       
    For Each rCell In rngImport.Cells
        ' format = sheetname ~ cell address ~ cell content
        cellImport = Split(rCell.Value, "~")
        With wbCurrent
            .Worksheets(cellImport(0)).Range(cellImport(1)).Formula = cellImport(2)
        End With
    Next rCell
    
    wbImport.Close SaveChanges:=False
    MsgBox "Duration in seconds: " & Timer - startTime
    
CleanExit:
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Yes, I tried addition of that one line of code, and it was pointing exactly to the right sheet and range. (It is pointing to A1:MA288)

I tried both your new macros, and they work like a charm on unprotected sheet. Actually the export took only 5 seconds and the import only 7 seconds, which is even better. I tried deleting all or portions of the data I had entered, and all the missing data reappeared after import. It's fabulous 🍻 I also tested your code in presence of Johnny's code (from this thread: Is there any way that 'filling' can be restricted to values only and not formatting, perhaps through VBA?) which I applied to the same sheet as well, and they worked together perfectly. Also none of my conditional formatting and data validation settings were affected.

But I tested with the sheet protected, and the export macro is not working. I only select the first two options (selecting locked and unlocked cells), and I didn't include a password for now. But the import worked very well with the sheet protected.

Looking forward to the next steps.

Thanks much!
 
Upvote 0
Oh sorry I missed that question. The only thing I do for protection is right-clicking on the sheet name, selecting protect sheet, then checking select locked and unlocked cells (only those two first check boxes) and then putting a password and confirming the password.

Is there a particular place (menu item etc.) that shows the protection settings applied? If something like this exists, I can take a screenshot and send you; also to make sure there are no other settings that I'm unaware of)

Also I was wondering if you could help me design a couple of buttons, one for import and one for export, that I could put in the "Info-Setup" sheet so the users can easily invoke the two functions when needed. This will be super nice.

The sheet names are:
1. "Comments"
2. "Info-Setup"
3. "Plates-Input"

Thank you so much 🤗
 
Upvote 0
Just updating that I played a bit with the file and was able to add the buttons myself. From the Developer tab, I selected "Insert\From Controls\button" and assigned import to one button and export to the other. The import worked beautifully as expected, but the export didn't.
 
Upvote 0
I am assuming the export issue is password related, can you confirm ?
Do you want to export all 3 sheets ?
 
Upvote 0
It is not password related. I just tested again without password.

And yes, all three sheets please.
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,103
Members
452,379
Latest member
IainTru

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