ActiveWorkbook.SaveAs with vlookup and cell.Value

TBP

Board Regular
Joined
Jul 30, 2011
Messages
55
Hi im using Split an Excel File in to Many using VBA from Chandoo.org

In the SaveAs part how would I change "& cell.Value &" to something like "& vlookup(cell.Value,MyNamedRange,2,0) &"

Code:
Dim cell As Range
Dim curPath As String
    
curPath = ActiveWorkbook.Path & "\"
    
For Each cell In Range("lstSalesman")
     [valSalesman] = cell.Value
     Range("myList").AdvancedFilter Action:=xlFilterCopy, _
         criteriarange:=Range("Criteria"), copyToRange:=Range("Extract"), unique:=False
     Range(Range("Extract"), Range("Extract").End(xlDown)).Copy
     Workbooks.Add
     ActiveSheet.Paste
     ActiveWorkbook.SaveAs Filename:=curPath & cell.Value & Format(Now, "dmmmyyyy-hhmmss") & ".xlsx", _
         FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
     ActiveWindow.Close
     Range(Range("Extract"), Range("Extract").End(xlDown)).ClearContents
Next cell
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
you can just replace cell.value with a variable say "TheFile" and assign a value like so:

TheFile=Application.Vlookup(cell.Value,MyNamedRange,2,0)
 
Upvote 0
you can just replace cell.value with a variable say "TheFile" and assign a value like so:

TheFile=Application.Vlookup(cell.Value,MyNamedRange,2,0)

Im not exactly sure how how to do what you have suggested?
I get a "Compile error: Variable not defined" with my below attempt.

Code:
Dim cell As Range
Dim curPath As String
    
curPath = ActiveWorkbook.Path & "\"

For Each cell In Range("lstSalesman")
   [valSalesman] = cell.Value
   Range("myList").AdvancedFilter Action:=xlFilterCopy, _
       criteriarange:=Range("Criteria"), copyToRange:=Range("Extract"), unique:=False
   Range(Range("Extract"), Range("Extract").End(xlDown)).Copy
   Workbooks.Add
   ActiveSheet.Paste
        
   TheFile = Application.VLookup(cell.Value, MyNamedRange, 2, 0)
        
   ActiveWorkbook.SaveAs Filename:=curPath & TheFile & Format(Now, "dmmmyyyy-hhmmss") & ".xlsx", _
       FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
   ActiveWindow.Close
   Range(Range("Extract"), Range("Extract").End(xlDown)).ClearContents
Next cell
 
Upvote 0
TheFile = Application.VLookup(cell.Value, MyNamedRange, 2, 0)

MyNamedRange is highlighted
 
Upvote 0
MyNamedRange is soemthig u have to define which is nothing but the range you wish to split
Hi jumbledore im not sure what you mean and how to get working?
Here is a link to my example file on OneDrive

Code:
Dim cell As Range
Dim curPath As String
    
'I added this
Dim MyNamedRange As Range
    
curPath = ActiveWorkbook.Path & "\"

    
For Each cell In Range("lstSalesman")
    [valSalesman] = cell.Value
    Range("myList").AdvancedFilter Action:=xlFilterCopy, _
        criteriarange:=Range("Criteria"), copyToRange:=Range("Extract"), unique:=False
    Range(Range("Extract"), Range("Extract").End(xlDown)).Copy
    Workbooks.Add
    ActiveSheet.Paste
        
    'I added this
    TheFile = Application.VLookup(cell.Value, MyNamedRange, 2, 0)
        
    'I changed cell.Value > TheFile
    ActiveWorkbook.SaveAs Filename:=curPath & TheFile & Format(Now, "dmmmyyyy-hhmmss") & ".xlsx", _
        FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWindow.Close
    Range(Range("Extract"), Range("Extract").End(xlDown)).ClearContents
Next cell
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top