I'm trying link to workbooks. Then Remove the link. The formula is working fine when the Full path is given but fails the moment a string is passed. In the below vba i'm trying to give the name of the location of the files from a cell value in Sheet1.
'Location of Template and Country
Cntryloc = """" & Sheet1.Range("B5") & """"
Debug.Print Cntryloc
TempLoc = "" & Sheet1.Range("B11") & ""
Finaltemplloc = Sheet1.Range("B17")
i=2
'Getting the name of excel Sheet
CntryExcel = Sheet1.Range("C5")
TempLoc = "" & Sheet1.Range("B11") & ""
Workbooks.Open TempLoc & "\" & "Bank" & ".xlsx", True, False
Workbooks("" & FName & ".xlsx").Activate
ActiveWorkbook.Unprotect Password:="Tall.Trees"
Worksheets("Template").Unprotect Password:="Tall.Trees"
Worksheets("Template").Range("D14").Formula = "='&"["&CntryExcel&"]Dump"&"'"&"!"&"$A$" & i""
ActiveWorkbook.BreakLink Name:=Cntryloc, Type:=xlExcelLinks
Worksheets("Template").Protect Password:="Tall.Trees"
ActiveWorkbook.Protect Password:="Tall.Trees"
'Location for Final Output
ActiveWorkbook.SaveAs Filename:=Finaltemplloc & "\" & Bank.xlsx
ActiveWorkbook.Close
try with this
Worksheets("Template").Range("D14").Value = "='[" & CntryExcel & "]Dump'!" & "$A$" & i & ActiveWorkbook.BreakLink & "Name:=" & Cntryloc & ", Type:=" & xlExcelLink
try this
Worksheets("Template").Range("D14").Formula = "='[" & CntryExcel & "]Dump!$A$" & "i"
that should fix the formula input
but check for CntryExcel to hold a workbook name and not a sheet name as per your comment preceeding its initialization ('Getting the name of excel Sheet)
Related
I'm trying to get this vba working. It reads the correct filestructure and it does find the first .xlsx and it import the needed data to the control.xlsm.
I notice that after it gets to fileName = dir() the fileName becomes empty. I read that it does this because it cannot find files that match the criteria, but what am i doing wrong?
Here is the code
Sub test_werk_final()
Application.ScreenUpdating = False
Application.CutCopyMode = False
Application.DisplayAlerts = False
Dim directory, fileName As String, sheet As Worksheet
directory = ThisWorkbook.Path & "\"
fileName = Dir(directory & "*.xlsx")
controlFile = Dir(directory & "control.xlsm")
lijn = 2
MsgBox "1" & directory & "2" & fileName & "3" & controlFile
Do Until fileName = ""
MsgBox "1" & directory & "2" & fileName & "3" & controlFile
Workbooks.Open fileName:=(directory & fileName)
MsgBox "1" & directory & "2" & fileName & "3" & controlFile
naam = Sheets("Sheet2").Range("A1").Value
leeftijd = Sheets("Sheet2").Range("A2").Value
Workbooks(controlFile).Worksheets("control").Cells(lijn, 1) = naam
Workbooks(controlFile).Worksheets("control").Cells(lijn, 2) = leeftijd
For Each sheet In Workbooks(fileName).Worksheets
naam = Workbooks(fileName).Worksheets.Range("A1").Value
leeftijd = Workbooks(fileName).Worksheets.Range("A2").Value
Workbooks(controlFile).Worksheets("control").Cells(lijn, 1) = naam
Workbooks(controlFile).Worksheets("control").Cells(lijn, 2) = leeftijd
Next sheet
Workbooks(fileName).Close
MsgBox "1" & directory & "2" & fileName & "3" & controlFile
lijn = lijn + 1
MsgBox "1" & directory & "2" & fileName & "3" & controlFile
fileName = Dir() ' volgende
MsgBox "1" & directory & "2" & fileName & "3" & controlFile
Loop
Application.ScreenUpdating = True
I'm not an expert coding guru, but i do posses basic programming skills.
P.S: i already looked for the questone on differnt fora en didn't find anything that could help me. Maybe i used the wrong search string.
Thanks in advance
You should simple change the order of the two Dir-statements:
controlFile = Dir(directory & "control.xlsm")
fileName = Dir(directory & "*.xlsx")
When you issue a Dir-command with parameter, a new search is started according to the pattern you pass. Dir-command without parameter fetches the next file matching that pattern. In your code, you started first a search with wildcards and then a second search with a fixed filename. When you start your loop, the dir-command will try to find another file with name control.xlsm and of course fails.
I am using Vlookup formula from another workbook in my code. The other workbook named as a variable TifuliWB Workbook but I keep getting an error run time error 1004. I am sure that it's such a small mistake of mine that stops the sub but I can't know what.
With MainWB.Worksheets(2)
LR = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("J2:J" & LR).FormulaR1C1 = _
"=VLOOKUP(RC[-8]," '"[" & TifuliWB.Worksheets(1) & "]"'"!C1:C71,65,FALSE)"
.Range("J2:J" & LR).NumberFormat = "m/d/yyyy"
.Cells.Copy
End With
Try referencing the columns' full external address instead of concatenating in the workbook and worksheet name.
.Range("J2:J" & LR).FormulaR1C1 = _
"=VLOOKUP(RC[-8]," & TifuliWB.Worksheets(1).range("A:BS").address(1, 1, external:=true, referencestyle:=xlr1c1) & ",65,FALSE)"
'alternately in xlA1 style
.Range("J2:J" & LR).Formula = _
"=VLOOKUP(J2," & TifuliWB.Worksheets(1).range("A:BS").address(1, 1, external:=true) & ",65,FALSE)"
Your original should have used the .Name or .FullName property and there were some string concatenation issues.
.Range("J2:J" & LR).FormulaR1C1 = _
"=VLOOKUP(RC[-8], '[" & TifuliWB.fullname & "]" & TifuliWB.Worksheets(1).name & "'!C1:C71,65,FALSE)"
I am trying to create a cell reference to a cell in another workbook. In my code below, I am using a variable for workbook name and sheet name.
SourceFileNamePath = Path and name of workbook I am linking to
SourceTab = Tab in the workbook I want to link to
Though the code runs fine, the formula generated is not working. Does anyone have any thoughts on whether I am referencing SourceFileNamePath and SourceTab correctly?
Code is below:
Cells(destStartRow, destStartCol).FormulaR1C1 = "='[" & SourceFileNamePath & "]" & SourceTab & "'!R" & sourceStartRow & "C" & sourceStartCol
The format to access a cell in a sheet in an external workbook is
'path\[filename]sheetname'!cell_reference
so if you have a variable called SourceFileNamePath containing the path and filename (e.g. "C:\Temp\Data\Book1.xlsx") then you need to separate the filename from the path.
You could use something like:
SourceFileNamePath = "C:\Temp\Data\Book1.xlsx" ' or however you set that variable
SourceTab = "Sheet1" ' or however you set that variable
Dim SourceFilePath As String
Dim SourceFileName As String
SourceFilePath = Left(SourceFileNamePath, InStrRev(SourceFileNamePath, Application.PathSeparator))
SourceFileName = Mid(SourceFileNamePath, InStrRev(SourceFileNamePath, Application.PathSeparator) + 1)
Cells(destStartRow, destStartCol).FormulaR1C1 = "='" & SourceFilePath & "[" & SourceFileName & "]" & SourceTab & "'!R" & sourceStartRow & "C" & sourceStartCol
Note: If either the path or the filename contains any single-quotation marks (e.g. if the filename was Sukhbir's test file.xlsx) then it will need to be escaped (i.e. each single-quotation mark needs to be replaced by two single-quotation marks). This can be achieved by using the Replace function, e.g.:
Cells(destStartRow, destStartCol).FormulaR1C1 = _
"='" & Replace(SourceFilePath, "'", "''") & _
"[" & Replace(SourceFileName, "'", "''") & "]" & _
SourceTab & "'!R" & sourceStartRow & "C" & sourceStartCol
I have written the code below so that it will retreive the file locations and put them into Path Column(B) which corresponds to the .csv column(C) where a "YES" is found.
Dim csv_ap As Range
Dim path_report2 As String
Sheets("Mail Report").Activate
Set csv_ap = Range("C65000").End(xlUp)
If csv_ap.Value = "YES" Then
path_report2 = MAIN_PATH & "1. Invoices+BUFs - " & Sheets("Sheet1").Range("D65000").End(xlUp).Value _
& "\" & Sheets("Sheet1").Range("C65000").End(xlUp).Value & " - " & Sheets("Sheet1").Range("AK65000").End(xlUp).Value _
& "\" & "LOGGED" & "\" & Sheets("Sheet1").Range("E65000").End(xlUp).Value
csv_ap.Offset(0, -1) = path_report2
End If
As you can see only the bottom row for column B has been filled. I'm not 100% sure why this is but could be down to not having a loop involved? I have tirelessly looked into adding a loop but cannot do so without affecting the current code. Any ideas?
I have edited the code above and got a loop working. But now it is duplicating the bottom row.
Dim cell As Range
Dim path_report2 As String
Sheets("Mail Report").Activate
For Each cell In Sheets("Mail Report").Range("C2:C10").Cells
If cell = "YES" Then
path_report2 = MAIN_PATH & "1. Invoices+BUFs - " & Sheets("Sheet1").Range("D65000").End(xlUp).Value & "\" & Sheets("Sheet1").Range("C65000").End(xlUp).Value & " - " & Sheets("Sheet1").Range("AK65000").End(xlUp).Value & "\" & "LOGGED" & "\" & Sheets("Sheet1").Range("E65000").End(xlUp).Value
cell.Offset(0, -1) = path_report2
End If
Next
This is the result of the macro:
Your range is defined as a single cell by Set csv_ap = Range("C65000").End(xlUp).
If you want to process all the rows from 1 to the last occupied range, then you would need:
Set csv_ap = Range("C1:C" & Range("C65000").End(xlUp).Row)
I have reports that I work on each month and have to change the headers. I have 20 worksheets in the workbook, and each worksheet needs to have different headers.
I wanted to create a macro for header, that would reference to 4 cells in each worksheet.
eg - worksheet one - Q115 vs Q414 - Header needs to reference cell a150,a151,a152,a153. The cell a152 has a formula in it. FY15 vs FY14 worksheet would refer to cells a150,a151,a152,a153 within itself, so on ... I created a macro after googling but once I run it all the worksheets have the header from the first active worksheet.
Can you please help me correct it?
Sub InsertHeaderFooter()
Dim sheetIndex, numsheets As Integer
sheetIndex = 1
numsheets = Sheets.Count
' Loop through each sheet, but don't set any of them to active
While sheetIndex <= numsheets
Dim sheetname, role, labeltext As String
sheetname = Sheets(sheetIndex).name
role = GetRole(mode)
labeltext = "Some text - " & sheetname & " - " & role
strong text
With Sheets(sheetIndex).PageSetup
.LeftHeader = ""
.CenterHeader = vbCr & Range("A150").Text & vbCr & Range("A151").Text & _
vbCr & Range("A152").Text & vbCr & Range("A153").Text
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = "SanDisk Confidential"
.RightFooter = "&[Date] - &[Time]"
End With
sheetIndex = sheetIndex + 1
Wend
End Sub
Change
.CenterHeader = vbCr & Range("A150").Text & vbCr & Range("A151").Text & _
vbCr & Range("A152").Text & vbCr & Range("A153").Text
to
.CenterHeader = vbCr & .Range("A150").Text & vbCr & .Range("A151").Text & _
vbCr & .Range("A152").Text & vbCr & .Range("A153").Text
Range makes a reference to the active sheet.
.Range makes a reference to the With Sheets(sheetIndex) sheet.