Macro help for import sheet

SaraQuinn

New Member
Joined
May 19, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello, I am trying to achieve the following result in the attached sheet. Please advise if anyone can help me with a Macro to convert these values automatically.
  • If customer code in column "D" = "UPDS" convert code in column "N" from "1100" to "1101" any other customer code remains "1100".
  • If customer code in column "D" = "UPDS" convert codes in column "B" from "4050" to "4049" any other customer code remains "4050".
  • If cell in column "D" is blank, cell in column "N" remains blank.

1698252442632.png
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi,

This should work for you:
VBA Code:
Sub test()
  Dim myRange As Variant, isUPDS As Boolean, i As Long
  myRange = UsedRange
  For i = 1 To UBound(myRange, 1)
    If myRange(i, 4) = "UPDS" Then
      isUPDS = True
      myRange(i, 14) = 1101
    ElseIf isUPDS And myRange(i, 4) = "" Then
      myRange(i, 3) = 4049
    Else
      isUPDS = False
    End If
  Next
  Range("A1").Resize(UBound(myRange, 1), UBound(myRange, 2)).Value = myRange
End Sub
 
Upvote 0
Hi,

This should work for you:
VBA Code:
Sub test()
  Dim myRange As Variant, isUPDS As Boolean, i As Long
  myRange = UsedRange
  For i = 1 To UBound(myRange, 1)
    If myRange(i, 4) = "UPDS" Then
      isUPDS = True
      myRange(i, 14) = 1101
    ElseIf isUPDS And myRange(i, 4) = "" Then
      myRange(i, 3) = 4049
    Else
      isUPDS = False
    End If
  Next
  Range("A1").Resize(UBound(myRange, 1), UBound(myRange, 2)).Value = myRange
End Sub
Thank you but it didn't work : ( I got this error
1698328006822.png
 
Upvote 0
Please try to specify a worksheet name:
VBA Code:
Sub test()
  Dim myRange As Variant, isUPDS As Boolean, i As Long
  With Worksheets("yourWorksheet")
  myRange = .UsedRange
  For i = 1 To UBound(myRange, 1)
    If myRange(i, 4) = "UPDS" Then
      isUPDS = True
      myRange(i, 14) = 1101
    ElseIf isUPDS And myRange(i, 4) = "" Then
      myRange(i, 3) = 4049
    ElseIf myRange(i, 4) = "" Then
      myRange(i, 14) = ""
    Else
      isUPDS = False
    End If
  Next
  .Range("A1").Resize(UBound(myRange, 1), UBound(myRange, 2)).Value = myRange
  End With
End Sub
 
Upvote 1
Solution
Please try to specify a worksheet name:
VBA Code:
Sub test()
  Dim myRange As Variant, isUPDS As Boolean, i As Long
  With Worksheets("yourWorksheet")
  myRange = .UsedRange
  For i = 1 To UBound(myRange, 1)
    If myRange(i, 4) = "UPDS" Then
      isUPDS = True
      myRange(i, 14) = 1101
    ElseIf isUPDS And myRange(i, 4) = "" Then
      myRange(i, 3) = 4049
    ElseIf myRange(i, 4) = "" Then
      myRange(i, 14) = ""
    Else
      isUPDS = False
    End If
  Next
  .Range("A1").Resize(UBound(myRange, 1), UBound(myRange, 2)).Value = myRange
  End With
End Sub
It works perfect now. Thank you very much! I really appreciate your help
 
Upvote 0
Please correct this part:
VBA Code:
    ElseIf isUPDS And myRange(i, 4) = "" Then
      myRange(i, 3) = 4049
    ElseIf myRange(i, 4) = "" Then
      myRange(i, 14) = ""
    Else
Like this:
Excel Formula:
    ElseIf isUPDS And myRange(i, 4) = "" Then
      myRange(i, 3) = 4049
      myRange(i, 14) = ""
    ElseIf myRange(i, 4) = "" Then
      myRange(i, 14) = ""
    Else
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,664
Members
449,114
Latest member
aides

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