View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.newusers
JLatham JLatham is offline
external usenet poster
 
Posts: 2,203
Default VBA copy cell to another worksheet

I hope I have understood your needs and that this helps you. I don't believe
you need to use the clipboard for this. You can copy the value in cell B2 on
the active sheet (the one with the command button on it) and place it into
the other workbook.

Also, you were opening the sblocco.xls workbook as Read Only which means you
could not save the change you make to it. I have changed that.

Sub CopyCellValue()
Dim ObjWorshett As Worksheet
Dim strNomeFile As String
Dim UltimaRiga As Long
Dim Riga As Long

' new variables used
Dim objWorkbook As Workbook
Dim valueFromThisWorkbook As Variant

'get the value in Cell B2 on the sheet
'that is active in this workbook
valueFromThisWorkbook = ActiveSheet.Range("B2").Value

strNomeFile = "\\srv01\Dp\ANTONELLA\sblocco.xls"

'open the other workbook, do not update links, _
do NOT open as read only
Set objWorkbook = Workbooks.Open(strNomeFile, False, False)
Set ObjWorshett = objWorkbook.Sheets(1)

UltimaRiga = ObjWorshett.Range("A" & Rows.Count).End(xlUp).Row
Riga = UltimaRiga + 1
'if you do not need UltimaRiga later, you can rewrite those
'two statements as one:
' Riga = ObjWorshett.Range("A" & Rows.Count).End(xlUp).Row + 1

ObjWorshett.Cells(Riga, 1).Value = valueFromThisWorkbook
Set ObjWorshett = Nothing
objWorkbook.Close True ' close and save changes
Set objWorkbook = Nothing
End Sub


"franco monte" wrote:

Hello, I am trying to copy text information from one worksheet to
another.
I open only the first sheet then I have a comand button by where I
want to say transfer the values.

La macro give an error at ObjWorshett.Cells(Riga, 1).text =
oDO.GetText
Errore di run-time '1004'.
Errore definito dall'applicazione o dall'oggetto.
Thanks in advance!
Franco



Dim oDO As New DataObject
oDO.SetText [B2].Value
oDO.PutInClipboard

Dim ObjWorshett As Worksheet
Dim strNomeFile As String

strNomeFile = "\\srv01\Dp\ANTONELLA\sblocco.xls"
Set ObjWorshett = Application.Workbooks.Open(strNomeFile, False,
True).Sheets(1)
UltimaRiga = ObjWorshett.Range("A65356").End(xlUp).Row
Riga = UltimaRiga + 1

oDO.GetFromClipboard
ObjWorshett.Cells(Riga, 1).text = oDO.GetText
Set oDO = Nothing

ObjWorshett.Application.ActiveWorkbook.Close
.