From: Chris on
I have a data entry Form in excel 2007 that I created in vba. I was wondering
if instead of putting the data in the next sheet if it is possible to have it
put it into a workbook on a network. Here is my code.
Thanks!


Option Explicit

Sub GoInventory()
On Error Resume Next
Worksheets("Data").Activate
End Sub

Sub UpdateLogWorksheet()

Dim historyWks As Worksheet
Dim inputWks As Worksheet

Dim nextRow As Long
Dim oCol As Long

Dim myRng As Range
Dim myCopy As String
Dim myCell As Range

'cells to copy from Input sheet - some contain formulas
myCopy = "D5,D6,D7,D8,D9,D10"

Set inputWks = Worksheets("Input")
Set historyWks = Worksheets("Data")

With historyWks
nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
End With

With inputWks
Set myRng = .Range(myCopy)

If Application.CountA(myRng) <> myRng.Cells.Count Then
MsgBox "Please fill in all the cells!"
Exit Sub
End If
End With

With historyWks

oCol = 1
For Each myCell In myRng.Cells
historyWks.Cells(nextRow, oCol).Value = myCell.Value
oCol = oCol + 1
Next myCell
End With

'clear input cells that contain constants
With inputWks
On Error Resume Next
With .Range(myCopy).Cells.SpecialCells(xlCellTypeConstants)
.ClearContents
Application.GoTo .Cells(1) ', Scroll:=True
End With
On Error GoTo 0
End With
End Sub

Sub GoInput()
On Error Resume Next
Worksheets("Input").Activate
End Sub




From: Dave Peterson on
You (or your code) will have to open the history workbook first.

If you open it before your code runs, you can change this line:

Set historyWks = Worksheets("Data")
to
Set historyWks = workbooks("History.xls").Worksheets("Data")

If you want the code to open it, you could use:

Dim HistWkbk as workbook 'near the other declarations
....

Then this line:
Set historyWks = Worksheets("Data")

Gets replaced with this bunch of code:

'check to see if it's open first
set histwkbk = nothing
on error resume next
set histwkbk = workbooks("history.xls") 'don't include the path
on error goto 0

if histwkbk is nothing then
'not open yet, so try to open it
on error resume next
'include the complete path
set histwkbk = workbooks.open(filename:="C:\path\path\history.xls"
on error goto 0

if histwkbk is nothing then
msgbox "Can't find the history workbook"
exit sub
end if
end if

'try to find that history worksheet
set historywks = nothing
on error resume next
set historywks = histwkbk.worksheets("Data")
on error goto 0

if historywks is nothing then
msgbox "Found the workbook, but not the worksheet!
exit sub
end if

''''

(Untested, uncompiled. Watch for typos!)

Chris wrote:
>
> I have a data entry Form in excel 2007 that I created in vba. I was wondering
> if instead of putting the data in the next sheet if it is possible to have it
> put it into a workbook on a network. Here is my code.
> Thanks!
>
> Option Explicit
>
> Sub GoInventory()
> On Error Resume Next
> Worksheets("Data").Activate
> End Sub
>
> Sub UpdateLogWorksheet()
>
> Dim historyWks As Worksheet
> Dim inputWks As Worksheet
>
> Dim nextRow As Long
> Dim oCol As Long
>
> Dim myRng As Range
> Dim myCopy As String
> Dim myCell As Range
>
> 'cells to copy from Input sheet - some contain formulas
> myCopy = "D5,D6,D7,D8,D9,D10"
>
> Set inputWks = Worksheets("Input")
> Set historyWks = Worksheets("Data")
>
> With historyWks
> nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
> End With
>
> With inputWks
> Set myRng = .Range(myCopy)
>
> If Application.CountA(myRng) <> myRng.Cells.Count Then
> MsgBox "Please fill in all the cells!"
> Exit Sub
> End If
> End With
>
> With historyWks
>
> oCol = 1
> For Each myCell In myRng.Cells
> historyWks.Cells(nextRow, oCol).Value = myCell.Value
> oCol = oCol + 1
> Next myCell
> End With
>
> 'clear input cells that contain constants
> With inputWks
> On Error Resume Next
> With .Range(myCopy).Cells.SpecialCells(xlCellTypeConstants)
> .ClearContents
> Application.GoTo .Cells(1) ', Scroll:=True
> End With
> On Error GoTo 0
> End With
> End Sub
>
> Sub GoInput()
> On Error Resume Next
> Worksheets("Input").Activate
> End Sub

--

Dave Peterson
 | 
Pages: 1
Prev: Macro to Delete Duplicate Cells
Next: e-mail issue