VBA to loop through list of names

kira023

New Member
Joined
Mar 13, 2019
Messages
14
Hi

I'm having an issue the macro that I'm creating. I'm trying to loop through the list of names on the CONSOLIDATED file which should start on B10 and would input data to Template file then save the file. It's working but for some reason it's skipping names and only doing 9 files and not going through the list of names. Please help me fix the issue.

VBA Code:
Sub OpenWorkbook()

Dim wsCopy As Worksheet
Dim wsDest As Worksheet

Dim FileName As String
Dim newFileName As String
Dim StName As String
FileName = "ICT-SF9-SF10-B1-Template.xlsx"


'Open a workbook
  'Open method requires full file path to be referenced.
    Workbooks.Open "C:\Users\cj\Desktop\TEST\" & FileName
  
    Set wsCopy = Workbooks("CONSOLIDATED.xlsm").Worksheets("Sheet1")
    Set wsDest = Workbooks("Template.xlsx").Worksheets("SF 9 FRONT")

  
  'Open method has additional parameters

    For i = 11 To Columns.Count
   
        If Range("A" & i).Value <> "" Then
            'Copy & Paste Data
                wsCopy.Range("C" & i).Copy
                wsDest.Range("Q10").PasteSpecial xlPasteValues
              
                StName = wsCopy.Range("C" & i).Value
        
                wsCopy.Range("E" & i).Copy
                wsDest.Range("P11").PasteSpecial xlPasteValues
              
                wsCopy.Range("B" & i).Copy
                wsDest.Range("S14").PasteSpecial xlPasteValues
                
                newFileName = "C:\Users\cj\Desktop\TEST\ICT-SF9-SF10-B1-" & StName & ".xlsx"
                ActiveWorkbook.SaveAs FileName:=newFileName
                Else
            End If
    
    Next
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Maybe
VBA Code:
Sub OpenWorkbook()

Dim wsCopy As Worksheet
Dim wsDest As Worksheet

Dim FileName As String
Dim newFileName As String
Dim StName As String
FileName = "ICT-SF9-SF10-B1-Template.xlsx"


'Open a workbook
  'Open method requires full file path to be referenced.
    Workbooks.Open "C:\Users\cj\Desktop\TEST\" & FileName
  
    Set wsCopy = Workbooks("CONSOLIDATED.xlsm").Worksheets("Sheet1")
    Set wsDest = Workbooks("Template.xlsx").Worksheets("SF 9 FRONT")

  
  'Open method has additional parameters

    For i = 10 To wsCopy.Range("B" & Rows.Count).End(xlUp).Row
   
        If wsCopy.Range("B" & i).Value <> "" Then
            'Copy & Paste Data
                wsCopy.Range("C" & i).Copy
                wsDest.Range("Q10").PasteSpecial xlPasteValues
              
                StName = wsCopy.Range("C" & i).Value
        
                wsCopy.Range("E" & i).Copy
                wsDest.Range("P11").PasteSpecial xlPasteValues
              
                wsCopy.Range("B" & i).Copy
                wsDest.Range("S14").PasteSpecial xlPasteValues
                
                newFileName = "C:\Users\cj\Desktop\TEST\ICT-SF9-SF10-B1-" & StName & ".xlsx"
                wsDest.SaveAs FileName:=newFileName
                Else
            End If
    
    Next
End Sub
 
Upvote 0
Solution
WOW That worked!Thank you so much!

I have another question. If in Column B the list of names are seperated by Male and Female how can I add a condition to put Male or Female on a specific cell on the new file created?


Maybe
VBA Code:
Sub OpenWorkbook()

Dim wsCopy As Worksheet
Dim wsDest As Worksheet

Dim FileName As String
Dim newFileName As String
Dim StName As String
FileName = "ICT-SF9-SF10-B1-Template.xlsx"


'Open a workbook
  'Open method requires full file path to be referenced.
    Workbooks.Open "C:\Users\cj\Desktop\TEST\" & FileName
 
    Set wsCopy = Workbooks("CONSOLIDATED.xlsm").Worksheets("Sheet1")
    Set wsDest = Workbooks("Template.xlsx").Worksheets("SF 9 FRONT")

 
  'Open method has additional parameters

    For i = 10 To wsCopy.Range("B" & Rows.Count).End(xlUp).Row
  
        If wsCopy.Range("B" & i).Value <> "" Then
            'Copy & Paste Data
                wsCopy.Range("C" & i).Copy
                wsDest.Range("Q10").PasteSpecial xlPasteValues
             
                StName = wsCopy.Range("C" & i).Value
       
                wsCopy.Range("E" & i).Copy
                wsDest.Range("P11").PasteSpecial xlPasteValues
             
                wsCopy.Range("B" & i).Copy
                wsDest.Range("S14").PasteSpecial xlPasteValues
               
                newFileName = "C:\Users\cj\Desktop\TEST\ICT-SF9-SF10-B1-" & StName & ".xlsx"
                wsDest.SaveAs FileName:=newFileName
                Else
            End If
   
    Next
End Sub
 
Upvote 0
You would be better off putting the Male/Female into another cell along side their name & then refer to that.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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