Excel VBA - trying to reference values of a range using cells

Kingwi11y

New Member
Joined
May 18, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I am trying to set the values of cells in a range in the current worksheet

  • to be the values of an equivalent range from another worksheet
  • using .range(cells... since the range size is variable
This works perfectly:

VBA Code:
Range(Cells(2, "A"), Cells(ListSize, "A")).Value = Worksheets("Staff list").Range("A2:A22").Value

This fills all cells in the range with the value of the first cell in the reference range:

VBA Code:
Range(Cells(2, "A"), Cells(ListSize, "A")).Value = Worksheets("Staff list").Cells(2, "A").Value

But these do not work:

VBA Code:
Range(Cells(2, "A"), Cells(ListSize, "A")).Value = Worksheets("Staff list").Range(Cells(2, "A"), Cells(22, "A")).Value
Range(Cells(2, "A"), Cells(ListSize, "A")).Value = Worksheets("Staff list").Range(Cells(2, "A"), Cells(ListSize, "A")).Value

I get "Run-time error 1004 - Application-defined or object defined error"

I am a little stumped and I could do with some help please Many thanks
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I referenced the worksheet within the range function for each cells and it fixed it:
VBA Code:
Range(Cells(2, "A"), Cells(ListSize, "A")).Value = Worksheets("Staff list").Range(Worksheets("Staff list").Cells(2, "A"), Worksheets("Staff list").Cells(22, "A")).Value
 
Upvote 0
Typically you use a method that guarantees the sizes on the 2 sides will match. Why are using ListSize on the left but 22 on the right ?
 
Upvote 0
I think you can simplify your code like this:
VBA Code:
With Worksheets("Staff list").Range("A2:A22")
     Range("A2").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
 
Upvote 0
Typically you use a method that guarantees the sizes on the 2 sides will match. Why are using ListSize on the left but 22 on the right ?
Hi - sorry I should have clarified that.
I took ListSize out of the right side and replaced it with 22 when troubleshooting in case my referencing it was part of the issue.
Once the referencing was working, I put ListSize back in.
 
Upvote 0
You are using ListSize as a row number, the name suggests it's a number of rows.

If its a row number you can change Akuini's suggestion to:
VBA Code:
With Worksheets("Staff list").Range("A2:A" & ListSize)
     Range("A2").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With

If it is a number of rows you can change it to:
VBA Code:
With Worksheets("Staff list").Range("A2").Resize(ListSize)
     Range("A2").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
 
Upvote 0
Solution
That's brilliant guys
Thank you so much for this
I already had the rows.count in my ListSize variable
So I simplified it down to:

VBA Code:
   With Worksheets("Staff list").Range("A2").Resize(ListSize)
        Range("A2").Resize(ListSize).Value = .Value
   End With

Lovely and neat - thanks :)
 
Upvote 0

Forum statistics

Threads
1,223,950
Messages
6,175,582
Members
452,653
Latest member
craigje92

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