Advice on copying and pasting single cell value to cells on different sheet - number of which specified

LPepito

New Member
Joined
Jul 29, 2024
Messages
8
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Hi everyone,
I am a new to VBA and could really do with some help. I have a table that specifies text in one column and a number in the neighboring column. I'm trying to build a macro that will copy the text on a different sheet the same number of times specified in that neighboring column starting from the first empty cell on the new sheet. I've tried a bunch of different things, but can't seem to get it to work.

At the moment, my code is:

Sub CopyValuesAcross

Dim NextFree As String
Dim DC As Worksheet
Dim MR As Worksheet
Dim PRange As Rnage
Dim SelectionValue As Range

Set DC As Sheets("Sheet1")
Set MR As Sheets("Sheet2")

DC.Select

Set PRange = DC.Range("B5:B15")
Set SelectionValue = DC.Range("C5:C15")

For each cell in PRange

If Not IsEmpty(cell) Then
Selection.Copy

MR.Select

NextFree = Range("A2:A" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row

Range("A" & NextFree).Select

Selection.PasteSpecial xlPasteValues

End If

Next

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Welcome to the MrExcel forum!

Try:

VBA Code:
Sub CopyValuesAcross()
Dim PRange As Range, MR As Range, c As Range

    Set PRange = Worksheets("Sheet1").Range("B5:B15")
    Set MR = Worksheets("Sheet2").Range("A2")
    
    For Each c In PRange
        If c <> "" Then
            Set MR = MR.Offset(Rows.Count - MR.Row).End(xlUp).Offset(1)
            MR.Resize(c.Offset(, 1)).Value = c.Value
        End If
    Next c
    
End Sub
 
Upvote 0
Solution
Thanks so much - really appreciate your help! Just having a look at that and I am seeming to get caught on the MR.Resize line. Maybe I've copied it wrong? Sorry!
 

Attachments

  • Screenshot 2024-07-30 115705 - Copy.png
    Screenshot 2024-07-30 115705 - Copy.png
    49.8 KB · Views: 9
Upvote 0
Can you provide some visibility of your data (include the Row & Column references). Eric has had to guess at the layout and I think your columns might be the other way around. The code is copying B using the number of times from C and I am thinking it might be the other way around.
 
Upvote 0
Hi Alex - sorry about that, I should have done that in the first place. The first screenshot is the data and the second is what I'd like it to look like - hopefully that makes sense?
 

Attachments

  • Screenshot 2024-07-30 123359 - Copy.png
    Screenshot 2024-07-30 123359 - Copy.png
    5.7 KB · Views: 5
  • Screenshot 2024-07-30 123413 - Copy.png
    Screenshot 2024-07-30 123413 - Copy.png
    9.6 KB · Views: 4
Upvote 0
Oh wow - that was really silly of me, thanks for your patience - proper shots attached.
 

Attachments

  • Screenshot 2024-07-30 130013 - Copy.png
    Screenshot 2024-07-30 130013 - Copy.png
    24.2 KB · Views: 3
  • Screenshot 2024-07-30 130028 - Copy.png
    Screenshot 2024-07-30 130028 - Copy.png
    14.4 KB · Views: 3
Upvote 0
Based on that and setting up equivalent data. The code works fine for me.
Is that definitely the first error message you get ?
Do you have any even macros running ?
 
Upvote 0
Super weird, it's working perfectly now - I have no idea what happened, maybe I was running something else in the background. I'm sorry, but thank you to both you and Eric. Really helped me out, cheers!
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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