vba help please

shyamvinjamuri

Board Regular
Joined
Aug 2, 2006
Messages
175
we havea work book with 13 sheets. First sheet is view and the rest are months like Jan, Feb....etc.
Sheet View cell A1 is date input.
How can I copy columns B through Z from the 'month sheet' (eg Feb) into sheet view depending on the month of input in A1 in sheet View?

Please help
Thanks
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
This code will copy the columns to the "View" worksheet.
Code:
Sub NewTest1()
    sh = Sheets("View").Range("A1").Value
    Sheets(sh).Columns("B:Z").Copy Sheets("View").Range("B1")
End Sub
 
Upvote 0
View Sheet code module code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
  Columns("B:Z").Clear
  myMonth = Format(Range("A1").Value, "MMM")
  Set SceSht = ThisWorkbook.Sheets(myMonth)
  SceSht.Columns("B:Z").Copy Range("B1")
End If
End Sub
File here.

It's very basic, no checks, should get you started. Note, it's a date, not a month that you're supposed to enter in A1, as requested.
 
Last edited:
Upvote 0
Firstly put this in a standard module and run it, it will create a validationlist of all the worksheets in sheet1
Code:
Sub UpdateValidationList()
    
    Dim wsArray As Variant
    Dim sWsList As String
    Dim x As Integer
    
    wsArray = AllWorkSheets()
    'Separate array of worksheet names into a string separated by commas.
    sWsList = Join(wsArray, ",")
    
    'Add sWsList string to data validation for "A1"
    With Sheets(1).Range("A1").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:=sWsList
    End With
End Sub

Public Function AllWorkSheets() As Variant
    Dim wsArray() As Variant
    Dim x As Integer
    ReDim wsArray(Sheets.Count - 1)
    For x = 0 To Sheets.Count - 1
        wsArray(x) = Sheets(x + 1).Name
    Next x
    AllWorkSheets = wsArray
End Function
next add this to sheet1 code module (right click sheet tab and choose view code)
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Me.Range("A1").Value = Me.Name Then Exit Sub
Application.EnableEvents = False
Me.Range("A2:IV65536").ClearContents
Sheets(Range("A1").Value).Range("B1:Z" & Sheets(Range("A1").Value).Range("B" & Rows.Count).End(xlUp).Row).Copy
    Sheets("Sheet1").Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Application.CutCopyMode = False
        Application.EnableEvents = True
End Sub
now every time you change sheet names in A1 the used range gets copied over to sheet1
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,673
Members
452,937
Latest member
Bhg1984

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