Microsoft Excel - excle sheet1 data save to selected (same workbook in) sheet (ie. sheet2, sheet3,......)

Asked By narendra patel on 31-Mar-13 07:29 AM

i have above code by save data "invoice" to "sheet3" ( High light),  But i want to save in sheets name as range("g4") .value ( as  13, 14 or 15 sheets name)

Please help me.

=======================
Private Sub CommandButton1_Click()
 Application.ScreenUpdating = False
If Worksheets("sheet2").Range("c5").Value <> "" And Worksheets("sheet2").Range("c6").Value <> "" And Worksheets("sheet2").Range("c11").Value <> "" And Worksheets("sheet2").Range("c7").Value <> "" And Worksheets("sheet2").Range("c12").Value <> "" Then
Application.ScreenUpdating = False
Sheets("sheet6").Visible = xlSheetVisible
mypassword = nn
ActiveSheet.Unprotect "mypassword"
Call Macroprint
ActiveSheet.Unprotect "mypassword"
Sheets("sheet6").Visible = xlSheetVeryHidden
Dim r As Range, j As Integer
Dim array1 As Variant
If Worksheets("sheet2").Range("c5").Value <> "" And Worksheets("sheet2").Range("c6").Value <> "" And Worksheets("sheet2").Range("c7").Value <> "" Then
With Worksheets("INVOICE")
array1 = Array(.Range("aa2"), .Range("ab2"), .Range("ac2"), .Range("ad2"), .Range("ae2"), .Range("af2"), .Range("ag2"), .Range("ah2"), .Range("ai2"), .Range("aj2"), .Range("ak2"), .Range("al2"), .Range("am2"), .Range("an2"), .Range("ao2"), .Range("ap2"))
With Worksheets("sheet3")
Set r = .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
For j = 0 To 15
 r.Offset(0, j) = array1(j)
Next j
End With
End With
End If
End If
For Each cell In [C5,C6,C7,C8,C9,C10,C11,C12]
If cell.Value <> "" Then cell.ClearContents
Next cell
ActiveWorkbook.SAVE
 Application.ScreenUpdating = True
End Sub
Harry Boughen replied to narendra patel on 01-Apr-13 12:24 AM
Hello Narendra,

If you make the bolded additions/changes below you should get some way towards doing what you want.  This code does not check for duplicate named sheets so could crash if you try to make sheets with an existing name.  It creates a new sheet at the end of the Workbook and then changes the name to the value in Cell G4.

Regards
Harry

Dim r As Range, j As Integer
Dim array1 As Variant
Dim WS As Worksheet
Dim strName As String


If Worksheets("sheet2").Range("c5").Value <> "" And Worksheets("sheet2").Range("c6").Value <> "" And Worksheets("sheet2").Range("c7").Value <> "" Then

'Create a new Worksheet
With ThisWorkBook
   Set WS = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
End With


With Worksheets("INVOICE")

'Record the new sheet name
strName = Range("G4").Value

array1 = Array(.Range("aa2"), .Range("ab2"), .Range("ac2"), .Range("ad2"), .Range("ae2"), .Range("af2"), .Range("ag2"), .Range("ah2"), .Range("ai2"), .Range("aj2"), .Range("ak2"), .Range("al2"), .Range("am2"), .Range("an2"), .Range("ao2"), .Range("ap2"))
With WS
Set r = .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
For j = 0 To 15
 r.Offset(0, j) = array1(j)
Next j
End With
End With

'Rename the sheet
WS.Name = strName