VBA to paste into a table

Fester675

Board Regular
Joined
Sep 16, 2016
Messages
141
I already have a macro to copy & paste a range on 1 worksheet to a table on another sheet. However, I want to either....

a) paste into the next available column

or

b) paste into the column with the previous month's heading, based on today's date.

Any ideas? HELP!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Before:

Excel 2010
ABCDEF
1HeaderHeaderHeaderHeaderHeader
Sheet1


After:

Excel 2010
ABCDEF
1HeaderHeaderHeaderHeaderHeaderMy New Cell Value
Sheet1


Code to find next open column:
Code:
[COLOR=#0000ff]Sub [/COLOR]Test()


 [COLOR=#0000ff]   Dim [/COLOR]LCol [COLOR=#0000ff]As Integer[/COLOR]
  [COLOR=#0000ff]  Dim[/COLOR] myNewVal [COLOR=#0000ff]As String[/COLOR]
    
    myNewVal = "My New Cell Value"
    LCol = Cells(1, Columns.Count).End(xlToLeft).Column
    Cells(1, LCol + 1) = myNewVal


[COLOR=#0000ff]End Sub[/COLOR]

Find Last Month Column (If dates are end of month values):


Excel 2010
ABCDE
11/31/20172/28/20173/31/20174/30/20175/31/2017
Sheet1


Code:
[COLOR=#0000ff]Sub [/COLOR]FindPreviousMonthCol()


 [COLOR=#0000ff]   Dim [/COLOR]PrevMonthEnd[COLOR=#0000ff] As Date[/COLOR]
    [COLOR=#0000ff]Dim [/COLOR]rngMyCol[COLOR=#0000ff] As[/COLOR] Range
   [COLOR=#0000ff] Dim[/COLOR] myCol [COLOR=#0000ff]As Integer[/COLOR]
    
    PrevMonthEnd = Format(DateAdd("d", -1, DateSerial(Year(Date), Month(Date), 1)), "DD/MM/YYYY")
  [COLOR=#0000ff]  Set [/COLOR]rngMyCol = Rows(1).Find(PrevMonthEnd)
    myCol = rngMyCol.Column
    
[COLOR=#0000ff]End Sub[/COLOR]

This returns 4 for myCol
 
Upvote 0
Thanks for this mrmmickle1!
I presume I would enter either of these codes after the paste code - or before the End Sub?
 
Upvote 0
I believe so. Not quite sure since I don't know what your code looks like. You may have to fiddle around with the procedures a bit if your scenario does not involve data in Row 1. Just let me know if you run into issues. I'm happy to help.
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,461
Members
449,085
Latest member
ExcelError

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