PDA

View Full Version : macro to select different sheet tabs



Dankzy
03-23-2019, 03:13 AM
Hi I'm new to this forum and very new to VBA codeing
I'm working on a PC using Excel 2013
I'm working on a simple accounting process - By using a button I want to copy data from an "invoice" sheet into a purchase ledger (All in the same workbook) - I've sorted this ok but want the purchase ledger to have 12 worksheets Jan - Dec
On the Invoice sheet I have a drop down list to determine the month (need to do this as the month end may vary) this is in Cell L2
So at the start of my macro I want to reference L2 and then select which sheet to copy too. I've looked for a solution to this but have been unsuccessful.

I hope someone can help

Cheers Dankzy

offthelip
03-23-2019, 04:16 AM
you haven't specified what range you wnat copied but this shows you how to select the sheet to copy it to. This code does depend on the text in cell L2 being exactly the same as the sheet name. if it isn't you will need to use a lookup table to convert it.

shtn = Range("L2")
Worksheets("Invoice").Range("A1").Copy Worksheets(shtn).Range("A1")

Dankzy
03-23-2019, 10:24 AM
offthelip - thanks for your help worked it into my code - worked at treat see below

'selects month in L2 to determine sheet tab
shtn = Range("L2")
'selects cells to copy on invoce tab
Worksheets("Invoice").Range("O8:T8").Select
'copies selection
Selection.Copy
' moves to sheet "shtn" selected in L2
Sheets(shtn).Select
'positions cursor in A1
Range("A1").Select
'moves down to last cell with data
Selection.End(xlDown).Select
'moves down one cell
ActiveCell.Offset(1, 0).Range("A1").Select
'past selection
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'moves cursor to end of data in row
ActiveCell.Offset(0, 6).Range("A1").Select