简体   繁体   中英

Open silent excel file from list to change value to specific cell

I am a little new to vba and it is hard for me to come up with code to do what I am about to explain, any help I can get it is extremely appreciated.

Sub FileNametoExcel()

Dim fnam As Variant
' fnam is an array of files returned from GetOpenFileName
' note that fnam is of type boolean if no array is returned.
' That is, if the user clicks on cancel in the file open dialog box, fnam is set to FALSE

Dim b As Integer 'counter for filname array
Dim b1 As Integer 'counter for finding \ in filename
Dim c As Integer 'extention marker

' format header
Range("A1").Select
ActiveCell.FormulaR1C1 = "Path and Filenames that had been selected to Rename"
Range("A1").Select
With Selection.Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
End With

' first open a blank sheet and go to top left ActiveWorkbook.Worksheets.Add

fnam = Application.GetOpenFilename("all files (*.*), *.*", 1, _
"Select Files to Fill Range", "Get Data", True)

If TypeName(fnam) = "Boolean" And Not (IsArray(fnam)) Then Exit Sub

'if user hits cancel, then end

For b = 1 To UBound(fnam)
' print out the filename (with path) into first column of new sheet
ActiveSheet.Cells(b + 1, 1) = fnam(b)
Next


End Sub

what I would like to do then is after I have the list of files on A:A is to open those workbooks and replace the value of b3 for =MID(CELL("filename"),SEARCH("[",CELL("filename"))+1,SEARCH(".xlsx",CELL("filename"))-SEARCH("[",CELL("filename"))-1) then to Save As without changing the path.

May be this is what you looking for else I failed to understand

Dim Wb As Workbook, rng As Range
Application.ScreenUpdating = False  ' Since you mentioned Silent
    For b = 1 To UBound(fnam)
    ThisWorkbook.ActiveSheet.Cells(b + 1, 1) = fnam(b)
    Set Wb = Workbooks.Open(fnam(b))
    Wb.Sheets(1).Range("B3").Formula = "=MID(CELL(" & Chr(34) & "filename" & Chr(34) & "),SEARCH(" & Chr(34) & "[" & Chr(34) & ",CELL(" & Chr(34) & "filename" & Chr(34) & "))+1,SEARCH(" & Chr(34) & ".xlsx" & Chr(34) & ",CELL(" & Chr(34) & "filename" & Chr(34) & "))-SEARCH(" & Chr(34) & "[" & Chr(34) & ",CELL(" & Chr(34) & "filename" & Chr(34) & "))-1)"
    Wb.Close True
    Next
Application.ScreenUpdating = True

I think silent means without seeing opened files

Sub FiletoExcel()
fnam = Application.GetOpenFilename("all files (*.xls*), *.xls*", 1, _
"Select Files to Fill Range", "Get Data", True)
If TypeName(fnam) = "Boolean" And Not (IsArray(fnam)) Then Exit Sub
Set exlApp = CreateObject("Excel.Application")
For b = 1 To UBound(fnam)
    Set Wb = exlApp.Workbooks.Open(fnam(b))
    Wb.Sheets(1).Range("B3").Formula = "=MID(CELL(" & Chr(34) & "filename" & Chr(34) & "),SEARCH(" & Chr(34) & "[" & Chr(34) & ",CELL(" & Chr(34) & "filename" & Chr(34) & "))+1,SEARCH(" & Chr(34) & ".xlsx" & Chr(34) & ",CELL(" & Chr(34) & "filename" & Chr(34) & "))-SEARCH(" & Chr(34) & "[" & Chr(34) & ",CELL(" & Chr(34) & "filename" & Chr(34) & "))-1)"

    Wb.Close True
Next
exlApp.Quit
Set exlApp = Nothing
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM