Creating a macro to update cells

kevin67

Board Regular
Joined
Feb 22, 2020
Messages
55
Office Version
  1. 2016
Platform
  1. Windows
Hello Excel expert or VB expert
I have a sheet one about 22000 records as per below. In column 1 Header is LVL and Column 2 header GROUPNAME. if condition is met that is if A2 = 0 then B2 will be copied to D2 if A2 =1 then B2 will be copied to E2 , if A2=3 then B2 will be copied to F2. the values in the column LVL can be between 0 to 10. I am looking like an array to do this task . Any help will be grateful

Thanks

LVLGROUPNAME
0AB
1BC
0DD
1AX
2AZ
3NO

Here is the code i am using but getting error Runtime error 1004 Method Range of Object worksheet failed

Sub Replace_responseRegion()


Dim lr As Long
Dim x As Integer
Dim ary As Variant, i As Long
ary = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

'Declare the workbook to retrieve info'
Dim wkRead As Workbook
Set wkRead = Workbooks("Masterfile.xlsx")

'Declare the worksheet from the workbook maysurvey.xlsx'
Dim shtRead As Worksheet
Set shtRead = wkRead.Worksheets("Master")

lr = shtRead.Range("A" & Rows.Count).End(xlUp).row
Debug.Print lr


With shtRead
For i = LBound(ary) To UBound(ary)


shtRead.Range("A2:B" & lr).AutoFilter 1, ary(i)

If i = 0 Then

shtRead.Range("B2:B" & lr).Copy shtRead.Range("D2" & Rows.Count).End(3)(1)
ElseIf i = 1 Then
shtRead.Range("D3:D" & lr).Copy shtRead.Range("B2" & Rows.Count).End(3)(1)
ElseIf i = 2 Then
shtRead.Range("D4:D" & lr).Copy shtRead.Range("B2" & Rows.Count).End(3)(1)
End If

Next
End With


End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
if A2 = 0 then B2 will be copied to D2 if A2 =1 then B2 will be copied to E2 , if A2=3 then B2 will be copied to F2.
Did you mean if A2=2?

If so, try this with a copy of your data. It is written for the active sheet as I don't have your workbook set-up. Hopefully you can adapt if it is what you want.

VBA Code:
Sub AllocateToColumn()
  Dim a As Variant, b As Variant
  Dim i As Long
  
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value2
  ReDim b(1 To UBound(a), 1 To 11)
  For i = 1 To UBound(a)
    b(i, a(i, 1) + 1) = a(i, 2)
  Next i
  Range("D2").Resize(UBound(b), UBound(b, 2)).Value = b
End Sub
 
Upvote 0
Solution
Did you mean if A2=2?

If so, try this with a copy of your data. It is written for the active sheet as I don't have your workbook set-up. Hopefully you can adapt if it is what you want.

VBA Code:
Sub AllocateToColumn()
  Dim a As Variant, b As Variant
  Dim i As Long
 
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value2
  ReDim b(1 To UBound(a), 1 To 11)
  For i = 1 To UBound(a)
    b(i, a(i, 1) + 1) = a(i, 2)
  Next i
  Range("D2").Resize(UBound(b), UBound(b, 2)).Value = b
End Sub
Thanks a lot Peter, it works as per requested
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,585
Members
448,972
Latest member
Shantanu2024

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