userform buttons

showboat098

New Member
Joined
Dec 3, 2012
Messages
17
I have a userform with a macro attached to scan through all files in a folder and match the caption to the first three letters in the file name. There are 21 check boxes one of which is a select all button. Right now it's set up as the following:


Dim j As Integer
Dim compDir As Object
Dim compDir2 As Object
Dim nameProd As String

Private Sub CommandButton1_Click()

Dim FromPath As String
Dim ToPath As String
Dim FSO As Object
Dim i As Integer
Dim Home1 As String
Dim Home2 As String
'''Dim FileInFromFolder As Object
Dim fileObj As Object
'Dim compDir As Object
Dim FileComp As Object
Dim Str1 As String
'Dim compDir2 As Object
Dim fileComp2 As Object
'Dim nameProd As String
'Dim j As Integer


Home1 = "Z:\home\Marketing\common\BDS Associates\Consultant Database Group\D - Quarterly Files & Notes\" & thisYear & thisQuarter & "\Semi-Annual PDM Review\eVestment\"
Home2 = "Z:\home\Marketing\common\BDS Associates\Consultant Database Group\D - Quarterly Files & Notes\" & thisYear & thisQuarter & "\Semi-Annual PDM Review\Mercer\"
'Set FSO = CreateObject("scripting.filesystemobject")


Set FSO = CreateObject("Scripting.FileSystemObject")
Set compDir = FSO.GetFolder("\\nasprodpmwin\marketing\common\BDS Associates\Consultant Database Group\D - Quarterly Files & Notes\" & thisYear & thisQuarter & "\Semi-Annual PDM Review\eVestment\")
Set compDir2 = FSO.GetFolder("\\nasprodpmwin\marketing\common\BDS Associates\Consultant Database Group\D - Quarterly Files & Notes\" & thisYear & thisQuarter & "\Semi-Annual PDM Review\Mercer\")
Set FileComp = compDir.Files
Set fileComp2 = compDir2.Files

For j = 1 To 20
If Me.Controls("checkbox" & j).Value = True Then
nameProd = Me.Controls("checkbox" & j).Caption
End If

For Each fileObj In FileComp
Str1 = Left(fileObj.Name, 3)
If Str1 = Left(nameProd, 3) Then
FromPath = fileObj
ToPath = Home1 & nameProd & "\" & fileObj.Name
FSO.MoveFile source:=FromPath, Destination:=ToPath
End If
Next
For Each fileObj In fileComp2
Str1 = Left(fileObj.Name, 3)
If Str1 = Left(nameProd, 3) Then
FromPath = fileObj
ToPath = Home2 & nameProd & "\" & fileObj.Name
FSO.MoveFile source:=FromPath, Destination:=ToPath
End If

Next

Next j


It looks like setting j=1 to 20 makes it compare each caption to each file. I'd rather it only compare the active checkbox captions to the files. Going through that loop 20 times when only one box is checked is a bit excessive and slows the macro a tone. Any suggestions?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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