473,796 Members | 2,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Excel VBA problem

63 New Member
--------------------------------------------------------------------------------

I have a bit of visual basic code in an excel spreadsheet that I need some help with. I am attempting to search a file called "TO Cancellations2. xls" for names and for each name that it finds that matches the list of names on the "counterparties " sheet of a file called Time Option Template WIP.xls I want the whole line copied and pasted into a new excel worksheet that should have the counterparty name and then FXTOX.xls after it.

The TO Cancellation2.x ls file looks like this

Counterparty
Oy AB 109385 H2 07
Oy AB 109389 H1 08
Oy AB 109390 H2 08
Oy AB 109387 H2 07
Oy AB 109388 H1 08
AB (Sweden) 109391 H2 08

The code below works ok but if the TO Cancellations2. xls file has the same counterparty more than once (as in this case with 5 different records against Oy AB) then instead of saving all 5 records into one file it overwrites the file each time with each of these 5 instances - only keeping the last record. Can anyone suggest any ideas on how to alter this code so that if there is more than one row countaining the same counterparty name it saves all of the rows to the same file and doesn't keep overwriting like it is currently doing?


Sheets("Counter parties").Selec t

' Initialise Counter
Countz = 0
For Each c In Range("A1:A100" )
If c.Value = "" Then
Exit For
Else
Countz = Countz + 1
End If
Next c

Sheets("Data2") .Select (NB data2 is a sheet in the Time Option Template WIP.xls file)
Range("A1").Sel ect

For Counter = 1 To Countz

Set Matching = Worksheets("Cou nterparties").C ells(Counter, 1)
Set Matching2 = Worksheets("Cou nterparties").C ells(Counter + 1, 1)
Set Matching3 = Worksheets("Cou nterparties").C ells(Counter + 2, 1)
Set Matching4 = Worksheets("Cou nterparties").C ells(Counter + 3, 1)
Set Matching5 = Worksheets("Cou nterparties").C ells(Counter + 4, 1)
Set Matching6 = Worksheets("Cou nterparties").C ells(Counter + 5, 1)
Set Matching7 = Worksheets("Cou nterparties").C ells(Counter + 6, 1)

For Each c In Range("A1:A5001 ")

If c.Value = Matching.Value Then
If c.Offset(0, 0).Value = Matching2.Value Or c.Offset(0, 0).Value = Matching3.Value Or c.Offset(0, 0).Value = Matching4.Value Or c.Offset(0, 0).Value = Matching5.Value Or c.Offset(0, 0).Value = Matching6.Value Or c.Offset(0, 0).Value = Matching7.Value Then
Else
c.EntireRow.Sel ect
Selection.Copy
Workbooks.Add
ActiveSheet.Pas te
Cells(2, 1).Select
stdocname = "Q:\Dealing Room\Dealing room administration\ Admin Things\Batch Report Masters\Outputs \" & Matching.Value & " FXTOX.xls"
ActiveWorkbook. SaveAs Filename:=stdoc name _
, FileFormat:=xlN ormal, Password:="", WriteResPasswor d:="", _
ReadOnlyRecomme nded:=False, CreateBackup:=F alse
Windows("Time Option Template WIP.xls").Activ ate
stdocname = Matching.Value & " FXTOX.xls"
Counti = 2
Do Until c.Offset(Counti , 0).Value = Matching2 Or c.Offset(Counti , 0).Value = Matching3 Or c.Offset(Counti , 0).Value = Matching4 Or c.Offset(Counti , 0).Value = Matching5 Or c.Offset(Counti , 0).Value = Matching6 Or c.Offset(Counti , 0).Value = Matching7 Or c.Offset(Counti , 0).Value = "Contact RG"
c.Offset(Counti , 0).EntireRow.Se lect
Selection.Copy
Windows(stdocna me).Activate
Cells(Counti + 1, 1).Select
ActiveSheet.Pas te
Windows("Time Option Template WIP.xls").Activ ate
Counti = Counti + 1
Loop
Apr 20 '09 #1
2 1694
ems9tech
12 New Member
When I have multiple rows that I need in a worksheet, I usually send them to a separate function as a recordset or dictionary item (you could use a collection also), and write them to the workbook.

