VBA code for blank columns

SebastianHuang

New Member
Joined
Dec 5, 2019
Messages
38
Office Version
  1. 2013
Platform
  1. Windows
What is the vba code for
if column A and B are blank, then copy to cell A1
If column A and B are not blank.

Two separate codes.

Thank you a million!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I don't think your question make sense.
I think we need more details.
If column A is blank, what exactly are you copying to cell A1?

Sometimes, showing a simple example of what you are trying to do goes a long way in explaining it.
 
Upvote 0
Why 2 codes, just use an If statement...

VBA Code:
Sub testit()
    Range("D1").Value = "XXXX"
    Range("D1").Copy
    If WorksheetFunction.CountA(Range("A:B")) = 0 Then
        Range("A1").PasteSpecial ' or just .Paste if same sheet as this is
    Else
        Range("C1").PasteSpecial ' or just .Paste if same sheet as this is
    End If
    Application.CutCopyMode = False
End Sub
If you insist on 2 codes then for not blank it is
Code:
If WorksheetFunction.CountA(Range("A:B")) > 0 Then

Edit: Umm, I definitely need to find an easy way to see if someone else has posted on this new board :(
 
Upvote 0
This is the actual code I currently have. So it pastes selection from active sheet to Test 2, and, go to cell a1, go all the way to right, move 1 over , then paste the values. The issue is that if the first two columns are empty, this would return error. So I need to set up an if so that if column A if empty, it will go to a1 and paste there. If column B is empty, then it will go to B1, and paste it there. Otherwise, it will go to A1, go to the right, move 1 over and paste it there. Thanks a million!!


Sub CopyPaste()
'
' CopyPaste Macro

Selection.EntireColumn.Select
Selection.Copy
Sheets("Test 2").Select
Application.Goto Reference:="R1C1"
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveSheet.Paste
Application.Goto Reference:="R1C1"
ActiveSheet.Previous.Select
End Sub
 
Upvote 0
Not sure what you mean by
Otherwise, it will go to A1, go to the right, move 1 over and paste it there
if you mean the first column that is after the last column with data (in the below it is based on row 1, you need to tell us if that cell can ever be empty but data in other cells in the column) then try the code below.

VBA Code:
Sub testit()
    Selection.EntireColumn.Copy
    If WorksheetFunction.CountA(Range("A:A")) = 0 Then
        Range("A1").PasteSpecial
    ElseIf WorksheetFunction.CountA(Range("B:B")) = 0 Then
        Range("B1").PasteSpecial
    Else
        Cells(1, Columns.Count).End(xlToLeft).Offset(, 1).PasteSpecial
    End If
    Application.CutCopyMode = False
End Sub
 
Upvote 0
I think I figured it out :)
Sub CopyPaste()
'
' CopyPaste Macro

Selection.EntireColumn.Select
Selection.Copy
Sheets("Test 2").Select
ActiveSheet.Select
If WorksheetFunction.CountA(Range("A:A")) = 0 Then
ActiveSheet.Select
Cells(1, 1).Select
ActiveSheet.Paste
ActiveSheet.Previous.Select

ElseIf WorksheetFunction.CountA(Range("B:B")) = 0 Then
ActiveSheet.Select
Cells(1, 2).Select
ActiveSheet.Paste
ActiveSheet.Previous.Select


Else

Application.Goto Reference:="R1C1"
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveSheet.Paste
ActiveSheet.Previous.Select
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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