从VBS运行时,MID函数不执行任何操作

时间:2014-10-20 06:02:05

标签: excel-vba vbscript vba excel

我一直在努力做一些非常简单的事情。我是VB的绝对新手所以我确信我错过了一些东西。

我一直在尝试使用MID将电子表格中的数字拆分。

这是我到目前为止所做的事情(我一直试图只做一个以确保它有效):

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\Documents and Settings\<username>\Desktop\New.csv")

Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Activate

sub_str = Mid(A1, 1, 4)

因此应用程序打开,工作表处于活动状态,然后......没有。没有错误或任何事情。它就像它从字面上停在那里并完全忽略了最后一行。我要分割的数字在Excel中如下所示。它们只是向后的日期,因此想要分裂,所以我可以将它分开并以正确的方式进行。

20140101

20140127

20140303

20140310

20140310

20140310

20140310

20140418

20140419

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:2)

试试这个:

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\USER\Desktop\new1.csv")

Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Activate
rowCount=objWorksheet.usedrange.rows.count
Set rngA=objWorksheet.Range("A1:A" & rowCount)
'Set rngB=rngA.offset(,1)             'objWorksheet.Range("B1")


with objWorksheet
for each cell in rngA
sub_strY = Mid(cell.value, 1, 4)
sub_strM=Mid(cell.value, 5, 2)
sub_strD=Mid(cell.value, 7, 2)
'msgbox sub_strY
'msgbox sub_strM
'msgbox sub_strD
strDate=sub_strD & "/" & sub_strM & "/" & sub_strY
msgbox strDate
'cell.offset(,1).value=strDate     ''to another column  
cell.value=strDate                 ''to overwrite
next
end with

答案 1 :(得分:1)

可以在vbscript中使用Mid。问题不存在。问题出在A1行中的sub_str = Mid(A1, 1, 4)

A1被视为变量。最好使用对象。此外,如果您希望20140419更改为04192014,那么您实际上不需要Mid。您可以使用LeftRight

我假设数据格式为yyyymmdd,您希望输出为mmddyyyy。如果您希望输出为ddmmyyyy,则必须使用Mid。像这样

sDate = Right(.Range("A" & i).Value, 2) & _
        Mid(.Range("A" & i).Value, 5, 2) & _
        Left(.Range("A" & i).Value, 4)

这是你在尝试的吗?

Const xlUp = -4162

Dim oXLApp, oXLwb, oXLws
Dim lRow, i
Dim sFile, sDate 

'~~> Change this to the relevant file
sFile = "C:\Users\Siddharth Rout\Desktop\book1.xlsx"

'~~> Establish an EXCEL application object
On Error Resume Next
Set oXLApp = GetObject(, "Excel.Application")

'~~> If not found then create new instance
If Err.Number <> 0 Then
    Set oXLApp = CreateObject("Excel.Application")
End If
Err.Clear
On Error GoTo 0

'~~> Hide Excel
oXLApp.Visible = False

'~~> Open files
Set oXLwb = oXLApp.Workbooks.Open(sFile)

'~~> Set the worksheet you want to work with
Set oXLws = oXLwb.Sheets(1)

'~~> work with the worksheet
With oXLws
    lRow = .Range("A" & .Rows.Count).End(xlUp).Row

    For i = 1 To lRow
        '~~> 20140101 becomes 01012014. If the Col A is formatted 
        '~~> as number then the leading `0` will disappear as 
        '~~> shown in the screenshot below
        sDate = Right(.Range("A" & i).Value, 4) & Left(.Range("A" & i).Value, 4)
        .Range("A" & i).Value = sDate
    Next
End With

'~~> Close and save
oXLwb.Close (True)

'~~> CLEANUP (VERY IMPROTANT)
Set oXLws = Nothing
Set oXLwb = Nothing
oXLApp.Quit
Set oXLApp = Nothing

MsgBox "DONE" 'OR wscript.echo "Done"

截图:

enter image description here

enter image description here

答案 2 :(得分:0)

Mid函数不是VBscript函数,而是VBA函数,因此无论如何这可能有效:

objExcel.Mid([A1], 1, 4)

而不是:

Mid(A1, 1, 4)

如果这不起作用,则需要运行Excel,然后尝试将所有逻辑放在Excel函数中并在需要时从VBscript执行它(如下所示):

RunMacro
Sub RunMacro() 
  dim xl
  Set xl = CreateObject("Excel.application")
  Dim xlBook      
  Dim sCurPath
  path = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
  Set xl = CreateObject("Excel.application")
  Set xlBook = xl.Workbooks.Open(path & "\Book1.xlsm", 0, True)      
  xl.Application.Visible = False
  xl.DisplayAlerts = False    
  xl.Application.run "Book1.xlsm!Module.MyMacro"
  xl.ActiveWindow.close
  Set xlBook = Nothing
  xl.Quit 
  Set xl = Nothing
End Sub