Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6134849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:24:27+00:00 2026-05-23T17:24:27+00:00

I’m in the process of looping through an Excel spreadsheet and combining all the

  • 0

I’m in the process of looping through an Excel spreadsheet and combining all the cells into a string, which I did. Now I need to format the string with XML tags before I send it for upload, and I’m having some difficulty working the tagging into the loop correctly. It seems like it is almost working, but a few of the tags are not going in the correct place. Any help would be much appreciated.

Code:

Public file As String

Sub locate_file()

Dim sheet1_95 As String
Dim theRange As Range
Dim strVal As String
Dim wb As Workbook
Dim counterDT As Integer
Dim counterSVR As Integer
Dim counterMB As Integer

Dim outputStr As String


'prompt user for location of other excel sheet'
file = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx")

Set wb = Workbooks.Open(file)

Dim cell As Range

'initializing the xml string'
strVal = "<root>"

Sheets("DT").Activate

counterDT = 1

For Each cell In ActiveSheet.UsedRange.Cells
'this first if-block is just excluding the few header cells from the data collection'
  If cell.Value <> "SKU" And cell.Value <> "P Number" And cell.Value <> "Month" _
     And cell.Value <> "DP Dmd" And cell.Value <> "Vertical" Then
    If cell.Column = "1" Then
      strVal = strVal & "<item><sku>" & cell.Value & "</sku>"
    ElseIf cell.Column = "2" Then strVal = strVal & "<pnum>" & cell.Value & "</pnum>"
    ElseIf cell.Column = "3" Then strVal = strVal & "<month>" & cell.Value & "</month>"
    ElseIf cell.Column = "4" Then strVal = strVal & "<forecast>" & cell.Value & "</forecast>"
    Else: strVal = strVal & "<vertical>" & cell.Value & "</vertical>"
    End If

    counterDT = counterDT + 1

    If cell.Row <> 1 Then
      If counterDT Mod 6 = 0 Then
        strVal = "<item>" & strVal & "<percent>" & category.percent(cell, "DT") & "</percent>"
       Else: End If
    Else: End If
  End If
Next

strVal = strVal & "</root>"

So basically the problem is, this loop/nested if is printing like 30 “item” tags at the very beginning of the string and I’m not sure why.

For some other information, the Excel sheet is 6 columns, and will always be 6.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:24:28+00:00Added an answer on May 23, 2026 at 5:24 pm

    When I’m creating xml tags, I like to move the actual tagging into a separate function. The upside is that it ensures my tags match. The downside is that you don’t “apply” the tags until the end. Tags like item and root are done after all the tags within them are done. Here’s an example:

    Sub locate_file()
    
        Dim sVal As String
        Dim sRow As String
        Dim wb As Workbook
        Dim sh As Worksheet
        Dim lCntDT As Long
        Dim rCell As Range
        Dim rRow As Range
        Dim vaTags As Variant
    
        gsFile = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx")
    
        If gsFile <> "False" Then
            Set wb = Workbooks.Open(gsFile)
            Set sh = wb.Sheets("DT")
            vaTags = Array("sku", "pnum", "month", "forecast", "vertical")
    
            lCntDT = 1
    
            For Each rRow In sh.UsedRange.EntireRow
                sRow = ""
                If rRow.Cells(1) <> "SKU" Then
                    For Each rCell In Intersect(sh.UsedRange, rRow).Cells
                        If rCell.Column <= 4 Then
                            sRow = sRow & TagValue(rCell.Value, vaTags(rCell.Column - 1))
                        Else
                            sRow = sRow & TagValue(rCell.Value, vaTags(UBound(vaTags)))
                        End If
                    Next rCell
    
                    lCntDT = lCntDT + 1
                    If rRow.Row <> 1 And lCntDT Mod 6 = 0 Then
                        sVal = sVal & TagValue("CatPct", "percent")
                    End If
                    sRow = TagValue(sRow, "item")
                    sVal = sVal & sRow & vbNewLine
                End If
            Next rRow
            sVal = TagValue(sVal, "root")
        End If
    
        Debug.Print sVal
    
    End Sub
    
    Function TagValue(ByVal sValue As String, ByVal sTag As String) As String
    
        TagValue = "<" & sTag & ">" & sValue & "</" & sTag & ">"
    
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.