For loop doesn't complete on the first run

Varidian

New Member
Joined
Jan 27, 2021
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hi all I have a bit of code that is basically looks to the cell 2 its right, finds the @ and returns the domain name. however, sometimes when I first run the full macro, it only completes about 30 rows even though there are 900 + rows in it.

at first, I thought it was a time issue so I put a wait in for 5 seconds. It's really bugging me

VBA Code:
  For x = 2 To Lr
        SplitPos = InStr(1, Cells(x, 1), "@", vbTextCompare)
        Ws_Clean.Cells(x, 2).value = Mid(Cells(x, 1), SplitPos + 1)
  Next x
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
The problem will be the value of lr, not the loop.
 
Upvote 0
We would need to see the code were lr is determined, since when it should be 200+ but ends at 30, there should be the problem.
 
Upvote 0
I thought that, but it works if I restart the module, and when I inspect Lr is the correct value.
 
Upvote 0
How are we to help, if you don't show the code?

As one guess: you have 2 worksheets, but in your code you fail to point to the needed worksheet. So depending on which worksheet is active when activating the macro it might work correctly or not.

VBA Code:
  For x = 2 To Lr
        SplitPos = InStr(1, WHICHWORKSHEET.Cells(x, 1), "@", vbTextCompare)
        Ws_Clean.Cells(x, 2).value = Mid(WHICHWORKSHEET.Cells(x, 1), SplitPos + 1)
  Next x
 
Upvote 0
Another possibility is
VBA Code:
  For x = 2 To Lr
        SplitPos = InStr(1, Ws_Clean.Cells(x, 1), "@", vbTextCompare)
        Ws_Clean.Cells(x, 2).value = Mid(Ws_Clean.Cells(x, 1), SplitPos + 1)
  Next x
assuming both cells are on the same sheet.
 
Upvote 0
Another possibility is
VBA Code:
  For x = 2 To Lr
        SplitPos = InStr(1, Ws_Clean.Cells(x, 1), "@", vbTextCompare)
        Ws_Clean.Cells(x, 2).value = Mid(Ws_Clean.Cells(x, 1), SplitPos + 1)
  Next x
assuming both cells are on the same sheet.

Hmmm... am I wrong or does the code not what was stated in the first post: "...code that is basically looks to the cell 2 its right, finds the @ and returns the domain name..."
This code looks up in the left cell (column 1) and returns to the right cell (column 2). Or am I missing something?
 
Upvote 0
Depends on where the OP is from, some countries have the sheets & cells running right to left.
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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