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
Related
I have a macro that generates a copy of an excel under a new name while not overriding the original. It is used to generate a copy for all staff while protecting the original.
I am trying to create a similar file that saves a copy of the visible sheets with todays date in another sharepoint location, but my macro hits an error because the sheets I am copying contain tables.
Exact error: "cannot copy sheet that contains table" the macro fails on line "Sheets(myArray).Copy" any ideas would be greatly appreciated :-)
Dim myArray() As Variant
Dim i As Integer
Dim j As Integer
j = 0
For i = 1 To Sheets.Count
If Sheets(i).Visible = True Then
ReDim Preserve myArray(j)
myArray(j) = i
j = j + 1
End If
Next i
Sheets(myArray).Select
Sheets(myArray).Copy
ActiveWorkbook.SaveAs Filename:="Low Level" & newdate & ".xlsx",
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Save
ActiveWindow.Close
OK, according to this article the limitation is only that you cannot copy multiple sheets with tables at the same time using Sheets(..).Copy.
That is, you can copy one sheet with a table at a time with Sheets(..).Copy or alternatively, you can copy multiple sheets with tables using some other methods such as ActiveWindow.SelectedSheets.Copy.
(I have not tested this, so you will have to confirm for yourself)
I have a PowerPoint that I need to copy the numbers in text boxes to a specific excel file. I have decided to use a macro on the PowerPoint because the file will be shared with multiple users and I want to compile each file's data into a spreadsheet. My code works fine so far, but only copies to a specific row and thus overwrites the existing data. I am having trouble with the code for finding the next empty row. What I tried so far only seems to work when going from excel to excel.
rw = Range("A" & Rows.Count).End(xlUp).Offset(1).Select
I am a novice at best when it comes to VBA. Please help.
While xlsWB.Worksheets(1).Range("A" & row) <> ""
row = row + 1
Wend
This will find the next empty row and store the row number within the integer row. xlsWB is an Object which stores CreateObject("Excel.Application").Workbooks.Open("File Path")
Then, use the following syntax to fill in your cells.
xlsWB.Worksheets(1).Range("A" & row) = ""
Also, you can use a For loop to automate it.
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)
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.
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.