Help with code

nat30

New Member
Joined
Feb 16, 2010
Messages
4
I'm a complete code novice - can someone help with two problems I'm having since upgrading to Excel 2007?

I understand that FileSearch is no longer used in Excel - how can I rewrite this code to work?
Private Sub CommandButton2_Click()
Dim x As String
Dim y As String
Dim z As String
z = ActiveSheet.Cells(2, 6).Value
x = ActiveSheet.Cells(1, 6).Value
x = x & ".pdf"
y = ActiveSheet.Cells(3, 6).Value
Set fs = Application.FileSearch
With fs
.LookIn = z
.Filename = x
If .Execute > 0 Then
ActiveWorkbook.FollowHyperlink Address:=(y), _
NewWindow:=True
Else
MsgBox "No signature files were found."
End If
End With
End Sub


Also, how can I change this code to refresh the Active sheet only? At the moment it refreshes all sheets
Private Sub SEARCHUNIT_Click()
Range("A5").Select
ThisWorkbook.RefreshAll
End Sub


I've spent hours trying to figure these out with no success - Help appreciated - Thanks
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Thanks meldoc
But I'm a complete novice to coding and can't work out how to rewrite it. I've tried substituting Application.FileSearch with FileSearch and it didn't work. I've also tried New AltFileSearch
I don't know how to rewrite the code.

Thanks
 
Upvote 0
I took your code and set it up in a way that should work in 2007.

This is untested as I do not have your data for your x, y, z variables:
Code:
Private Sub CommandButton2_Click()
Dim basebook As Workbook
Dim mybook As Workbook
Dim rnum As Long
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String
Dim Cnum As Integer
Dim cell As Range

'Set Drive and Folder variables
    SaveDriveDir = CurDir
    MyPath = "C:\Documents and Settings" '< Change to Path you want to use...
    ChDrive MyPath
    ChDir MyPath
    FNames = Dir("*.xls")
'Check Folder for files
    If Len(FNames) = 0 Then
        MsgBox "No files in the Directory"
        ChDrive SaveDriveDir
        ChDir SaveDriveDir
        Exit Sub
    End If
'Turn off screen updating
    Application.ScreenUpdating = False
'Set workbook variable
    Set basebook = ThisWorkbook
''clear all cells on active worksheet
'    basebook.Worksheets("Sheet1").Cells.Clear

z = ActiveSheet.Cells(2, 6).Value
x = ActiveSheet.Cells(1, 6).Value
x = x & ".pdf"
y = ActiveSheet.Cells(3, 6).Value

rnum = 1
'Loop thru file names
    Do While FNames <> ""
        Set mybook = Workbooks.Open(FNames)
            ActiveWorkbook.FollowHyperlink Address:=(y), _
            NewWindow:=True
        
'        ' This will add the workbook name in column A
'        basebook.Worksheets(1).Cells(rnum, "A").Value = mybook.Name
'        ' Copy the cell values from each cell in one row starting in column B
'        Cnum = 2
'            For Each cell In mybook.Worksheets(1).Range("B1,B2,B3,B4,B5")
'                basebook.Worksheets(1).Cells(rnum, Cnum).Value = cell.Value
'                Cnum = Cnum + 1
'            Next cell
        mybook.Close False
        rnum = rnum + 1
        FNames = Dir()
    Loop
'Return to original Drive and Folder
    ChDrive SaveDriveDir
    ChDir SaveDriveDir
'Turn Screen Updating back on
    Application.ScreenUpdating = True
End Sub
Change the Path in the code as needed. I used "C:\My Documents and Settings".
 
Upvote 0

Forum statistics

Threads
1,216,153
Messages
6,129,176
Members
449,491
Latest member
maxim_sivakon

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