Macro for making a XML in a command button active X

Romano_odK

Active Member
Joined
Jun 4, 2020
Messages
380
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

I would prefer to have this code in a command active button but I can't get it to work. Does anyone know how to do this?


Thank you for your time,

Romano


Public Itemcode As String
Public SalesPrice As String
Public CostPrice As String
Public SupplierMain As String
Public SupplierCode As String
Public SupplierPrice As String

Public Itemrow As Integer ' Counter for actual row
Public SaveDir As String ' Header Cell (2,2)
Public ItemCurrency As String ' Header Cell (4,2)
Public ItemResource As String ' Header Cell (6,2)
Public Action As Boolean ' Flag
Private Function Conversion(ByVal InputString As String) As String

Dim ASCII As Integer
Conversion = ""
While Len(InputString) > 0
ASCII = AscW(Left(InputString, 1))

Select Case ASCII
Case 1 To 9999 '47, 58 To 64, 91 To 96, 123 To 235
Conversion = Conversion & "&#" & ASCII & ";"
Case Else
Conversion = Conversion & Left(InputString, 1)
End Select

InputString = Right(InputString, Len(InputString) - 1)
' ,"ø","ø")
Wend
End Function
Sub ConvertPurchasepriceToXML()
' ----------------------------------------------------------------------------------------
' Create output file as an object in the system with the file name as stated in the SaveDir variable
' If this file exists, it will be overwritten without questions asked
SaveDir = Worksheets("A100").Cells(3, 1)
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("" & SaveDir & "", True)
' ----------------------------------------------------------------------------------------
' Write xml header
a.writeline ("<?xml version=""1.0"" ?>")
a.writeline ("<eExact xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:noNamespaceSchemaLocation=""eExact-Schema.xsd"">")
a.writeline ("<Items>")
' ----------------------------------------------------------------------------------------
' Step 1: initialize
' ----------------------------------------------------------------------------------------
Action = True
If Worksheets("A100").Cells(3, 2) = Empty Then
MsgBox ("Currency not filled")
Action = False
End If
If Worksheets("A100").Cells(3, 2) = Empty Then
MsgBox ("Resource not filled")
Action = False
End If

' ----------------------------------------------------------------------------------------
' Step 2: Write records for Items
' ----------------------------------------------------------------------------------------

Itemrow = 7
While Worksheets("A100").Cells(Itemrow, 1) <> Empty And Action

Itemcode = Worksheets("A100").Cells(Itemrow, 1)
' SalesPrice = Replace(Worksheets("A100").Cells(Itemrow, 3), ",", ".")
' CostPrice = Replace(Worksheets("A100").Cells(Itemrow, 6), ",", ".")
SupplierMain = Worksheets("A100").Cells(Itemrow, 27)
SupplierCode = Worksheets("A100").Cells(Itemrow, 22)
SupplierPrice = Replace(Worksheets("A100").Cells(Itemrow, 17), ",", ".")

Call WriteRecord(a)
Itemrow = Itemrow + 1
Wend
' ----------------------------------------------------------------------------------------
' Step 3: End file
' ----------------------------------------------------------------------------------------
If Action = True Then
a.writeline ("</Items>")
End If
a.writeline ("</eExact>")
a.Close
End Sub

Private Sub WriteRecord(ByVal a As Object)
a.writeline (" <Item code=""" & Itemcode & """ type=""S"">")

' a.writeline (" <Sales>")
' a.writeline (" <Price type=""S"">")
' a.writeline (" <Value>" & SalesPrice & "</Value>")
' a.writeline (" <VAT code=""" & VATCode & """/>")
' a.writeline (" </Price>")
' a.writeline (" <Unit unit=""" & SalesUnit & """/>")
' a.writeline (" </Sales>")
' a.writeline (" <Costs>")
' a.writeline (" <Price>")
' a.writeline (" <Currency code=""" & ItemCurrency & """/>")
' a.writeline (" <Value>" & CostPrice & "</Value>")
' a.writeline (" </Price>")
' a.writeline (" </Costs>")

If SupplierCode <> Empty Then
a.writeline (" <ItemAccounts>")
a.writeline (" <ItemAccount default=""" & SupplierMain & """>")
a.writeline (" <Account code="""" type=""S""><Creditor code=""" & SupplierCode & """/></Account>")
a.writeline (" <Purchase>")
a.writeline (" <Price type=""P"">")
a.writeline (" <Value>" & SupplierPrice & "</Value>")
a.writeline (" </Price>")
a.writeline (" </Purchase>")
a.writeline (" </ItemAccount>")
a.writeline (" </ItemAccounts>")
End If
'
a.writeline (" </Item>")
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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