Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 12
Default problem with comparing strings in VBA

Experts,

I need to copy data from once excel file to other based on string
comparison. For example consider two workboooks.

1. Source file

These are the contets in column A of source file corresponding

A1 - "Year"
A2 - "Month"
A3 - "Date"
A4 - "Hour"

2. target file

These are the contets column B in target file corresponding

B1 - "Year"
B2 - "Month"
B3 - "Date"
B4 - "Hour"

So if contents of cell A1 = contents of cell B1
AND
contents of cell A2 = contents of cell B2
AND
contents of cell A3 = Contents of cell B3
AND
contents of cell A4 = Contents of cell B4

Then
Copy contents C1:C4 from source to C1:C4 in target.

I am trying to do it with following code, but the problem is that my
code is copying the contents of range in cells columns "C" even when
contents of colums in A in source does not match with contents of
column B in target. Can somebody help??

This is what I have so far:

Dim source_column As Long
Dim source_row1 As Long
Dim source_row2 As Long
Dim source_row3 As Long
Dim source_row4 As Long

Dim target_column As Long
Dim target_row1 As Long
Dim target_row2 As Long
Dim target_row3 As Long
Dim target_row4 As Long

Dim source_string1 As String
Dim source_string2 As String
Dim source_string3 As String
Dim source_string4 As String

Dim target_string1 As String
Dim target_string2 As String
Dim target_string3 As String
Dim target_string4 As String

source_row1 = 1
source_row2 = 2
source_row3 = 3
source_row4 = 4
target_row1 = 1
target_row2 = 2
target_row3 = 3
target_row4 = 4

For target_column = 4 To 27
For source_column = 3 To 17
target_string1 =
wbTarget.Worksheets("bilateralgaspurchase").Cells( target_row1,
target_column).Value
source_string1 = wbSource.Worksheets("sheet1").Cells(source_row1,
source_column).Value
If target_string1 = source_string1 Then
target_string2 =
wbTarget.Worksheets("bilateralgaspurchase").Cells( target_row2,
target_column).Value
source_string2 =
wbSource.Worksheets("sheet1").Cells(source_row2, source_column).Value
If target_spring2 = target_spring2 Then
target_string3 =
wbTarget.Worksheets("bilateralgaspurchase").Cells( target_row3,
target_column).Value
source_string3 =
wbSource.Worksheets("sheet1").Cells(source_row3, source_column).Value
If target_spring3 = target_spring3 Then
target_string4 =
wbTarget.Worksheets("bilateralgaspurchase").Cells( target_row4,
target_column).Value
source_string4 =
wbSource.Worksheets("sheet1").Cells(source_row4, source_column).Value
If target_spring4 = target_spring4 Then
MsgBox "Strings matched are " & target_string1 &
source_string1 & target_string2 & source_string2 & target_string3 &
source_string3 & target_string4 & source_string4
:::::
::::::::
End If
End If
End If
End If
Next
Next

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 42
Default problem with comparing strings in VBA

On Jun 25, 1:45 pm, "
wrote:
Experts,

I need to copy data from once excel file to other based on string
comparison. For example consider two workboooks.

1. Source file

These are the contets in column A of source file corresponding

A1 - "Year"
A2 - "Month"
A3 - "Date"
A4 - "Hour"

2. target file

These are the contets column B in target file corresponding

B1 - "Year"
B2 - "Month"
B3 - "Date"
B4 - "Hour"

So if contents of cell A1 = contents of cell B1
AND
contents of cell A2 = contents of cell B2
AND
contents of cell A3 = Contents of cell B3
AND
contents of cell A4 = Contents of cell B4

Then
Copy contents C1:C4 from source to C1:C4 in target.

I am trying to do it with following code, but the problem is that my
code is copying the contents of range in cells columns "C" even when
contents of colums in A in source does not match with contents of
column B in target. Can somebody help??

This is what I have so far:

Dim source_column As Long
Dim source_row1 As Long
Dim source_row2 As Long
Dim source_row3 As Long
Dim source_row4 As Long

Dim target_column As Long
Dim target_row1 As Long
Dim target_row2 As Long
Dim target_row3 As Long
Dim target_row4 As Long

Dim source_string1 As String
Dim source_string2 As String
Dim source_string3 As String
Dim source_string4 As String

Dim target_string1 As String
Dim target_string2 As String
Dim target_string3 As String
Dim target_string4 As String

source_row1 = 1
source_row2 = 2
source_row3 = 3
source_row4 = 4
target_row1 = 1
target_row2 = 2
target_row3 = 3
target_row4 = 4

