VBA If range [J:K] not empty, then copy [H:I] to the end of [J:K]

Xiggie

New Member
Joined
Feb 23, 2022
Messages
12
Office Version
  1. 2013
Platform
  1. Windows
Hello!

I have two ranges, [H23:I32] and [J23:K50].

I need to copy values from [H23:I32] to [J23:K50] if [J23:K50] is empty, and if [J23:K50] is not empty I need to find the last row and add [H23:I32] below.
It's a growing list of values.

The "copy if empty" works, but the "add to the end of the list" doesn't unfortunately. It does something, but clearly not the thing I need.

Instead of copying H23-I23 to J:K row by row my code starts wiping data J23 > K23 > J24 > K24 one cell at a time instead of copying [H:I] to the end of [J:K]

VBA Code:
Sub Total_Loop()

Application.ScreenUpdating = False

Dim c  As Range

For Each c In Range("J23:K50" & Cells(Rows.Count, "J").End(xlUp).Row)
If c.Value <> "" Then
Range("J23:K50" & Cells(Rows.Count, "J").End(xlUp).Row + 1) = Range("H23:I32")
Else: c.Value = c.Offset(, -2).Value
    
End If
Next
    
Application.ScreenUpdating = True

End Sub

Any suggestions how to fix this?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Maybe something like this............. Change the worksheet = your worksheet name
VBA Code:
Sub MoveData()
Dim lrow As Long
Dim ws As Worksheet
Set ws = Sheets(YourName)  'change here

If WorksheetFunction.CountA(ws.Range("J23:K50")) = 0 Then
     ws.Range("H23:I32").Copy ws.Range("J23")
Else
   lrow = ws.Range("J23:K50").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
   ws.Range("H23:I32").Copy ws.Range("J" & lrow + 1)
End If
End Sub
 
Upvote 0
Thank you! Is there a way to do it without defining the worksheet?
Unfortunately it breaks the rest of my macros
 
Upvote 0
Thank you! Is there a way to do it without defining the worksheet?
Unfortunately it breaks the rest of my macros
You implied this is a standalon macro you required. If so defining the sheet has no bearing on your other macros.
You should always fully define ranges but if you want to do it without defining then try this but be aware it may not ive you the results you expect.

VBA Code:
Sub MoveData()
Dim lrow As Long

If WorksheetFunction.CountA(Range("J23:K50")) = 0 Then
     Range("H23:I32").Copy Range("J23")
Else
   lrow = Range("J23:K50").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
   Range("H23:I32").Copy Range("J" & lrow + 1)
End If
End Sub
 
Upvote 0
Thank you! Is there a way to do it without defining the worksheet?
Unfortunately it breaks the rest of my macros
Nevermind! It was all me, I copied it wrong! But I am getting weird results:

It is kind of doing what I want it to do but it copies [F:G] instead of [H:I] to [J:K].
But at least it's alive!
 
Upvote 0
Nevermind! It was all me, I copied it wrong! But I am getting weird results:

It is kind of doing what I want it to do but it copies [F:G] instead of [H:I] to [J:K].
But at least it's alive!
it would be impossible for that code to copy F:G. Have you named any ranges etc. If this is part of a bigger code then maybe paste the entire code here.
 
Upvote 0
Unfortunately it doesn't fit, so I uploaded my excel to file.io https://www.file.io/zXX8/download/ovpChH1BJIsV

However, I think I understand the nature of my issue: it copies formulas, not values. I think I know a workaround for it!
if you want it to copy values then use this code. As you did not specify you wanted values in your post the code wasnt written as such
VBA Code:
Sub MoveData()
Dim lrow As Long
Dim ws As Worksheet
Set ws = Sheets(YourName)  'change here


If WorksheetFunction.CountA(ws.Range("J23:K50")) = 0 Then
     ws.Range("H23:I32").Copy
     ws.Range("J23").PasteSpecial xlPasteValues
Else
     lrow = ws.Range("J23:K50").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
     ws.Range("H23:I32").Copy
     ws.Range("J" & lrow + 1).PasteSpecial xlPasteValues
End If
End Sub
 
Upvote 0
Solution
And now it works!! Splendid and thank you so very much!!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
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