Loop through folder and Copy values and Formats of first sheet - excel

I have been working through VBA code that copies the first worksheet in a folder to another workbook.
The code copies the data correctly but upon saving I get certain cells with errors (#Ref) this is due some of the copied cells having formula.
I would like the copied to data to retain the original formatting but to only have values. Or alternatively the cells with the 2 errors are M11 and O11 which have an index match formula, if these 2 cells values could be pasted without formula the rest of the copied data will be fine.
Any help will be appreciated.
I have tried to use PasteSpecial xlPasteValuesAndNumberFormats and .PasteSpecial xlPasteFormats but I am not sure how to amend the copy function.
Sub MergeMultipleWorkbooks()
Dim Path, Filename As String
Path = "C:\Users\User\Desktop\ProMacro\"
Filename = Dir(Path & "*.xlsx")
Do While Filename <> ""
With Workbooks.Open(Filename:=Path & Filename, ReadOnly:=True)
.Worksheets(1).Copy After:=ThisWorkbook.Sheets(1)
.Close False
End With
Filename = Dir()
Loop
MsgBox "Files has been copied Successfull", , "MergeMultipleExcelFiles"
End Sub
The code copies the first sheet from the designated file in the folder, my only issue is that certain cells will have a #Ref when saving the file as the formulas having being copied.

When the workbook is opened, copy all the contents of the sheet and paste it to the same sheet as values. It's the code you put between
With Workbooks.Open(Filename:=Path & Filename, ReadOnly:=True) and .Worksheets(1).Copy After:=ThisWorkbook.Sheets(1)
Formatting is not changed and formulas are replaced by values.
It's not a very friendly solution, but nothing other comes to my mind after contemplating and trying to find out a good one.

Related

Formula referencing from other workbooks

I'm quite new to VBA and I'm having trouble debugging a certain code. What I want is a cell formula that has the formula link to another spreadsheet. However, I want to add cells from multiple workbooks. For example, if we had workbook1 and workbook 2. I want in cell F10 in final workbook to have formula reading '[workbook1]Sheet1'!!F10' + '[workbook2]Sheet1'!!F10'
I like to make the formula as flexible and have the following conditions
I like to have an open directory that lets me select excel files that I want as part of the formula
I can add as many external spreadsheets as possible
The final spreadsheet initially will have zeroes in them. I want to replace this with a formula link.
How i decided to code this is by first replacing the zero cell of the final workbook with cell F10 of first excel file selected from a directory. Once this step is done, any additional workbooks selected from directory will add on as an extra formula link to the cell. Below is a code I attempted but I can not figure why it doesn't work. Could anyone please let me know what is going wrong? Thanks.
Sub Sum_workbooks_Form()
Dim FileNameXls, f
Dim wb As Workbook, i As Integer
FileNameXls = Application.GetOpenFilename(filefilter:="Excel Files, *.xl*", MultiSelect:=True)
If Not IsArray(FileNameXls) Then Exit Sub
For Each f In FileNameXls
Set wb = Workbooks.Open(f)
If ThisWorkbook.Sheets("Sheet1").Cells(11, 6).Value = 0 Then
ThisWorkbook.Sheets("Sheet1").Cells(11, 6).Formula = "=[" & wb.Name & "]Sheet1!" & Cell(11, 6).Name
Else
ThisWorkbook.Sheets("Sheet1").Cells(11, 6).Formula = "=[" & ThisWorkbook.Name & "]Sheet1!" & Cell(11, 6).Name & " + [" & wb.Name & "]Sheet1!" & Cell(11, 6).Name
End If
wb.Close SaveChanges:=False
Next f
End Sub
Well, normally a reference to cell F10 of Sheet1 of Book2 is expressed like this in a formula:
=[Book2]Sheet1!$F$10
Is it possible for you to reference the Sheet NAME instead of the NUMBER?
If yes, the first of your assignments should look like this:
ThisWorkbook.Sheets(9).Cells(11, 6).Formula = "=[" & wb.Name & "]Sheet1!$F$10"
Where Sheet1 is of course the name of your Sheets(9)

use VBA in excel to get data from closed workbooks with multiple file names

I have a workbook that has a list of excel file names in one column and I need to use those file names to have a macro get data from one cell from each one of those files in the list. Where do I begin? Sorry for such a dumb question.
workbook with data sheet
I would like to get data from a workbook whose name comes from column K, with the worksheet name coming from column L, and always cell A13. And put that value in the correct row in column M.
This is what you have to do :
Iterate through each of the names (lets say the list range is A1:A5)
Open that workbook.
Get the value from the cell
Paste that value on main workbook
Close workbook and go to the next one
This is how you do this:
Sub CopyFromWorkbooks()
Dim path As String
path = "THE PATH WERE THOSE WORKBOOKS ARE SAVED (Eg: "C:\Users\YOU\Documents")"
For Each cell In ThisWorkbook.Sheets("YOUR_SHEET_NAME").Range("A1:A5")
'Open the workbook
Workbooks.Open Filename:=path & "\" & cell.Value 'adding workbook name and path to get full workbook path
'Copy cell value to this workbook
Workbooks(cell.Value).Sheets("YOUR_SHEET_NAME").Range("THE RANGE TO COPY").Copy
'Paste copied value in this workbook
ThisWorkbook.Sheets("YOUR_SHEET_NAME").Range("THE RANGE TO PASTE").PasteSpecial Paste:=xlPasteValues
'Close Workbook
Workbooks(cell.Value).Close True
Next cell
End Sub
This code as is will paste the copied data always on the same cell if you need help with that just write back ;)

