Set column range based on cell value

AdamCons

New Member
Joined
May 24, 2021
Messages
2
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi All

I've searched all day, but I cannot find answer (or I'm too lame to understand). I want to copy values from column from one sheet ('New week'!B) to column 'Week'!XX. Range 'Week'!XX has to be dynamic and should read column name from cell 'New week"!N18 (values in cell DI, AZ - outcome 'Week'!DI, 'Week'!AZ etc.).

I'll appreciate yours help.

Non dynamic code
VBA Code:
Sub CopyWeek()

Dim j
j = 5

Do Until Range("'New week'!A" & j) = ""

Range("'Week'!DI" & j) = Range("'New week'!B" & j)

j = j + 1
Loop

End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I made solution code for 3 columns. Can anyone check if it can be write in better way? I'm just doing first steps.

VBA Code:
Sub CopyWeek()

Dim j
j = 5

Dim qty As Variant
qty = Range("'New week'!O13")

Dim qtybxs As Variant
qtybxs = Range("'New week'!P13")

Dim qtyplts As Variant
qtyplts = Range("'New week'!Q13")


Do Until Range("'New week'!A" & j) = ""

Worksheets("Week").Cells(j, qty).Value = Range("'New week'!B" & j)

Worksheets("Week").Cells(j, qtybxs).Value = Range("'New week'!C" & j)

Worksheets("Week").Cells(j, qtyplts).Value = Range("'New week'!D" & j)

j = j + 1
Loop

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,651
Messages
6,120,738
Members
448,988
Latest member
BB_Unlv

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