I'm a little confused. Are you trying to write each type of data to a different Worksheet within the same Workbook or does each type of data go into a separate Workbook?
Apr 20 '09 #2
grego9
63 New Member
Thanks for the response. I am trying to save all the lines that have the same counterparty against them into the same workbook. The code I currently have takes each line and keeps saving over the previous record (by creating a new workbook each time). I really just the need the code to recognise that there may be more than one line item against each counterparty.

So as an example:

OY AB 545454545
AB SWEDEN 11313131
AB SWEDEN 65648787

I would want both AB Sweden entries saving in the AB Sweden spreadsheet. Currently the last entry saves over the first entry leaving me with just one record on the file

How would I send the items to a separate Function./Recordset - I don't know how to do this

thanks
Apr 27 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
3373
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to process this .txt file. Goal: I am working on a vba script to:
0
4201
by: Mike Knight | last post by:
(I've also posted this problem on microsoft.public.excel.programming) I have a MS Access 2003 Database named "AS400 Fields.mdb". This database contains links to tables on an AS400. In MS Excel 2003, I have VBA code that creates and executes queries using the Access database, and returns the results to an Excel sheet. The first time the query is executed, results are returned to Excel in usually less than 10 seconds. However, if the...
2
5438
by: Praveen K | last post by:
I have a problem in communicating between the C# and the Excel Interop objects. The problem is something as described below. I use Microsoft Office-XP PIA dll’s as these dll’s were been recommended by many for web applications. I create the instances of Excel, Workbook and the worksheet. And later on Release the references by “System.Runtime.InteropServices.Marshal.ReleaseComObject(Object)” and making the object as null finally....
0
1219
by: rhett | last post by:
howdy folks, first off, I'd like to not that I have browsed the forums and thanks to what I've found there I'm 80% through this problem, but the last part seems to be a profound obstacle and any help you could provide would be much appreciated. As my title indicates, I'm devloping a C# application which will need to opne up an excel template...manipulate and save it, and then quit. The problem I'm having is that the instance of...
4
2520
by: msnnews.msn.com | last post by:
hi there, i've got a form that populates a datagrid, and a button that calls a function to export to an excel file. All is well with the export, no errors are returned, but the Excel instance doesnt want to terminate properly even when i set excelApp.Quit, and excelApp = nothing. has anyone else experienced a problem like this??? code below:
18
3096
by: lgbjr | last post by:
Hi All, I have a VB.NET app that, among other things, writes data to Excel. I am having trouble getting the Excel process to terminate after I quit Excel. I found an article related to this problem: http://support.microsoft.com/default.aspx?scid=kb;en-us;317109 Below is a sample of code that I wrote based on the above article. The excel workbook, worksheets, and all of the cells are properly formatted when Excel
1
2580
by: Randall Arnold | last post by:
I'm converting a vbscript program to vb.net. Witht he exception of .net idiosyncrasies, most of it is working well with the same code. My only problem is that some properties and methods are missing from the Chart object. For instance, the following line worked fine in vbscript (csPowerSpeed is the chart): objXL.csPowerSpeed.seriescollection.newseries() However, in VS.Net, newseries is no longer avaialable as a method for series...
6
2298
by: Darrell Wesley | last post by:
A VB2003 application upgraded to VB2005 that builds an Excel spreadsheet. Everything appears to work correctly except that the Excel object does not go away it is still in the Process list in task manager. This problem did not exist in the VB2003 version. Are there a any ideas of what might be happening? All of the Excel objects are being disposed of by setting them to Nothing and the Quit method is called for Excel. I've even used...
2
5622
by: James | last post by:
I am doing some Excel 2000 automaton using Vb 2005. I am referencing the Excel 9.0 COM Object Library. The following code was working fine: Dim xlApp as new Excel.Application Dim xlWb as Excel.Workbook Dim xlSheet as Excel.Worksheet xlWb = xlApp.Workbooks.Add
0
9531
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.