IanWallbridge
New Member
- Joined
- Jan 29, 2014
- Messages
- 10
Hi all,
I've written some VBA which manipulates data in two existing workbooks, and am trying to add some extra functionality - but am struggling to get this to work using variables to replace hard coded filenames.
Within the code I have the following:
The variable 'extwbk' which appears in the first two paragraphs is interchangeable with the actual workbook name "odc_full_user_list___macro_backup_report.xlsx" - and as the code stands, everything works as expected whether I use the filename or the variable.
If however I make exactly the same substitution into either the Application.Vlookup or the If statements - so that for example the If statement begins
then the code doesn't work as expected, although I don't get any error messages as such. I'm slightly confused by this, as (in the If statement at least) I'm using exactly the same convention as in the second paragraph.
Can anyone advise? Note that I'm working with a xlsx file here, and I believe that these require more precise VBA syntax than older Excel files.
If there's any other info that's required, please let me know.
Many thanks,
Ian
I've written some VBA which manipulates data in two existing workbooks, and am trying to add some extra functionality - but am struggling to get this to work using variables to replace hard coded filenames.
Within the code I have the following:
Code:
With extwbk.Sheets("Sheet1")
myLSFinalRow = .Cells(.Rows.Count, 2).End(xlUp).Row
End With
Workbooks(extwbk).Worksheets("Sheet1").Range("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Selection.NumberFormat = "000000"
'Populate LS Leaver
For RowCount = 2 To myLSFinalRow
y = Application.VLookup(Workbooks("odc_full_user_list___macro_backup_report.xlsx").Worksheets("Sheet1").Range("A" & RowCount), _
Workbooks("Daily SIP report.xls").Worksheets(mySIPWorksheet).Range("$A$2:$O$7000"), 4, False)
If (Workbooks("odc_full_user_list___macro_backup_report.xlsx").Worksheets("Sheet1").Range("O" & RowCount) = "Staff" _
Or Workbooks("odc_full_user_list___macro_backup_report.xlsx").Worksheets("Sheet1").Range("O" & RowCount) = "Contractor") _
And Workbooks("odc_full_user_list___macro_backup_report.xlsx").Worksheets("Sheet1").Range("T" & RowCount) = "Active" _
And IsError(y) Then
Sheets("LS Leaver").Range("user_id")(RowCount - 1) = Workbooks("odc_full_user_list___macro_backup_report.xlsx").Worksheets("Sheet1").Range("A" & RowCount)
Sheets("LS Leaver").Range("name")(RowCount - 1) = Workbooks("odc_full_user_list___macro_backup_report.xlsx").Worksheets("Sheet1").Range("C" & RowCount)
Sheets("LS Leaver").Range("title")(RowCount - 1) = Workbooks("odc_full_user_list___macro_backup_report.xlsx").Worksheets("Sheet1").Range("D" & RowCount)
End If
Next RowCount
The variable 'extwbk' which appears in the first two paragraphs is interchangeable with the actual workbook name "odc_full_user_list___macro_backup_report.xlsx" - and as the code stands, everything works as expected whether I use the filename or the variable.
If however I make exactly the same substitution into either the Application.Vlookup or the If statements - so that for example the If statement begins
Code:
If (Workbooks(extwbk).Worksheets("Sheet1").Range("O" & RowCount) = "Staff"
Can anyone advise? Note that I'm working with a xlsx file here, and I believe that these require more precise VBA syntax than older Excel files.
If there's any other info that's required, please let me know.
Many thanks,
Ian