Vba loop through 2 columns

Flowcentric

New Member
Joined
Jul 29, 2016
Messages
14
Hi

I am creating a xml file from excel using vba. I have a list of stockcodes in a column and list of Warehouses in a column .
Each stock code must be attached to all warehouse. I want the code to selected the first stock code in column A and create the xml stockcode warehouse combination then move to A2 and then repeat then repeat the process until cell in the stockcode column = "" (so one stockcode many warehouses) Many thanks for your assistance

Here is the code below. I am not sure how to loop correctly
PHP:
Private Sub WarehouseWHXM()
Dim Row1
Dim STOCK
Dim Wh
Dim ROW


Document = Document & "<setupinvwarehouse>"


Row1 = 7
ROW = 2


 
Do While Range("AH" & ROW).Text <> ""
Do While Range("A" & Row1).Text <> ""


Document = Document & "<item>"
Document = Document & "<key>"
Document = Document & "<stockcode>" & Range("A" & Row1).Text & "</stockcode>"
Document = Document & "<warehouse>" & Range("AH" & ROW).Text & "</warehouse>"
Document = Document & "</key>"
Document = Document & "<costmultiplier>1.10</costmultiplier>"
Document = Document & "<unitcost>10.00</unitcost>"
Document = Document & "</item>"
Row1 = Row1 + 1
Loop
ROW = ROW + 1
Loop
Document = Document & "</setupinvwarehouse>"


'Create file
'
   Dim FSO
   Dim FSOFile
   Set FSO = CreateObject("Scripting.FileSystemObject")
   'Set FSOFile = FSO.CreateTextFile("\\Tevdbn-app-01\SYSPRO7\DFM\GlUpdate\GLChanges.xml")
   Set FSOFile = FSO.CreateTextFile("C:\New folder\new.xml")
   
    
  FSOFile.WriteLine (Document)
  FSOFile.Close


 'Housekeeping
 


Set FSO = Nothing
Set FSOFile = Nothing
Document = ""

End Sub
 
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi

Try changing

