如何在不使用Excel的情况下运行宏

时间:2012-11-26 13:24:58

标签: excel excel-vba vba

我是一名DB Guy,我对VB一无所知。 我在Excel中有一个宏,在Excel中我有交叉表格记录。 我的宏将Crosstabular记录转换为表格记录。 但我的要求是我想在excel之外运行相同的宏。 .VBS文件应该在那里,每当我们运行.VBS时,它应该从某个地方选择excel并将交叉表记录转换为表格记录并保存在某个不同的位置。 我已经通过Google搜索创建了相同的代码。有人请查看下面的代码并帮助我使用正确的代码。

Sub RunMacro()

Dim xlApp 'As Excel.Application 

Dim xlBook 'As Workbook 

Dim xlSheet 'As Worksheet

Dim wsCrossTab 'As Worksheet

Dim wsList 'As Worksheet

Dim iLastCol 'As Long

Dim iLastRow 'As Long

Dim iLastRowList 'As Long

Dim rngCTab 'As Range 'Used for range in Sheet1 cross tab sheet

Dim rngList 'As Range 'Destination range for the list

Dim I 'As Long


Set xlApp = CreateObject("Excel.Application")

Set xlBook = xlApp.Workbooks.Open("D:\Source.xls")

CrossTabToList()

xlBook.SaveAs "D:\Results.xls"

xlApp.Quit

End Sub

Sub CrossTabToList()

Set wsCrossTab = Worksheets("Tabular")

Set wsList = Worksheets.Add


'Find the last row in Sheet1 with the cross tab

iLastRow = wsCrossTab.Cells(Rows.Count, "A").End(xlUp).Row

'Set the initial value for the row in the destination worksheet

iLastRowList = 2

'Find the last column in Sheet1 with the cross tab

iLastCol = wsCrossTab.Range("A8").End(xlToRight).Column

'Create a new sheet and set the heading titles

wsList.Range("A1:C1") = Array("CATEGORY", "SUBCATEGORY", "VALUE")

'Start looping through the cross tab data

For I = 2 To iLastRow

Set rngCTab = wsCrossTab.Range("A" & I) 'initial value A2

Set rngList = wsList.Range("A" & iLastRowList) 'initial value A2

'Copy individual names in Col A (A2 initially) into as many rows as there are data columns in the cross tab (less 1 for Col A).

rngCTab.Copy rngList.Resize(iLastCol - 1)

'Move up a I rows less one and across one column (using offset function) to select heading row. Copy.

rngCTab.Offset(-(I - 1), 1).Resize(, iLastCol - 1).Copy

'Paste transpose to columns in the list sheet alongside the names

rngList.Offset(0,1).PasteSpecial Transpose:=True

'Staying on same row (2 initially) copy the data from the cross tab

rngCTab.Offset(, 1).Resize(, iLastCol - 1).Copy

'Past transpose as column in list sheet

rngList.Offset(0, 2).PasteSpecial Transpose:=True

'Set the new last row in list sheet to be just below the last name copied

iLastRowList = iLastRowList + (iLastCol - 1)

'increment I by 1

 Next I


Application.DisplayAlerts = False

Sheets("Tabular").Select

ActiveWindow.SelectedSheets.Delete

Application.DisplayAlerts = True

Sheets("Sheet1").Select

Sheets("Sheet1").Name = "Results"

objwkbk.SaveAs "D:\Results.xls"


End Sub

谢谢,

普利文


正如我所提到的,我不是Java开发人员或编码人员,我是数据库人员,我对Java一无所知。我想将上述代码用作.VBS文件。我希望有人将我的上述代码更正为在.VBS文件中使用它。如果你能做到这一点,我将非常感激。 在此先感谢。

1 个答案:

答案 0 :(得分:0)

这是一个非常好的主意。 Excel文件中的VBA可能会使用户感到困惑,因此我会尽可能避免这种情况。

我建议您将过程存储在Access文件中。转换它有一点工作,但这应该让你开始:

  1. 制作新的Access数据库
  2. 在新数据库中,创建一个新的VBA 模块。将代码粘贴到那里。
  3. 添加最新版本的 Microsoft Excel对象库
  4. 进行必要的其他更改以使代码再次处于正常工作状态(您将需要进行一些试验和错误。重复运行代码并在弹出时处理错误消息)
  5. 更改为功能(您需要执行此操作才能从宏中调用它)
  6. 制作新的。使用参数 RunMacro()
  7. 添加 RunCode 操作

    将来,您只需打开数据库并单击宏即可运行代码。

相关问题