I want to make a interface in vb6 which deals with excel file. Where we can select drive from drive box and select a excel file which is a source file and copy the value of cell from the source file and paste it into destination file's cell. my problem is i want it dynamic mean i will take excel file, sheet and cell from user in runtime.
(this is the source excel file from where i want to copy the value of the cell)I m selecting excel file from filelistbox and then selecting a sheet from combobox where the sheets will b listed and then giving cell number from textbox and with d help of the command i show data that cell contains in a text box.
(this the destination file where i want to paste the cell value that i copied) again in same way i giving the destination file and sheet n cell . i want the code for a command button which will paste the value of copied cell to the destination cell. i tried many thing but nothing works.
i made 2-3 function for copy cell but it doesnt work And i m using VB6
this is my code
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowcmd As Long) As Long
Private Const SW_SHOWNORMAL = 1
Private Sub Check1_Click()
MsgBox ("ok")
End Sub
Private Sub Form_Load()
Text3.Text = Text
Text4.Text = Text
Text7.Text = Text
Text8.Text = Text
End Sub
Private Sub Combo1_Click()
Text3.Text = Combo1.Text
End Sub
Private Sub Combo2_Click()
Text4.Text = Combo2.Text
End Sub
Private Sub gnt_db_cmd_5_Click()
Dim dbApp As Excel.Application
Dim dbBook As Excel.Workbook
Set dbApp = New Excel.Application
Set dbBook = dbApp.Workbooks.open(Text7.Text)
dbApp.Visible = True
Set dbBook = Nothing
Set dbApp = Nothing
End Sub
Private Sub exit_Click()
End
End Sub
Private Sub clearall_Click()
Text1.Text = " "
Text2.Text = " "
Text3.Text = " "
Text4.Text = " "
Text5.Text = " "
Text6.Text = " "
Text7.Text = " "
Text8.Text = " "
Text9.Text = " "
Combo1.Clear
Combo2.Clear
End Sub
Private Sub Dir1_Change()
File1.Pattern = "*.xlsb;*.xls;*.xlsx;*.xlsm;*.xltx;*.xlt;*xml;*.xlam;*xla;*.xlw;*.xll;*.xltm;*.xlm"
File1.Path = Dir1.Path
End Sub
Private Sub Drive1_Change()
If Option1.Value = True Then
Dir1.Path = Left$(Drive1.Drive, 1) & ":\"
Else:
If Option2.Value = True Then Dir1.Path = Left$(Drive1.Drive, 1) & ":\"
End If
End Sub
Private Sub File1_Click()
If Option1.Value = True Then
Text1 = File1.List(File1.ListIndex)
Text7 = File1.Path & "\" & File1
Combo1.Clear
Else:
If Option2.Value = True Then
Text2 = File1.List(File1.ListIndex)
Text8 = File1.Path & "\" & File1
Combo2.Clear
End If
End If
End Sub
Private Sub File1_DblClick()
FileName = File1.Path
If Right$(FileName, 1) <> "\" Then FileName = FileName & "\"
FileName = FileName & File1.FileName
ShellExecute Me.hwnd, vbNullString, File1.FileName, vbNullString, Dir1.Path, SW_SHOWNORMAL
End Sub
Private Sub Combo1_GotFocus()
If Text7.Text = "" Then
Text7.Text = ""
Else
'Dim MyXLApp As Excel.Application
'Dim MyXLWorkBook As Excel.Workbook
'Set MyXLApp = New Excel.Application
'Set MyXLWorkBook = MyXLApp.Workbooks.Open(Text7.Text)
'For i = 1 To MyXLWorkBook.Sheets.Count
'Set myXLSheet = MyXLWorkBook.Sheets(i)
'With my_XLSheet
'.Unprotect
'.Select
'End With '
'Combo1.AddItem myXLSheet.Name
'Next i
'End If
Dim oxl As Object
Set oxl = CreateObject("Excel.Application")
Set oxlwbk = oxl.Workbooks.open(Text7.Text)
For i = 1 To oxlwbk.Sheets.Count
Set oxlsht = oxlwbk.Sheets(i)
With oxlsht
.Unprotect
End With
Combo1.AddItem oxlsht.Name
Next i
End If
End Sub
Private Sub Combo2_GotFocus()
If Text8.Text = "" Then
Text8.Text = ""
Else
Dim My_XLApp As Excel.Application
Dim My_XLWorkBook As Excel.Workbook
Set My_XLApp = New Excel.Application
Set My_XLWorkBook = My_XLApp.Workbooks.open(Text8.Text)
For i = 1 To My_XLWorkBook.Sheets.Count
Set my_XLSheet = My_XLWorkBook.Sheets(i)
With my_XLSheet
.Unprotect
'.Select
End With
Combo2.AddItem my_XLSheet.Name
Next i
End If
End Sub
Private Sub Command1_Click()
Dim objExcel As New Excel.Application ' Source Excel
Dim objWorkbook As Excel.Workbook 'source workbook object
Dim objWorksheet As Excel.worksheet 'source worksheet object
Set objWorkbook = objExcel.Workbooks.open(Text8.Text)
Set objWorksheet = objWorkbook.Sheets(Text4.Text)
Text9.Text = objWorksheet.Range(Text6.Text).Value
End Sub
Private Sub Command4_Click()
If Text7.Text = "" Then
MsgBox ("Enter the Cell Number")
Else
'copy
'CopyOpenItems
copy_c
End If
End Sub
Sub copy()
Dim xl As New Excel.Application
Dim wbksour As Workbook
Dim wbkdes As Workbook
Dim strFirstFile As String
Dim strSecondFile As String
strFirstFile = Text8.Text
strSecondFile = Text7.Text
Set wbksour = xl.Workbooks.open(strFirstFile)
With wbksour.Sheets(Text4.Text)
.Range(Text6.Text).copy
End With
Set wbkdes = xl.Workbooks.open(strSecondFile, , False)
With wbkdes
.Activate
.ReadOnlyRecommended = False
End With
With wbkdes.Sheets(Text3.Text)
.Range(Text5.Text).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With
With wbkdes
.Activate
.Save
.Close False, Text7.Text
End With
xl.quit
Set wbkdes = Nothing
Set xl = Nothing
End Sub
Sub copy_c()
Dim wbksour As Object
Dim Source As Object
Dim wbkdes As Object
Dim des As Object
Dim strFirstFile As String
Dim strSecondFile As String
strFirstFile = Text8.Text
strSecondFile = Text7.Text
Application.ScreenUpdating = False
Set Source = CreateObject("Excel.Application")
Set wbksour = Source.Workbooks.open(strFirstFile)
With wbksour.Sheets(Text4.Text)
.Range(Text6.Text).copy
End With
'wbksour.Sheets(Text4.Text).Range(Text6.Text).copy
Set des = CreateObject("Excel.Application")
Set wbkdes = des.Workbooks.open(strSecondFile, , False)
With wbkdes
.Activate
.ReadOnlyRecommended = False
End With
With wbkdes.Sheets(Text3.Text)
.Range(Text5.Text).PasteSpecial Paste:=xlPasteValues
End With
'wbkdes.Sheets(Text3.Text).Range(Text5.Text).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
'False, Transpose:=False
With wbkdes
.Activate
.Save
.Close
End With
Application.ScreenUpdating = True
End Sub
Sub CopyOpenItems()
Dim xl As New Excel.Application
Dim wbTarget As Workbook 'workbook where the data is to be pasted
Dim wbThis As Workbook 'workbook from where the data is to copied
Dim strName As String 'name of the source sheet/ target workbook
'set to the current active workbook (the source book)
Set wbThis = xl.Workbooks.open(Text8.Text)
'get the active sheetname of the book
strName = ActiveSheet.Name
'open a workbook that has same name as the sheet name
Set wbTarget = xl.Workbooks.open(Text7.Text, , False)
'select cell A1 on the target book wbTarget.Range("A1").Select
'clear existing values form target book
wbTarget.Sheets(Text3.Text).Range(Text5.Text).Clear
'activate the source book
wbThis.Activate
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
'copy the range from source book
wbThis.Sheets(Text4.Text).Range(Text6.Text).copy
'paste the data on the target book
wbTarget.Sheets(Text3.Text).Range(Text5.Text).PasteSpecial
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
'save the target book
wbTarget.Save
'close the workbook
wbTarget.Close
'activate the source book again
wbThis.Activate
'clear memory
Set wbTarget = Nothing
Set wbThis = Nothing
End Sub