For target_column = 4 To 27
For source_column = 3 To 17
target_string1 =
wbTarget.Worksheets("bilateralgaspurchase").Cells( target_row1,
target_column).Value
source_string1 = wbSource.Worksheets("sheet1").Cells(source_row1,
source_column).Value
If target_string1 = source_string1 Then
target_string2 =
wbTarget.Worksheets("bilateralgaspurchase").Cells( target_row2,
target_column).Value
source_string2 =
wbSource.Worksheets("sheet1").Cells(source_row2, source_column).Value
If target_spring2 = target_spring2 Then
target_string3 =
wbTarget.Worksheets("bilateralgaspurchase").Cells( target_row3,
target_column).Value
source_string3 =
wbSource.Worksheets("sheet1").Cells(source_row3, source_column).Value
If target_spring3 = target_spring3 Then
target_string4 =
wbTarget.Worksheets("bilateralgaspurchase").Cells( target_row4,
target_column).Value
source_string4 =
wbSource.Worksheets("sheet1").Cells(source_row4, source_column).Value
If target_spring4 = target_spring4 Then
MsgBox "Strings matched are " & target_string1 &
source_string1 & target_string2 & source_string2 & target_string3 &
source_string3 & target_string4 & source_string4
:::::
::::::::
End If
End If
End If
End If
Next
Next


This seems a hard way to do it. But the problem that you are having
is that your if statments are comparing the same variable and it is
not the variable you think it is. You are comparing TARGET_SPRING4 to
TARGET_SPRING4 when I think you want TARGET_STRING4=SOURCE_STRING4
The only one that is correct is the 1st one.

Pete

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 12
Default problem with comparing strings in VBA

Thanks!!

I corrected it meanwhile.. :)

Thanks

On Jun 25, 3:57 pm, "
wrote:
On Jun 25, 1:45 pm, "
wrote:





Experts,


I need to copy data from once excel file to other based on string
comparison. For example consider two workboooks.


1. Source file


These are the contets in column A of source file corresponding


A1 - "Year"
A2 - "Month"
A3 - "Date"
A4 - "Hour"


2. target file


These are the contets column B in target file corresponding


B1 - "Year"
B2 - "Month"
B3 - "Date"
B4 - "Hour"


So if contents of cell A1 = contents of cell B1
AND
contents of cell A2 = contents of cell B2
AND
contents of cell A3 = Contents of cell B3
AND
contents of cell A4 = Contents of cell B4


Then
Copy contents C1:C4 from source to C1:C4 in target.


I am trying to do it with following code, but the problem is that my
code is copying the contents of range in cells columns "C" even when
contents of colums in A in source does not match with contents of
column B in target. Can somebody help??


This is what I have so far:


Dim source_column As Long
Dim source_row1 As Long
Dim source_row2 As Long
Dim source_row3 As Long
Dim source_row4 As Long


Dim target_column As Long
Dim target_row1 As Long
Dim target_row2 As Long
Dim target_row3 As Long
Dim target_row4 As Long


Dim source_string1 As String
Dim source_string2 As String
Dim source_string3 As String
Dim source_string4 As String


Dim target_string1 As String
Dim target_string2 As String
Dim target_string3 As String
Dim target_string4 As String


source_row1 = 1
source_row2 = 2
source_row3 = 3
source_row4 = 4
target_row1 = 1
target_row2 = 2
target_row3 = 3
target_row4 = 4


For target_column = 4 To 27
For source_column = 3 To 17
target_string1 =
wbTarget.Worksheets("bilateralgaspurchase").Cells( target_row1,
target_column).Value
source_string1 = wbSource.Worksheets("sheet1").Cells(source_row1,
source_column).Value
If target_string1 = source_string1 Then
target_string2 =
wbTarget.Worksheets("bilateralgaspurchase").Cells( target_row2,
target_column).Value
source_string2 =
wbSource.Worksheets("sheet1").Cells(source_row2, source_column).Value
If target_spring2 = target_spring2 Then
target_string3 =
wbTarget.Worksheets("bilateralgaspurchase").Cells( target_row3,
target_column).Value
source_string3 =
wbSource.Worksheets("sheet1").Cells(source_row3, source_column).Value
If target_spring3 = target_spring3 Then
target_string4 =
wbTarget.Worksheets("bilateralgaspurchase").Cells( target_row4,
target_column).Value
source_string4 =
wbSource.Worksheets("sheet1").Cells(source_row4, source_column).Value
If target_spring4 = target_spring4 Then
MsgBox "Strings matched are " & target_string1 &
source_string1 & target_string2 & source_string2 & target_string3 &
source_string3 & target_string4 & source_string4
:::::
::::::::
End If
End If
End If
End If
Next
Next


This seems a hard way to do it. But the problem that you are having
is that your if statments are comparing the same variable and it is
not the variable you think it is. You are comparing TARGET_SPRING4 to
TARGET_SPRING4 when I think you want TARGET_STRING4=SOURCE_STRING4
The only one that is correct is the 1st one.

Pete- Hide quoted text -

- Show quoted text -



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find number of pairs of strings from list of strings? greg_overholt Excel Worksheet Functions 5 January 27th 06 10:42 PM
Comparing text strings in cells Andy Excel Discussion (Misc queries) 0 January 11th 06 10:38 AM
Comparing text strings in cells Andy Excel Discussion (Misc queries) 0 December 7th 05 04:17 PM
last name, first name strings data_diva New Users to Excel 6 November 18th 05 10:39 PM
How can I count strings within strings Paul W Excel Worksheet Functions 4 June 14th 05 12:39 PM


All times are GMT +1. The time now is 01:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"