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
Related
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 have received the attached Excel file from my friend who wishes to convert into English(US).
While there is no Arabic text but only numerals, I am not able to work. If I copy and paste the cells into a new workbook, even the formats are saved. The numbers have spaces in between and I tried using Trim function.
Even if the spaces are deleted manually, the number is still shown on the right side of the formula bar. Pls help.
There are two settings regarding text direction and column layout within File, Option.
file, options, advanced, display, default direction.
... and,
file, options, advanced, display options for this worksheet, show sheet right to left
One way is to print to PDF, then open the PDF and export as spreadsheet. It's not perfect but I haven't found any of the other ways to reverse this.
This (https://www.extendoffice.com/documents/excel/1763-excel-change-sheet-direction.html) seemed helpful and may work for others but did not work for me, nothing did - the workbook that I had may have somehow been locked in this regard (I'm not sure if that's a thing but nothing I tried was correcting it)
Another possible solution is to open a new workbook with the correct structure and run a VBA script that copies, one sheet at a time and one cell at a time, from the Arabic-orientated workbook to the Western orientation. If there are graphs it will get a bit more tricky with putting them in the correct place but I'm sure for each sheet you could loop through each object, copy it and paste it in the other workbook and then move them around once there. Again not an easy or ideal solution, but a solution.
Here is a very simple implementation I'm using. It could be written in a more sophisticated way with objects, etc but this gets the job done. To use this, close all workbooks. Open the workbook you want to copy from. This is workbooks(1). Then open a new, blank workbook. Alt + F11 to bring up the VBA editor, add a module to your project and paste the code below.
Run step01, this creates the sheets in your new workbook.
In the new workbook, delete all default sheets - Sheet1, Sheet2 and Sheet 3.
Run step02. It can take some time as it's a slow method activating different workbooks. This will loop through every sheet, find the last cell and then loop through every cell. It will copy it, and paste it in the correct row order but in reverse column order in the new workbook.
Sub step01_create_sheets()
'create sheets in other workbook
Workbooks(1).Activate
For k = 1 To Workbooks(1).Sheets.Count
Workbooks(1).Activate
Workbooks(1).Sheets(k).Activate
val1 = Workbooks(1).ActiveSheet.Name
Workbooks(2).Activate
Workbooks(2).Sheets.Add(, Sheets(Sheets.Count)).Name = val1
Next k
End Sub
Sub step02_copy_sheet1()
Workbooks(1).Activate
Sheets(1).Activate
sheet_count = Workbooks(1).Sheets.Count
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For m = 1 To sheet_count
Workbooks(1).Activate
Sheets(m).Activate
row_count = Range("a1").SpecialCells(xlCellTypeLastCell).Row
col_count = Range("a1").SpecialCells(xlCellTypeLastCell).Column
For k = 1 To row_count
For j = 1 To col_count
Workbooks(1).Activate
Sheets(m).Activate
val2 = Cells(k, j).Formula
Workbooks(2).Activate
Sheets(m).Activate
Cells(k, col_count - j + 1 + 1).Formula = val2
DoEvents
Application.StatusBar = "Sheet " & m & " of " & sheet_count & " ; Row " & k & " of " & row_count & "; Column " & j & " of " & col_count
Next j
Next k
Next m
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
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.
I'm in a bad spot - any help pointing me in the right direction would be helpful and much appreciated.
I've created a Access process that copies an excel file and updates it with data from 2 record sets using Copy Recordset. The process now creates 39 workbooks by copying a 'template workbook' to a new file. Each workbook will contain at least one tab, but generally contain more, one workbook has over 20 tabs.
This is all done in Access. It copies the 'template' file and then runs a loop, and using Copy Recordset copies the 'template' worksheet to each tab.
After adding the tab, it loads the data from 2 record sets into the sheet. The 1st recordset loads one row of data - no problem. However the second loads multiple rows - which can vary in number.
This all works fine. The problem is formatting columns D through G.
If Column C = Revenue, Cost or Gross Margin (GM) I want that row's columns D through G formatted as currency.
If Column C = GM% then I want columns D through G formatted as a percentage.
If column C = Hours I don't have to format it.
What I would like is to create a function that formats these columns/rows based on Column C value, when the workbook is opened. I know there is an event that fires when the workbook is opened, and I know I have looped through all of the tabs in a workbook, o I'd want to run that function for each tab.
There may be one other issue - I think I can fix that in Access, but the EAC column is being exported as text and it should numeric. Not sure right now if that's being exported as Text - or EXCEL sees it as text.
Any help would be greatly appreciated. Thanks in advance.
I have images - but it won't let me post them just yet. If they would help - let me know, I'll try to email them to you.
Bob
I've come up with a way to do it, I've included the code.
However there may be a better way - so I'm open to suggestions.
Public Sub FormatTaskRows()
Dim Sheet As Object
Dim rngTasks As Range
Dim rngFCells As Range
Dim strTaskEnd As String
Dim ix As Integer
For Each Sheet In Sheets
If Sheet.Name = "Template" Then
Sheet.Visible = xlSheetVeryHidden
Else
Sheet.Visible = xlSheetVisible
Sheet.Activate
strTaskEnd = Range("C44").End(xlDown).Address
Set rngTasks = Range("$C$44:" & strTaskEnd)
'For Each Row In rngTasks
For ix = 1 To rngTasks.Rows.Count
Sheet.Unprotect
If rngTasks.Cells(ix) = "Revenue" Or rngTasks.Cells(ix) = "Cost" Or rngTasks.Cells(ix) = "Gross Margin (GM)" Then
Set rngFCells = Range(ActiveSheet.Name & "!D" & ix + 43 & ":G" & ix + 43)
rngFCells.Select
Selection.NumberFormat = "$#,##0.00"
Else
If rngTasks.Cells(ix) = "GM%" Then
Set rngFCells = Range(ActiveSheet.Name & "!D" & ix + 43 & ":G" & ix + 43)
rngFCells.Select
Selection.NumberFormat = "0.0000%"
End If
End If
Sheet.Protect
Next ix
End If
Next
End Sub