Macro runs correctly on every second run

cmf30

New Member
Joined
Apr 27, 2020
Messages
8
Office Version
  1. 365
  2. 2016
Platform
  1. MacOS
Hi,

I am a complete beginner so I assume this is something obvious but I haven't been able to figure it out.

Essentially, the below code runs correctly every second time I run it. What the code does is

1) Copies a number of columns from one sheet to another
2) Formats the TitleCode Column to text in both sheets
3) Uses a Vlookup with Worksheet wsTitre to get a title that corresponds to a title code.

It is the third step that causes the issue. The title only gets populated on every second run, would it be something to do with the loop? This is the loop code.

VBA Code:
For i = 2 To intLoopNumber

On Error Resume Next

    strTitleCode = trg.Range("E" & i).Value
    strFullTitle = Application.WorksheetFunction.VLookup(strTitleCode, wsTitre.Range("$A$2:$B$30"), 2, False)
    trg.Range("F" & i).Value = strFullTitle

Next i

On Error GoTo 0

The full code is copied below in case it helps.


VBA Code:
Sub CreateRegister()

Dim src As Worksheet
Dim trg As Worksheet
Dim wsTitre As Worksheet
Dim strFullTitle As String
Dim strTitleCode As String
Dim intLoopNumber As Integer


'Set relevant worksheet variables
Set src = ThisWorkbook.Worksheets("Faculty")
Set trg = ThisWorkbook.Worksheets("ExpertRegister")
Set wsTitre = ThisWorkbook.Worksheets("TITREFAC")

'Copy relevant columns over to ExpertRegister Sheet from Faculty Sheet
src.Range("P1:P250").Copy Destination:=trg.Range("E1")
trg.Range("E1").Value = "Title Code"
trg.Range("F1").Value = "Full Title" 'Blank column for now.

src.Range("X1:X250").Copy Destination:=trg.Range("G1")
trg.Range("G1").Value = "Active"


'Format the Title Code to text in both ExpertRegister and TITREFAC sheets
   wsTitre.Select
   wsTitre.Range("A2:A30").Select
   Selection.NumberFormat = "@"

   trg.Select
   trg.Range("E1:E250").Select
   Selection.NumberFormat = "@"
    
'Get rows to iterate though
intLoopNumber = Cells(trg.Rows.Count, 3).End(xlUp).Row

For i = 2 To intLoopNumber

On Error Resume Next

    strTitleCode = trg.Range("E" & i).Value
    strFullTitle = Application.WorksheetFunction.VLookup(strTitleCode, wsTitre.Range("$A$2:$B$30"), 2, False)
    trg.Range("F" & i).Value = strFullTitle

Next i

On Error GoTo 0
    
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Without seeing the sheet to confirm any potential issues, the only reasons that I can see for any problems would be,

strTitleCode not found in wsTitre.Range(A2:A30)

Merged cells in column E
 
Upvote 0
Thank you for this, I will check for these things.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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