VBA Excel 2013 Copy / paste - ensuring paste is a value

I am copying data from one workbook/ sheet to another workbook / sheet, extract of code below, and the Range (I119,I65) copy/pastes fine. My problem is that the ProvId copy/ paste sometimes enters the 4 digit ID as expected but on other times it puts "Today's Date". The occasions when this happens is when the Cell D9 is a formula. How do I ensure that ProvId is always the value of D9, in excel the Value function would work.
Workbooks.Open Filename:=path & excelfile, UpdateLinks:=False 'Open each upload excel file
ActiveWorkbook.Sheets("Audit Grant Return").Range("I19,I65").Copy
ProvId = Cell("D9")
MsgBox ("Id" & ProvId) 'To see what is happening
ActiveWorkbook.Close
Workbooks("15-16 AGR Data Imported.xlsm").Sheets(1).Activate
Sheets("Upload").Select
ActiveSheet.Range(Cells(2, ActiveRow), Cells(2, ActiveRow)) = ProvId
ActiveSheet.Range(Cells(4, ActiveRow), Cells(4, ActiveRow)).PasteSpecial
ProvId = ""
ActiveRow = ActiveRow + 1
This loops round picking up all the files to be uploaded and works fine except for this ProvId issue. FYI I have Dim ProvId As Text 'stores Provider Id from upload file

Externally Referencing Data with Variable Sheet Names

I am trying to import a range of cells from a closed workbook.
I use the external reference link built into Excel:
='F:\UGR\JOB DATA SHEET\[JOB SHEETS 1-500.xlsx]JobNumber'!B4
='F:\UGR\JOB DATA SHEET\[JOB SHEETS 1-500.xlsx]JobNumber'!B5
...
Going down the column from B4:B23 and replicating that for columns B-Z.
This works if the sheet name doesn't change. But that file contains sheets for Jobs 1 - 500, each on their own sheet. I am trying to pull those columns of data for whatever JobNumber gets entered into cell "B7". So ideally it would look like this:
='F:\UGR\JOB DATA SHEET\[JOB SHEETS 1-500.xlsx]&B7&'!B4
='F:\UGR\JOB DATA SHEET\[JOB SHEETS 1-500.xlsx]&B7&'!B5
...
Etc.
I know this won't work without the Indirect function, but I need to have the other file open for that to work. This isn't practical given the number of users who are using this file for reference.
I found a macro in VBA that should do what I need, but I can't get it to work. Here is the base macro before I started messing around with it.
Function GetValue(Path, File, Sheet, Ref)
'Retrieves a value from a closed workbook
Dim Arg As String
'Make sure the file exists
If Right(Path, 1) <> "\" Then Path = Path & "\"
If Dir(Path & File) = "" Then
GetValue = "File not Found"
Exit Function
End If
'Create the argument
Arg = "'" & Path & "[" & File & "]" & Sheet & "'!" & Range(Ref.Range("A1").Address(, , xlR1C1))
'Execute XLM macro
GetValue = ExecuteExcel4Macro(Arg)
End Function
Any ideas on how to get it to work, or an alternative work around? I could also temporarily import the sheet to my other file and overwrite it when a new value is entered, thus importing another sheet from the other workbook, but that seems far more complex.
I am using Excel 2013.
UPDATE: I am closer to figuring it out but I cant get it to display anything but #Value errors. My formula looks like this in excel:
=GetValue(H11,H12,B7,B4)
Cell H11 = F:\UGR\JOB DATA SHEET\
Cell H12 = JOB SHEETS 1-500.xlsx
Cell B7 = The input cell where the user enters a JobNumber (aka sheet name).
Cell B4 = B4 (The cell I want to search on the external workbook)
Cell B4 is where I think the error lies. Will this macro be able to tell that it needs to search the external file at cell B4?
I figured it out. The macro cannot be launched from within the workbook itself, it must be done from VBA.

VBA Excel Book to Book copy cell value not formula

I have developed (with help from stackoverflow user: #xificurC ) a VBA macro in excel that copies over a certain range of cells and pastes them, in a table in a seperate workbook. But there are formula on these tables that are relating to pieces of data that are not available with the user permissions on my account. So my question is how can I copy over the actual face values of the cells instead of the formula.
'get all excel files (and only excel files) from specified folder
file_checks = Dir(path & "\*.xls*")
Do Until file_checks = ""
'open file
Set wkbklp = Workbooks.Open(path & "\" & file_checks)
Set wkshtlp = wkbklp.Sheets(1)
'copy data away from original
Set data_return = wkshtlp.Range(start_cell).CurrentRegion
data_return.Copy check_sheet.Cells(check_sheet.Rows.Count, "B").End(xlUp).Offset(1, 0)
'close file without saving to make it all more automated
wkbklp.Close False
'loop through files until all are done
file_checks = Dir
Loop
That was the code that transfers the data from one book to another. I have not found a command that does this, but I'm only amateur so may have used the wrong key words.
Try using pastespecial.
data_return.Copy
check_sheet.Cells(check_sheet.Rows.Count, "B").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
You can use the pastespecial method, I don't see where are you pasting but it will be something like
Range.PasteSpecial Paste = xlPasteValues

Resources