SamarthSalunkhe
Board Regular
- Joined
- Jun 14, 2021
- Messages
- 103
- Office Version
- 2016
- Platform
- Windows
I am trying to use user form for below code which one is working perfectly with cell reference for destination folder but after changing destination folder reference to Userform textbox reference it is not working.
Can someone help me in this.
Below is the original code which one is working fine.
Code after changing reference of User form.
Can someone help me in this.
Below is the original code which one is working fine.
VBA Code:
Function ExtractZip() As Boolean
On Error GoTo ErrorHandler
Dim localZipFile As Variant
Dim destFolder As String
Dim ws As Worksheet
Dim Sh As Object
Dim zipFiles As Object
Set ws = ThisWorkbook.Sheets(1)
' Prompt user to select the zip file
localZipFile = Application.GetOpenFilename("Zip Files (*.zip), *.zip", , "Select Zip File")
' Check if user canceled or did not select a file
If localZipFile = False Then
MsgBox "No zip file selected.", vbExclamation
ExtractZip = False
Exit Function
End If
' Get the destination folder path from cell A2
destFolder = ws.Range("A2").Value
If Len(destFolder) = 0 Then
MsgBox "Error: Please provide a valid destination folder path in cell A2.", vbExclamation
ExtractZip = False
Exit Function
End If
' Check if the zip file exists
If Dir(localZipFile) = "" Then
MsgBox "Error: The zip file specified does not have any Excel file.", vbExclamation
ExtractZip = False
Exit Function
End If
' Check if there's more than one file in the zip file
Set Sh = CreateObject("Shell.Application")
Set zipFiles = Sh.Namespace(localZipFile).Items
If zipFiles.Count > 1 Then
MsgBox "Error: Multiple files found in the zip file. Please ensure only one file is present.", vbExclamation
ExtractZip = False
Exit Function
End If
' Check if there's more than one file in the destination folder
Set zipFiles = Sh.Namespace(destFolder).Items
If zipFiles.Count > 1 Then
MsgBox "Error: Multiple files found in the destination folder. Please ensure only one file is present.", vbExclamation
ExtractZip = False
Exit Function
End If
' Extract the files from the zip file
With Sh
.Namespace(destFolder).CopyHere .Namespace(localZipFile).Items
End With
ExtractZip = True
Exit Function
ErrorHandler:
MsgBox "Error: " & Err.Description, vbExclamation
ExtractZip = False
End Function
Code after changing reference of User form.
VBA Code:
Function ExtractZip() As Boolean
On Error GoTo ErrorHandler
Dim localZipFile As Variant
Dim destFolder As String
Dim Sh As Object
Dim zipFiles As Object
' Prompt user to select the zip file
localZipFile = Application.GetOpenFilename("Zip Files (*.zip), *.zip", , "Select Zip File")
' Check if user canceled or did not select a file
If localZipFile = False Then
MsgBox "No zip file selected.", vbExclamation
ExtractZip = False
Exit Function
End If
' Get the destination folder path from Textbox1
destFolder = Trim(UserForm1.TextBox1.Value)
If Len(destFolder) = 0 Then
MsgBox "Error: Please provide a valid destination folder path in cell A2.", vbExclamation
ExtractZip = False
Exit Function
End If
' Check if the zip file exists
If Dir(localZipFile) = "" Then
MsgBox "Error: The zip file specified does not have any Excel file.", vbExclamation
ExtractZip = False
Exit Function
End If
' Check if there's more than one file in the zip file
Set Sh = CreateObject("Shell.Application")
Set zipFiles = Sh.Namespace(localZipFile).Items
If zipFiles.Count > 1 Then
MsgBox "Error: Multiple files found in the zip file. Please ensure only one file is present.", vbExclamation
ExtractZip = False
Exit Function
End If
' Check if there's more than one file in the destination folder
Set zipFiles = Sh.Namespace(destFolder).Items
If zipFiles.Count > 1 Then
MsgBox "Error: Multiple files found in the destination folder. Please ensure only one file is present.", vbExclamation
ExtractZip = False
Exit Function
End If
' Extract the files from the zip file
With Sh
.Namespace(destFolder).CopyHere .Namespace(localZipFile).Items
End With
ExtractZip = True
Exit Function
ErrorHandler:
MsgBox "Error: " & Err.Description, vbExclamation
ExtractZip = False
End Function