Robdiqulous
New Member
- Joined
- Sep 11, 2017
- Messages
- 15
I am trying to get this code to work but having an issue. I have this code that works perfectly in one of my macros. When I run the whole thing it goes through and things end up how I want them. If I just run this sub by itself and not have it part of the main macro, it gets an error on the line with the first VLOOKUP. It is a 1004 error, saying object-defined or application-defined error. Why will this run when part of the rest of the code but not when it is ran by itself? It does not reference anything in the other part of the code?
Anyone have an idea? I feel like it has to do with something like what actual value of X is.
So this is the code that runs perfectly when ran as part of the whole macro. I don't think that the variable shtName is actually working, but it works anyway because there is only one sheet in that workbook. I know i can make it " & shtName & " but that still doesn't make it work when ran by itself.
I appreciate any help! Thank you!
Anyone have an idea? I feel like it has to do with something like what actual value of X is.
Code:
Private Sub Create_VLOOKUP_Using_Old_Kronos_Full_File()'
' Create_VLOOKUP_Using_Old_Kronos_Full_File Macro
'
'
Dim iRet As Integer
Dim strPrompt As String
Dim strTitle As String
' Promt
strPrompt = "Please select the last Kronos Full File before the dates of this HCM Report." & vbCrLf & _
"This will be used to find the Old Position, Org Unit, and Old Cost Center." & vbCrLf & _
"For example, if the date of this report is 7-28-17 thru 8-25-17, the closest Kronos Full File you would want to use is 7-27-17."
' Dialog's Title
strTitle = "Last Kronos Full File for Old Positions"
'Display MessageBox
iRet = MsgBox(strPrompt, vbOK, strTitle)
Dim LR As Long
Dim X As String
Dim lNewBracketLocation As Long
X = Application.GetOpenFilename( _
FileFilter:="Excel Files (*.xls*),*.xls*", _
Title:="Choose the Kronos Full File.", MultiSelect:=False)
Dim wbk As Workbook
Set wbk = Workbooks.Open(Filename:=X, ReadOnly:=True)
Dim shtName As String
shtName = wbk.Worksheets(1).name
wbk.Close
MsgBox "You selected " & X
'Find the last instance in the string of the path separator "\"
lNewBracketLocation = InStrRev(X, Application.PathSeparator)
'Edit the string to suit the VLOOKUP formula - insert "["
X = Left$(X, lNewBracketLocation) & "[" & Right$(X, Len(X) - lNewBracketLocation)
LR = Range("E" & Rows.Count).End(xlUp).Row
Range("T2").Formula = "=VLOOKUP($E2,'" & X & "]shtName'!$B$1:$AP$99999,15,0)"
Range("T2").AutoFill Destination:=Range("T2:T" & Range("E" & Rows.Count).End(xlUp).Row)
Range("T2:T" & Range("E" & Rows.Count).End(xlUp).Row).Select
Range("U2").Formula = "=VLOOKUP($E2,'" & X & "]shtName'!$B$1:$AP$99999,41,0)"
Range("U2").AutoFill Destination:=Range("U2:U" & Range("E" & Rows.Count).End(xlUp).Row)
Range("U2:U" & Range("E" & Rows.Count).End(xlUp).Row).Select
Range("V2").Formula = "=VLOOKUP($E2,'" & X & "]shtName'!$B$1:$AP$99999,18,0)"
Range("V2").AutoFill Destination:=Range("V2:V" & Range("E" & Rows.Count).End(xlUp).Row)
Range("V2:V" & Range("E" & Rows.Count).End(xlUp).Row).Select
Cells.Select
Cells.EntireColumn.AutoFit
So this is the code that runs perfectly when ran as part of the whole macro. I don't think that the variable shtName is actually working, but it works anyway because there is only one sheet in that workbook. I know i can make it " & shtName & " but that still doesn't make it work when ran by itself.
I appreciate any help! Thank you!