Input data in one sheet and store data in another workbook (Macro)
Hi,
Please can someone assist me with my code below, i am tying to get this to work, i input data in one sheet and then want to store the data in another workbook, if i save the data to the same workbook the code works fine, but when i attempt to save to another workbook, it records the data but does not close the workbook, i am also looking at sending this out to multiple users, have researched so many alternatives but none seem to work, there is talk about ADO but my knowledge is low on that...hoping that someoone can help with this...
Private Sub Auto_Open()
MsgBox ("Welcome to the Internal Collaboration Workbook")
Worksheets("Sheet1").Activate
Range("c2").Activate
End Sub
Sub RecordSale()
Dim agent As String, varResponse As Variant
Dim dateofc As String, curCommission As String
Dim introfeedback As String, smfeedback As String
Dim posfeedback As String, prodfeedback As String
Dim closefeedback As String, mnumber As String
Dim mname As String, sm As String
Dim mtype As String, cperson As String
On Error GoTo Handler:
Worksheets("Sheet1").Activate
agent = Range("c2").Value
dateofc = Range("c3").Value
introfeedback = Range("b19").Value
smfeedback = Range("c19").Value
posfeedback = Range("e19").Value
prodfeedback = Range("f19").Value
closefeedback = Range("g19").Value
mnumber = Range("f2").Value
mname = Range("c4").Value
sm = Range("f5").Value
mtype = Range("f4").Value
cperson = Range("f6").Value
Worksheets("sheet3").Visible = False
Worksheets("sheet3").Activate
Range("A2").Activate
Do
If ActiveCell.Value = "" Then Exit Do
ActiveCell.Offset(1, 0).Activate
Loop
ActiveCell.Value = mname
ActiveCell.Offset(0, 1).Value = dateofc
ActiveCell.Offset(0, 2).Value = mnumber
ActiveCell.Offset(0, 3).Value = agent
ActiveCell.Offset(0, 4).Value = sm
ActiveCell.Offset(0, 5).Value = mtype
ActiveCell.Offset(0, 6).Value = cperson
ActiveCell.Offset(0, 7).Value = introfeedback
ActiveCell.Offset(0, 8).Value = smfeedback
ActiveCell.Offset(0, 9).Value = posfeedback
ActiveCell.Offset(0, 10).Value = prodfeedback
ActiveCell.Offset(0, 11).Value = closefeedback
Worksheets("Sheet1").Activate
varResponse = MsgBox("Your Feedback has been recorded, thanks" & curCommission & ". Please select another merchant from the dropdown list", vbYesNo)
Application.ScreenUpdating = False
If varResponse = vbNo Then
Worksheets("Sheet1").Activate
Range("b2").Value = ""
Range("b3").Value = ""
Range("b19").Value = ""
Range("c19").Value = ""
Range("e19").Value = ""
Range("f19").Value = ""
Range("g19").Value = ""
Application.EnableEvents = False
ActiveWorkbook.Save
ActiveWorkbook.Close
Else
Worksheets("Sheet1").Activate
Range("c2").Value = ""
Range("c3").Value = ""
Range("b19").Value = ""
Range("c19").Value = ""
Range("e19").Value = ""
Range("f19").Value = ""
Range("g19").Value = ""
End If
Application.ScreenUpdating = True
Exit Sub
Handler:
MsgBox ("Please contact Sabir 0313673651")
End Sub
- Sabzero786's blog
- Login or register to post comments
- 6801 reads
Hi, Do you want to copy/save
Hi,
Do you want to copy/save one of your worksheet in a new workbook ?
Or, do you want to copy the whole workbook ?
Sending a file is possible, check the "sendmail" vba method or methods relative to MS Outlook if use it.
I tried cleaning your macro, here's the module.
'declarations for welcome sheet and cell
Dim rgWelcome As Range
Dim shWelcome As Worksheet
' declarations to help pasting informations
Dim shTarget1 As Worksheet
Dim rgTarget1Cell As Range ' helps to determine from where to paste
Dim rgTarget2Cell As Range ' contains where to paste
'declarations for welcome dialogbox
Dim MsgTitle As String
Dim MsgText As String
' give here the name of the welcome and target worksheet if it's not variable, and startrange
Const sShWelcome As String = "Feuil1"
Const sShTarget1 As String = "Feuil3"
Const srgTarget1Cell As String = "A2"
Private Sub Auto_Open()
MsgTitle = "ICW Welcome"
MsgText = "Welcome to the Internal Collaboration Workbook"
MsgBox MsgText, vbInformation + vbOKOnly, MsgTitle
Set shWelcome = Worksheets(sShWelcome)
shWelcome.Activate
ActiveSheet.Range("c2").Activate
End Sub
Sub RecordSale()
Dim agent As String, varResponse As Variant
Dim dateofc As String, curCommission As String
Dim introfeedback As String, smfeedback As String
Dim posfeedback As String, prodfeedback As String
Dim closefeedback As String, mnumber As String
Dim mname As String, sm As String
Dim mtype As String, cperson As String
' disable the screen refresh
Application.ScreenUpdating = False
On Error GoTo Handler:
' Activate welcome sheet
Set shWelcome = Worksheets(sShWelcome)
shWelcome.Activate
agent = Range("c2").Value
dateofc = Range("c3").Value
introfeedback = Range("b19").Value
smfeedback = Range("c19").Value
posfeedback = Range("e19").Value
prodfeedback = Range("f19").Value
closefeedback = Range("g19").Value
mnumber = Range("f2").Value
mname = Range("c4").Value
sm = Range("f5").Value
mtype = Range("f4").Value
cperson = Range("f6").Value
' Set in a worksheet object variable the worksheet to update
Set shTarget1 = Worksheets(sShTarget1)
With shTarget1
'Hide this worksheet
.Visible = True
' Determine where to pass the info, starting in A2
Set rgTarget1Cell = .Range(srgTarget1Cell)
Set rgTarget2Cell = .Cells(IIf(.Cells(.Rows.Count, 1).End(xlUp).Row < rgTarget1Cell.Row, rgTarget1Cell.Row, .Cells(.Rows.Count, 1).End(xlUp).Row + 1), 1)
Debug.Print rgTarget2Cell.Address(external:=True)
End With
' Data pasting below
With rgTarget2Cell
.Value = mname
.Cells(1, 1).Value = dateofc
.Cells(1, 2).Value = mnumber
.Cells(1, 3).Value = agent
.Cells(1, 4).Value = sm
.Cells(1, 5).Value = mtype
.Cells(1, 6).Value = cperson
.Cells(1, 7).Value = introfeedback
.Cells(1, 8).Value = smfeedback
.Cells(1, 9).Value = posfeedback
.Cells(1, 10).Value = prodfeedback
.Cells(1, 11).Value = closefeedback
End With
MsgText = "Your Feedback has been recorded, thanks" & curCommission & ". Please select another merchant from the dropdown list"
MsgTitle = "ICW - Pursing with another merchant ?"
varResponse = MsgBox(MsgText, vbInformation + vbYesNo, MsgTitle)
shWelcome.Activate
If varResponse = vbNo Then
Range("B2, B3, B19, C19, E19, F19, G19").ClearContents
Application.EnableEvents = False
ActiveWorkbook.Save
ActiveWorkbook.Close
Else
Range("C2, C3, B19, C19, E19, F19, G19").ClearContents
End If
Application.ScreenUpdating = True
Exit Sub
Handler:
MsgBox ("Please contact Sabir 0313673651")
End Sub