Paste range of values in the first 5 consecutive blank cells

theonealex

New Member
Joined
Nov 16, 2017
Messages
1
I am having trouble figuring out how to locate the first 5 consecutive blank cells in a specific row and then paste a range in those 5 blank cells.

Some details about the data I'm working with (Example data below):
  • User selects a product name from a drop down list (Sheet1, cell B1)
  • User enters 5 pieces of information about that product (Sheet1, cells D2:H2)
  • User clicks a button to submit the 5 pieces of information

Ideally the button copies the Sheet1 D2:H2 range, uses the selected product name to look up the corresponding row in Sheet2 for that product, then locates the first 5 blank cells in Sheet2 after column F to paste the range in (so previous information submitted for a product does not get overwritten).


Code:
Private Sub SubmitComment_Click()

Dim myValue As Range 'value selected from a drop down list
Dim findRow As Range
Dim targetRow As Long
Dim myComment As Range

Set myComment = Sheets("Sheet1").Range("D2:H2")
Set myValue = Sheets("Sheet1").Range("B1") 'location of drop down list
Set findRow = Sheets("Sheet2").Range("A:A").Find(What:=myValue, LookIn:=xlValues) 'searches column B in Sheet2 for drop down list value
targetRow = findRow.Row 'integer value of row matching the selected product

Sheets("Sheet2").Select

'// code to locate first 5 consecutive blank cells after column F
'// code to select the first cell in this range of 5 consecutive blank cells
'// code to paste myComment

End Sub

Any insight and help would be greatly appreciated!


Example workbook:

Sheet 1
ABCDEFGH
1Product:(drop down list of products)Info 1Info 2Info 3Info 4Info 5
2[user entered data]<user entered="" data=""></user>[user entered data]<user entered="" data=""></user>[user entered data]<user entered="" data=""></user>[user entered data]<user entered="" data=""></user>[user entered data]<user entered="" data=""></user>

<tbody>
</tbody>
[[["Submit Comment" button]]]

Sheet 2
ABCDEFGHIJKLMNOPQRSTU
1Comment 1Comment 1Comment 1Comment 1Comment 1Comment 2Comment 2Comment 2Comment 2Comment 2Comment 3Comment 3Comment 3Comment 3Comment 3
2Product ListDataDataDataDataDataInfo 1Info 2Info 3Info 4Info 5Info 1Info 2Info 3Info 4Info 5Info 1Info 2Info 3Info 4Info 5
3Product 1aaaaaaaaaaaaaaa
4Product 2
5Product 3aaaaaaaaa
6Product 4
7Product 5aaaaaaaaaaaaaaaaaaaaaaaa
8Product 6

<tbody>
</tbody>
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Re: How to paste range of values in the first 5 consecutive blank cells?

theonealex,

Welcome to the Board.

You might consider the following...

Code:
Private Sub SubmitComment_Click()
Dim myValue As Range 'value selected from a drop down list
Dim findRow As Range
Dim targetRow As Long
Dim myComment As Range
Dim LastColumn As Long
Dim ws As Worksheet

Set ws = Sheets("Sheet2")
Set myComment = Sheets("Sheet1").Range("D2:H2")
Set myValue = Sheets("Sheet1").Range("B1") 'location of drop down list
Set findRow = Sheets("Sheet2").Range("A:A").Find(What:=myValue, LookIn:=xlValues) 'searches column B in Sheet2 for drop down list value
targetRow = findRow.Row 'integer value of row matching the selected product
LastColumn = ws.Cells(targetRow, Columns.Count).End(xlToLeft).Column

If LastColumn > 6 Then
    ws.Range(ws.Cells(targetRow, LastColumn + 1), ws.Cells(targetRow, LastColumn + 6)).Value = myComment.Value
Else
    ws.Range(ws.Cells(targetRow, 7), ws.Cells(targetRow, 11)).Value = myComment.Value
End If
Sheets("Sheet2").Select
End Sub

Please note the code is untested, and assumes your original code ran error-free.

Cheers,

tonyyy
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,037
Members
449,062
Latest member
mike575

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