ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Trying To Append String Variable To Another String Variable (https://www.excelbanter.com/excel-programming/398814-trying-append-string-variable-another-string-variable.html)

Joe K.

Trying To Append String Variable To Another String Variable
 

I have several if statements listed below, that correspond to the string
values in the "B" column. If the .Cells(iRow, "B").Text= "BASE" then
prodcode = "0003408909". Please help me add the prodcode string to the
myStr string listed below.

Please help me modify the code listed below to add the prodcode to the myStr
string listed below.

Thanks so much for the help.

Dim prodcode As String
Dim myStr As String
Dim wks As Worksheet


With wks
FirstRow = 1
FirstCol = 3
For iRow = FirstRow + 1 To 6
If .Cells(iRow, "B").Text = "BASE" Then
prodcode = "0003408909"
ElseIf .Cells(iRow, "B").Text = "TEAM" Then
prodcode = "0003498909"
ElseIf .Cells(iRow, "B").Text = "COURSE" Then
prodcode = "0003407909"
End If
For iCol = FirstCol + 1 To 64
myStr = .Cells(1, iCol).Text & "," _
& prodcode & "," _
& .Cells(iRow, iCol).Text
Print #1, myStr
Next iCol
Next iRow
End With

JLGWhiz

Trying To Append String Variable To Another String Variable
 
The only thing I see that might cause a problem is:

Print #1, myStr

I have never seen that used before. What is it supposed to do?

"Joe K." wrote:


I have several if statements listed below, that correspond to the string
values in the "B" column. If the .Cells(iRow, "B").Text= "BASE" then
prodcode = "0003408909". Please help me add the prodcode string to the
myStr string listed below.

Please help me modify the code listed below to add the prodcode to the myStr
string listed below.

Thanks so much for the help.

Dim prodcode As String
Dim myStr As String
Dim wks As Worksheet


With wks
FirstRow = 1
FirstCol = 3
For iRow = FirstRow + 1 To 6
If .Cells(iRow, "B").Text = "BASE" Then
prodcode = "0003408909"
ElseIf .Cells(iRow, "B").Text = "TEAM" Then
prodcode = "0003498909"
ElseIf .Cells(iRow, "B").Text = "COURSE" Then
prodcode = "0003407909"
End If
For iCol = FirstCol + 1 To 64
myStr = .Cells(1, iCol).Text & "," _
& prodcode & "," _
& .Cells(iRow, iCol).Text
Print #1, myStr
Next iCol
Next iRow
End With


JLGWhiz

Trying To Append String Variable To Another String Variable
 
One other thing. Instead of .Cells(iRow, "B").Text = "BASE", I would
use: .Cells(iRow, "B").Value = "BASE"

"Joe K." wrote:


I have several if statements listed below, that correspond to the string
values in the "B" column. If the .Cells(iRow, "B").Text= "BASE" then
prodcode = "0003408909". Please help me add the prodcode string to the
myStr string listed below.

Please help me modify the code listed below to add the prodcode to the myStr
string listed below.

Thanks so much for the help.

Dim prodcode As String
Dim myStr As String
Dim wks As Worksheet


With wks
FirstRow = 1
FirstCol = 3
For iRow = FirstRow + 1 To 6
If .Cells(iRow, "B").Text = "BASE" Then
prodcode = "0003408909"
ElseIf .Cells(iRow, "B").Text = "TEAM" Then
prodcode = "0003498909"
ElseIf .Cells(iRow, "B").Text = "COURSE" Then
prodcode = "0003407909"
End If
For iCol = FirstCol + 1 To 64
myStr = .Cells(1, iCol).Text & "," _
& prodcode & "," _
& .Cells(iRow, iCol).Text
Print #1, myStr
Next iCol
Next iRow
End With


JLGWhiz

Trying To Append String Variable To Another String Variable
 
One more thing, you should probably add a line after your Dim statements like:

Wks = ActiveSheet
Or

Wks = Sheets(1)

Or whatever sheet your data is on. Although the Dim declaration tells the
compiler that Wks is a Worksheet, it does not tell it which one, so when you
use the:

With Wks

Statement without specifying which sheet it applies to, the compiler will
either ignore it or guess that it is the active sheet. So the outcome might
not be what you want.

So here is how I would write the code:

Dim prodcode As String
Dim myStr As String
Dim wks As Worksheet
Set wks = ActiveSheet

With wks
FirstRow = 1
FirstCol = 3
For iRow = FirstRow + 1 To 6
If .Cells(iRow, "B").Value = "BASE" Then
prodcode = "0003408909"
ElseIf .Cells(iRow, "B").Value = "TEAM" Then
prodcode = "0003498909"
ElseIf .Cells(iRow, "B").Value = "COURSE" Then
prodcode = "0003407909"
End If
For iCol = FirstCol + 1 To 64
myStr = .Cells(1, iCol).Text & "," _
& prodcode & "," _
& .Cells(iRow, iCol).Text
MsgBox myStr
Next iCol
Next iRow
End With

"Joe K." wrote:


I have several if statements listed below, that correspond to the string
values in the "B" column. If the .Cells(iRow, "B").Text= "BASE" then
prodcode = "0003408909". Please help me add the prodcode string to the
myStr string listed below.

Please help me modify the code listed below to add the prodcode to the myStr
string listed below.

Thanks so much for the help.

Dim prodcode As String
Dim myStr As String
Dim wks As Worksheet


With wks
FirstRow = 1
FirstCol = 3
For iRow = FirstRow + 1 To 6
If .Cells(iRow, "B").Text = "BASE" Then
prodcode = "0003408909"
ElseIf .Cells(iRow, "B").Text = "TEAM" Then
prodcode = "0003498909"
ElseIf .Cells(iRow, "B").Text = "COURSE" Then
prodcode = "0003407909"
End If
For iCol = FirstCol + 1 To 64
myStr = .Cells(1, iCol).Text & "," _
& prodcode & "," _
& .Cells(iRow, iCol).Text
Print #1, myStr
Next iCol
Next iRow
End With



All times are GMT +1. The time now is 01:22 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com