Move or offset one space if certain criteria is met.

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
774
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello, I have a large database and one of the columns represents a Country or a State within the US. I'm using the three letter code for the country and the two letter code for the state.
After using a pivot table I intend on using a map. Since I cannot have both countries and states in one column, I need to have two MAPS. One for the US and one for everything else.
All my location codes (countries/state) go in Column D within the Database.

I need a macro will it will go through all of Column D that has data
Excel Formula:
Range(Range("D2"), Range("D2").End(xlDown))
. then I need the code that will specifically look for date within only two letters in column D. If there is I need letters move to Column E using the offset code.

Thank you,
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Do you mean like this?

VBA Code:
With Range(Range("D2"), Range("D2").End(xlDown))
    .Offset(, 1).Value = Evaluate("IF(LEN(" & .Address & ")=2," & .Address & ","""")")
    .Value = Evaluate("IF(LEN(" & .Address & ")=3," & .Address & ","""")")
End With

ABCDE
1BEFORE
2AZ
3FL
4AUS
5GBR
6SUI
7CAN
8UT
9CA
10AUS
11AUS
12
Sheet1

ABCDE
1AFTER
2AZ
3FL
4AUS
5GBR
6SUI
7CAN
8UT
9CA
10AUS
11AUS
Sheet1
 
Upvote 0
The example you provided was perfect. Obviously I think I'm missing something. I used exactly what you had, and it clears out everything. I think I need a cut command in there some place.

This is what I used and it clears everything out in Column D.

Thank you so much for your input thus far.

VBA Code:
Sub Location()
With Range(Range("D2"), Range("D2").End(xlDown))
    .Offset(, 1).Value = Evaluate("IF(LEN(" & .Address & ")=2," & .Address & ","""")")
    .Value = Evaluate("IF(LEN(" & .Address & ")=3," & .Address & ","""")")
End With
       
End Sub
 
Upvote 0
This is what I used and it clears everything out in Column D.
The code relies on the data length in column D being either two or three, as you've stated.

I suggest you check the LEN(). Do you perhaps have leading or trailing space or non-printing characters?
 
Upvote 0
There is no leading spaces that I am aware of. I however, have a vlookup code in Column D.
Excel Formula:
=IFERROR(VLOOKUP($D2,'State Country Code'!$A$2:$B$10388,2,0),"")

I need that just to make sure I have the right 3 letter codes (ISO) for the countries and two letter for the state. That is where I want your macro to takeover.
Thank you,
 
Upvote 0
If you have ruled out LEN() as the problem, my next question would be whether you have other code running?

Based on the file you uploaded in this thread: Trying to use Index with Match but I get error code 2042 it appears that your Sub Worksheet_Change may be the culprit.

Try wrapping my code:

VBA Code:
Application.EnableEvents = False
Sub Location()
With Range(Range("D2"), Range("D2").End(xlDown))
    .Offset(, 1).Value = Evaluate("IF(LEN(" & .Address & ")=2," & .Address & ","""")")
    .Value = Evaluate("IF(LEN(" & .Address & ")=3," & .Address & ","""")")
End With
Application.EnableEvents = True
 
Upvote 0
I added the change you suggested, and ended up with a a compile error.

I provided a truncated version of the Excel file. I hope it may help you to see the full picture.



Thank you,
 
Upvote 0
VBA Code:
'You have
Application.EnableEvents = False
Sub Location()
    
    With Range(Range("D2"), Range("D2").End(xlDown))
        .Offset(, 1).Value = Evaluate("IF(LEN(" & .Address & ")=2," & .Address & ","""")")
        .Value = Evaluate("IF(LEN(" & .Address & ")=3," & .Address & ","""")")
    End With
    Application.EnableEvents = True

End Sub

'You need
Sub Location()

    Application.EnableEvents = False
    With Range(Range("D2"), Range("D2").End(xlDown))
        .Offset(, 1).Value = Evaluate("IF(LEN(" & .Address & ")=2," & .Address & ","""")")
        .Value = Evaluate("IF(LEN(" & .Address & ")=3," & .Address & ","""")")
    End With
    Application.EnableEvents = True

End Sub

But given your data is in column E, not D, just change to:

Rich (BB code):
Sub Location()

    Application.EnableEvents = False
    With Range(Range("E2"), Range("E2").End(xlDown))
        .Offset(, 1).Value = Evaluate("IF(LEN(" & .Address & ")=2," & .Address & ","""")")
        .Value = Evaluate("IF(LEN(" & .Address & ")=3," & .Address & ","""")")
    End With
    Application.EnableEvents = True

End Sub

(Or you could use a VLOOKUP formula in columns E and F, returning a value to column E if the length of the lookup was 3, or column F if length 2)
 
Upvote 1
Solution
You're right, I referred to the incorrect column. I think my issues would have been good if I got it right in the first place. You were spot on .

Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

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