Loop with selection not working

opazzo

Board Regular
Joined
Dec 21, 2005
Messages
69
Office Version
  1. 365
Platform
  1. Windows
Hi there,

Here is my code, not working for some reasons I can't understand. Hope somebody here can.

What I am trying to do is copy files with filename based on cell value. I want to copy only those cells that I select. It is working fine if i select one cell at a time, but doesn't work when I select multiple cells. I believe there is something wrong with my loop.


<VB>

GetFolder ("c:/")
dest = dest + "/"

Col = ActiveCell.Column
Rw = ActiveCell.Row

If Col > 1 Then Exit Sub
If Rw < 6 Then Exit Sub

'set path
FPath = "\\shared drive\folder\"

For Each cell In Selection

'get file name from cell & adjust for formatting
ftx = cell.Offset(0, 6).Value
ftx = Right("00000" & ftx, 5)

'try to get the full path and name from the folder
FullName = FPath & ftx


fName = cell.Value
fName = Application.WorksheetFunction.Substitute(fName, "-", "")
fName = fName & "_" & ActiveCell.Offset(0, 1).Value & ".pdf"

'Search the file in FullName path
With Application.FileSearch
.NewSearch
.LookIn = FullName
.SearchSubFolders = False
.Filename = fName
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
Fil = .FoundFiles(1)
Else
MsgBox "There were no files found"
Exit Sub
End If
End With

'and copy file(s)
FileCopy Fil, dest + fName

Next cell

eMsg = MsgBox("Files Copied. Please verify all files have been copied", vbInformation)

End Sub

Function GetFolder(strPath As String) As String
Dim fldr As FileDialog
Dim sItem As String
Dim sInitDir As String
sInitDir = CurDir ' Store initial directory
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
dest = sItem
End Function

</VB>
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
a single cell as a range is the default item of the selection

Dim mycell As Range


for each mycell in selection
...

next mycell


did you mean to look at the value in the cell 6 columns over

ftx = cell.Offset(0, 6).Value

also

instead of

fName = Application.WorksheetFunction.Substitute(fName, "-", "")

try
fName = Replace(fName, "-", "")
 
Upvote 0
Charles,

I confirm that I need to get one value from the cell 6 columns over to form part of my filename. And thanks for the replace function that I'll use instead of substitute.


Regarding my core problem OK for the Range, but if i filter my list, then my code will parse all cells, not only the visible ones. On the other hand if I change that to

For Each cell In Range("A6:A" & LastRow).SpecialCells(xlCellTypeVisible)

then my code will NOT work when I select a single cell... I can't really understand why...

Any ideas ?
 
Upvote 0
Can't find a way out of this.

In a filtered list I select some cells with the mouse and run a macro, but my loop keep on processing all cells in "selection", including those cells that are not visible, ie filtered and not selected.

I use the following
For Each cell In Selection
Next cell

Any ideas ? Should I replace Selection by something else like visible cells in selection ? Any help with the procedure is welcome :)

Thanks
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,487
Members
452,917
Latest member
MrsMSalt

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