Code:
[COLOR=#333333]Do While Range("AH" & ROW).Text <> ""[/COLOR]
[COLOR=#333333]Do While Range("A" & Row1).Text <> ""[/COLOR]

to

Code:
[COLOR=#333333]Do While [/COLOR][COLOR=#ff0000]Not[/COLOR][COLOR=#333333] Range("AH" & ROW).Text [/COLOR][COLOR=#ff0000]=[/COLOR][COLOR=#333333] ""[/COLOR]
[COLOR=#333333]Do While [/COLOR][COLOR=#ff0000]Not[/COLOR][COLOR=#333333] Range("A" & Row1).Text [/COLOR][COLOR=#ff0000]=[/COLOR][COLOR=#333333] ""[/COLOR]
 
Upvote 0
What Dave said above should work, but my question is why do you use Document = Document & "" so much? What is that doing?

When I run the code you have and the following on a junk file I created I get the same results each time

Code:
Private Sub WarehouseWHXM()

Dim FSO, STOCK, Wh, FSOFile, Row1, ROW


Row1 = 7
ROW = 2


    Do While Not Range("AH" & ROW).Text = ""
    
        Do While Not Range("A" & Row1).Text = ""
            Document = Document & "" & Range("A" & Row1).Text & ""
            Document = Document & "" & Range("AH" & ROW).Text & ""
            Document = Document & "1.10"
            Document = Document & "10.00"
            Row1 = Row1 + 1
        Loop
        
    ROW = ROW + 1
    Loop


'Create file


Set FSO = CreateObject("Scripting.FileSystemObject")
'Set FSOFile = FSO.CreateTextFile("\\Tevdbn-app-01\SYSPRO7\DFM\GlUpdate\GLChanges.xml")
Set FSOFile = FSO.CreateTextFile("C:\New folder\new.xml")


FSOFile.WriteLine (Document)
FSOFile.Close


'Housekeeping
Set FSO = Nothing
Set FSOFile = Nothing


End Sub
 
Last edited:
Upvote 0
What Dave said above should work, but my question is why do you use Document = Document & "" so much? What is that doing?

When I run the code you have and the following on a junk file I created I get the same results each time


Example of the XML file create
HTML:
[COLOR=blue]<?xml version="1.0"?>[/COLOR]
<style xmlns="http://www.w3.org/1999/xhtml">@namespace html url([url]http://www.w3.org/1999/xhtml);[/url] :root {                       font:small Verdana;        font-weight: bold;         padding: 2em;              padding-left:4em;       }                          * {                           display: block;            padding-left: 2em;      }                          html|style {                  display: none;          }                          html|span, html|a {           display: inline;           padding: 0;                font-weight: normal;       text-decoration: none;  }                          html|span.block {             display: block;         }                          *[html|hidden],            span.block[html|hidden] {     display: none;          }                          .expand {                     display: block;         }                          .expand:before {              content: '+';              color: red;                position: absolute;        left: -1em;             }  .collapse {                   display: block;         }                          .collapse:before {            content: '-';              color: red;                position: absolute;        left:-1em;              }                         </style>[URL="file:///C:/New%20folder/new.xml#"]<[COLOR=#990000]SetupInvMaster[/COLOR]>[/URL]<item>[URL="file:///C:/New%20folder/new.xml#"]<[COLOR=#990000]Item[/COLOR]>[/URL]<stockcode><[COLOR=#990000]StockCode[/COLOR]>ABC1000110010[COLOR=blue]</[COLOR=#990000]StockCode[/COLOR]>[/COLOR]</stockcode><warehouse><[COLOR=#990000]Warehouse[/COLOR]>21[COLOR=blue]</[COLOR=#990000]Warehouse[/COLOR]>[/COLOR]</warehouse>[COLOR=blue]</[COLOR=#990000]Item[/COLOR]>[/COLOR]</item><item>[URL="file:///C:/New%20folder/new.xml#"]<[COLOR=#990000]Item[/COLOR]>[/URL]<stockcode><[COLOR=#990000]StockCode[/COLOR]>ACC0001[COLOR=blue]</[COLOR=#990000]StockCode[/COLOR]>[/COLOR]</stockcode><warehouse><[COLOR=#990000]Warehouse[/COLOR]>21[COLOR=blue]</[COLOR=#990000]Warehouse[/COLOR]>[/COLOR]</warehouse>[COLOR=blue]</[COLOR=#990000]Item[/COLOR]>[/COLOR]</item><item>[URL="file:///C:/New%20folder/new.xml#"]<[COLOR=#990000]Item[/COLOR]>[/URL]<stockcode><[COLOR=#990000]StockCode[/COLOR]>1231[COLOR=blue]</[COLOR=#990000]StockCode[/COLOR]>[/COLOR]</stockcode><warehouse><[COLOR=#990000]Warehouse[/COLOR]>21[COLOR=blue]</[COLOR=#990000]Warehouse[/COLOR]>[/COLOR]</warehouse>[COLOR=blue]</[COLOR=#990000]Item[/COLOR]>[/COLOR]</item><item>[URL="file:///C:/New%20folder/new.xml#"]<[COLOR=#990000]Item[/COLOR]>[/URL]<stockcode><[COLOR=#990000]StockCode[/COLOR]>12313[COLOR=blue]</[COLOR=#990000]StockCode[/COLOR]>[/COLOR]</stockcode><warehouse><[COLOR=#990000]Warehouse[/COLOR]>21[COLOR=blue]</[COLOR=#990000]Warehouse[/COLOR]>[/COLOR]</warehouse>[COLOR=blue]</[COLOR=#990000]Item[/COLOR]>[/COLOR]</item>[COLOR=blue]</[COLOR=#990000]SetupInvMaster[/COLOR]>

So the code is selecting all the stockcodes in the list but is only creating the xml for the first warehouse in the list. I want the code to create the xml for each warehouse in the list. (Same stockcode-different warehouse) The next warehouse in the list is 22. The code must repeat the above for the next warehouse and the next warehouse in the list until the end of the warehouse list

Thank you for your assistance
[/COLOR]
 
Last edited by a moderator:
Upvote 0
Hi

Try changing

Code:
[COLOR=#333333]Do While Range("AH" & ROW).Text <> ""[/COLOR]
[COLOR=#333333]Do While Range("A" & Row1).Text <> ""[/COLOR]

to

Code:
[COLOR=#333333]Do While [/COLOR][COLOR=#ff0000]Not[/COLOR][COLOR=#333333] Range("AH" & ROW).Text [/COLOR][COLOR=#ff0000]=[/COLOR][COLOR=#333333] ""[/COLOR]
[COLOR=#333333]Do While [/COLOR][COLOR=#ff0000]Not[/COLOR][COLOR=#333333] Range("A" & Row1).Text [/COLOR][COLOR=#ff0000]=[/COLOR][COLOR=#333333] ""[/COLOR]


Hi Dave

Example of the XML file create
HTML:
[COLOR=blue]<!--?xml version="1.0"?-->[/COLOR]
[URL="file:///C:/New%20folder/new.xml#"]<[COLOR=#990000]SetupInvMaster[/COLOR]>[/URL]<item>[URL="file:///C:/New%20folder/new.xml#"]<[COLOR=#990000]Item[/COLOR]>[/URL]<stockcode><[COLOR=#990000]StockCode[/COLOR]>ABC1000110010<font color="blue"><!--[COLOR=#990000]StockCode[/COLOR]></stockcode><warehouse><[COLOR=#990000]Warehouse[/COLOR]>21<font color="blue"><!--[COLOR=#990000]Warehouse[/COLOR]></warehouse><font color="blue"><!--[COLOR=#990000]Item[/COLOR]></item><item>[URL="file:///C:/New%20folder/new.xml#"]<[COLOR=#990000]Item[/COLOR]>[/URL]<stockcode><[COLOR=#990000]StockCode[/COLOR]>ACC0001<font color="blue"><!--[COLOR=#990000]StockCode[/COLOR]></stockcode><warehouse><[COLOR=#990000]Warehouse[/COLOR]>21<font color="blue"><!--[COLOR=#990000]Warehouse[/COLOR]></warehouse><font color="blue"><!--[COLOR=#990000]Item[/COLOR]></item><item>[URL="file:///C:/New%20folder/new.xml#"]<[COLOR=#990000]Item[/COLOR]>[/URL]<stockcode><[COLOR=#990000]StockCode[/COLOR]>1231<font color="blue"><!--[COLOR=#990000]StockCode[/COLOR]></stockcode><warehouse><[COLOR=#990000]Warehouse[/COLOR]>21<font color="blue"><!--[COLOR=#990000]Warehouse[/COLOR]></warehouse><font color="blue"><!--[COLOR=#990000]Item[/COLOR]></item><item>[URL="file:///C:/New%20folder/new.xml#"]<[COLOR=#990000]Item[/COLOR]>[/URL]<stockcode><[COLOR=#990000]StockCode[/COLOR]>12313<font color="blue"><!--[COLOR=#990000]StockCode[/COLOR]></stockcode><warehouse><[COLOR=#990000]Warehouse[/COLOR]>21<font color="blue"><!--[COLOR=#990000]Warehouse[/COLOR]></warehouse><font color="blue"><!--[COLOR=#990000]Item[/COLOR]></item><font color="blue"><!--[COLOR=#990000]SetupInvMaster[/COLOR]>

So the code is selecting all the stockcodes in the list but is only creating the xml for the first warehouse in the list. I want the code to create the xml for each warehouse in the list. (Same stockcode-different warehouse) The next warehouse in the list is 22. The code must repeat the above for the next warehouse and the next warehouse in the list until the end of the warehouse list

Thank you for your assistance
 
Last edited by a moderator:
Upvote 0
So you need this to create multiple XML files?
 
Upvote 0
No one xml file with all the information in it. Repeating the code for each warehouse in the list . One stock code multiple warehouses
 
Upvote 0
Try this, I changed your ROW Row1 to help me identify what each was doing... let me know if this is at least closer to what you are looking for.

Code:
Private Sub WarehouseWHXM()


Dim rStock As Long, rWare As Long
Dim FSO As Object, FSOFile


Document = Document & "<setupinvwarehouse>"


rStock = 7
rWare = 2


Do While Range("A" & rStock).Text <> ""


    Do While Range("AH" & rWare).Text <> ""
             
        Document = Document & "<item>"
        Document = Document & "<key>"
        Document = Document & "<stockcode>" & Range("A" & rStock).Text & "</stockcode>"
        Document = Document & "<warehouse>" & Range("AH" & rWare).Text & "</warehouse>"
        Document = Document & "</key>"
        Document = Document & "<costmultiplier>1.10</costmultiplier>"
        Document = Document & "<unitcost>10.00</unitcost>"
        Document = Document & "</item>"
        rWare = rWare + 1
        
    Loop
    
    rStock = rStock + 1
Loop


    Document = Document & "</setupinvwarehouse>"
    
    'Create file
    Set FSO = CreateObject("Scripting.FileSystemObject")
    'Set FSOFile = FSO.CreateTextFile("\\Tevdbn-app-01\SYSPRO7\DFM\GlUpdate\GLChanges.xml")
    Set FSOFile = FSO.CreateTextFile("C:\New folder\new_" & stWare & ".xml")
    
    FSOFile.WriteLine (Document)
    FSOFile.Close
    
    'Housekeeping
    Set FSO = Nothing
    Set FSOFile = Nothing
    Document = ""


End Sub

********NOTE*********
Every time I save this post it removes everything within the <> so you will have to re-add all of that...
 
Last edited:
Upvote 0
After looking at the code more and re-reading your last comment, if there is only ONE (1) Stockcode then the first Loop is not required. It does not matter how many times the stockcode is listed within Column A if it is always the same, plus the way this is set up it will never pull Warehouses for any other stockcode in the list because the rWare never resets to 2... if this is all true then here is a shortened version of the code:

PHP:
Private Sub WarehouseWHXM()

Dim rStock As Long, rWare As Long
Dim FSO As Object, FSOFile
Document = Document & "<setupinvwarehouse>"

rStock = 7
rWare = 2

Do While Range("AH" & rWare).Text <> ""

    Document = Document & "<item>"
    Document = Document & "<key>"
    Document = Document & "<stockcode>" & Range("A" & rStock).Text & "</stockcode>"
    Document = Document & "<warehouse>" & Range("AH" & rWare).Text & "</warehouse>"
    Document = Document & "</key>"
    Document = Document & "<costmultiplier>1.10</costmultiplier>"
    Document = Document & "<unitcost>10.00</unitcost>"
    Document = Document & "</item>"
    rWare = rWare + 1

Loop

Document = Document & "</setupinvwarehouse>"

'Create file
Set FSO = CreateObject("Scripting.FileSystemObject")
'Set FSOFile = FSO.CreateTextFile("\\Tevdbn-app-01\SYSPRO7\DFM\GlUpdate\GLChanges.xml")
Set FSOFile = FSO.CreateTextFile("C:\New folder\new.xml")

FSOFile.WriteLine (Document)
FSOFile.Close

'Housekeeping
Set FSO = Nothing
Set FSOFile = Nothing
Document = ""

End Sub
</div>
 
Upvote 0
Column a has a list of stockcode ie A1 =1234, A2= 12223, A3=11232 Etc
Column AG has a list of Warehouses Ag1 = 21, Ag 2= 22, AG3 =25, AG4 = 26 etc

I want the code to go to stockcode in A1 - 1234 and create xml stockcode 1234
Warehouse 21

Stockcode 1234
Warehouse 22 until all warehouse is create for this code then


When the code is finish with the first stock code it must move to the next stock code and repeat as above with the next Stock code in column there are no more stockcodes in column A

hope this makes more sense

thanks

Hope this explains it beter
 
Upvote 0

Forum statistics

Threads
1,216,106
Messages
6,128,863
Members
449,473
Latest member
soumyahalder4

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