Help with my excel and vba

ellin00ra

New Member
Joined
Oct 2, 2004
Messages
41
I have managed to do (read:copy-paste) a bit of a code by myself, but now can't figure out couple of mistakes. First of all this makro that does the searching, searches also the temp file in that workbook, so there are duplicate rows in the end...

And then it shoud search whole sheets instead pieces of them...

I'll copy the code here, if someone could help me out..

Thanks
-e-

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Dim hakemisto As String, tied As String
Dim rivi As Integer

rivi = ActiveCell.Row

valittu = ActiveCell.Address
Range("A" & rivi).Select


'Jos muu sarake A on tyhjä - lopetetaan
If Range("A" & rivi) = "" Then Exit Sub


' *** ASETUKSET *****
hakemisto = "C:\"
tied = "kauppaloki.doc"

'********************



' MUUTTUJAT
yhtio = Range("A" & rivi)
etunimi = Range("Z" & rivi)
osoite = Range("AD" & rivi)
postinro = Range("AF" & rivi)
osakemaara = Range("AO" & rivi)

'EPÄSELVÄT
tilinro = Range("AX" & rivi)
kplhinta = Range("AY" & rivi)
verotuskunta = Range("AZ" & rivi)
puhnro = Range("BA" & rivi)
syntaika = Range("BB" & rivi)

Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True

Set wrdDoc = WordApp.Documents.Open(Filename:=hakemisto & tied)

wrdDoc.FormFields("yhtiö").Result = yhtio
wrdDoc.FormFields("etunimi").Result = etunimi
wrdDoc.FormFields("katuosoite").Result = osoite
wrdDoc.FormFields("Postiosoite").Result = postinro
wrdDoc.FormFields("Osakemäärä").Result = osakemaara

wrdDoc.FormFields("Tilinumero").Result = tilinro
wrdDoc.FormFields("Kauppahinta").Result = kplhinta * osakemaara
wrdDoc.FormFields("Kotikunta").Result = verotuskunta
wrdDoc.FormFields("Puhelinnumero").Result = puhnro
wrdDoc.FormFields("Syntymäaika").Result = syntaika

Dim oField As Field
Dim oStory As Range

For Each oField In wrdDoc.Fields
If oField.Type = wdFieldRef Then
oField.Update
End If
Next oField

'wrdDoc.Close savechanges:=True
'Set wrdDoc = Nothing
'WordApp.Quit
'Set WordApp = Nothing

End Sub

Dim oStory As Range

For Each oField In wrdDoc.Fields
If oField.Type = wdFieldRef Then
oField.Update
End If
Next oField

wrdDoc.Close savechanges:=True
Set wrdDoc = Nothing
WordApp.Quit
Set WordApp = Nothing

End Sub

Sub SearchandCopy()

Dim oSheet As Object
Dim Firstcell As Range
Dim NextCell As Range
Dim WhatToFind As Variant
Dim rCopyCells As Range

On Error GoTo Err

Application.ScreenUpdating = False
Sheets("Temp").Select
Range("A2:D1000").Select
Selection.ClearContents
Sheets("Vero").Select
Application.ScreenUpdating = True

WhatToFind = Application.InputBox("Kirjoita nimi?", "Etsi", , 100, 100, , , 2)

If WhatToFind = False Then
Sheets("Vero").Select
End
End If

If WhatToFind <> "" And Not WhatToFind = False Then
For Each oSheet In ActiveWorkbook.Worksheets
oSheet.Activate
oSheet.[a1].Activate
Set Firstcell = Cells.Find(What:=WhatToFind, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If Not Firstcell Is Nothing Then
Firstcell.Activate
If MsgBox("Add Record", vbInformation + vbYesNo) = vbYes Then
ActiveCell.Select
Selection.EntireRow.Copy Destination:= _
Sheets("Temp").Range("A65536").End(xlUp).Offset(1, 0)

Set rCopyCells = Nothing
End If

On Error Resume Next
While (Not NextCell Is Nothing) And (Not NextCell.Address = Firstcell.Address)
Set NextCell = Cells.FindNext(After:=ActiveCell)
If Not NextCell.Address = Firstcell.Address Then
NextCell.Activate
If MsgBox("Add Record", vbInformation + vbYesNo) = vbYes Then
ActiveCell.Select
Selection.EntireRow.Copy Destination:= _
Sheets("Temp").Range("A65536").End(xlUp).Offset(1, 0)

End If
End If
Wend
End If
Set NextCell = Nothing
Set Firstcell = Nothing
Next oSheet
End If

Application.ScreenUpdating = False
Sheets("Temp").Select
Range("A2,AA1300").Select
Application.ScreenUpdating = True
Sheets("Vero").Select
Sheets("Temp").Copy
End
Err:
MsgBox "Sorry, virhe. Yritä uudelleen!"
End
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,203,453
Messages
6,055,530
Members
444,794
Latest member
HSAL

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top