use next available cell in row (xltoright)

BRB1983

Board Regular
Joined
Aug 29, 2019
Messages
61
i need help getting this code to paste in next empty cell to right.
the code only paste in column B. i need-if column B is not blank then use next cell to right.
Code:
Dim ThisCell1 As Range
Dim ThisCell2 As Range
Application.ScreenUpdating = False
    For Each ThisCell1 In Sheets("sheet1").Range("A1:A1089")
        For Each ThisCell2 In Sheets("sheet0").Range("b2:b3392")
            If ThisCell1.Value = ThisCell2.Value Then
                ThisCell2.Offset(0, 1).Copy
                ThisCell1.Offset(0, 1).PasteSpecial Paste:=xlPasteValues
                Exit For
             End If
        Next ThisCell2
    Next ThisCell1
Application.ScreenUpdating = True
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try this

Code:
Sub next_available_cell()
  Dim Cell1 As Range, f As Range
  Application.ScreenUpdating = False
  For Each Cell1 In Sheets("sheet1").Range("A1", Sheets("sheet1").Range("A" & Rows.Count).End(xlUp))
    Set f = Sheets("sheet0").Range("b2:b3392").Find(Cell1, , xlValues, xlWhole)
    If Not f Is Nothing Then Cell1.Cells(1, Cell1.Cells(1, Columns.Count).End(xlToLeft).Column + 1).Value = f.Offset(, 1).Value
  Next
End Sub
 
Upvote 0
I did this, it works, but it is slow.
Code:
Dim ThisCell1 As RangeDim ThisCell2 As Range
Dim LCol As Long
    LCol = Cells(1, Columns.Count).End(xlToLeft).Column
Application.ScreenUpdating = False
    For Each ThisCell1 In Sheets("sheet1").Range("A1:A1089")
        For Each ThisCell2 In Sheets("sheet0").Range("b2:b3392")
            If ThisCell1.Value = ThisCell2.Value Then
                ThisCell2.Offset(0, 1).Copy
                If Not ThisCell1.Offset(0, 1) = "" Then
                ThisCell1(LCol, Columns.Count).End(xlToLeft).Offset(0, 1).PasteSpecial
                End If
                ThisCell1.Offset(0, 1).PasteSpecial Paste:=xlPasteValues
             End If
        Next ThisCell2
    Next ThisCell1
Application.ScreenUpdating = True
 
Upvote 0
I do not understand well.
Do you want the next available cell of the same row or the next available cell according to the row 1?

Try this

Code:
Sub next_available_cell()
  Dim Cell1 As Range, f As Range, lc As Long
  Application.ScreenUpdating = False
  lc = Sheets("sheet1").Cells(1, Columns.Count).End(xlToLeft).Column + 1
  For Each Cell1 In Sheets("sheet1").Range("A1", Sheets("sheet1").Range("A" & Rows.Count).End(xlUp))
    Set f = Sheets("sheet0").Range("b2:b3392").Find(Cell1, , xlValues, xlWhole)
    If Not f Is Nothing Then Cell1.Cells(1, lc).Value = f.Offset(, 1).Value
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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