我正在尝试使用DDE方法编写vba代码。该代码的目的是复制excel表的一组列,并将其粘贴到EES (工程方程求解器)软件的参数表中。然后运行EES代码来求解de表,生成输出数据的列。然后将此数据复制并粘贴回包含输入数据的excel文件中。
由于我是vba新手,所以我使用了EES (从EXCEL执行EES宏命令)提供的示例作为指导。
当数据粘贴回excel电子表格时会出现问题:代码似乎忽略了十进制分隔符!我的excel和EES都被设置为以逗号作为小数分隔符,当我手动复制来自EES的结果并粘贴到excel时,数字被正常粘贴,逗号(也是来自excel的数字被正确地粘贴到ESS中)。
但是,当我将代码设置为执行此任务时,诸如"15,47“之类的数字在excel中被粘贴为"1,55E+12”或"1547421377050“。守则如下:
Private Sub cmdDDE_Click()
Dim ChNumber As Integer
Dim myShell As String
ChNumber = -1
myShell = frmEESDDE.txtApp.Text
On Error Resume Next
'Copy selected rows into clipboard
Range("B2:G1401").Select
Selection.Copy
Shell_R = Shell(myShell, 1)
If Shell_R <> "" Then
'Initiate DDE
ChNumber = Application.DDEInitiate(app:="ees", topic:="")
If ChNumber <> -1 Then
'Open EES
Application.DDEExecute ChannelNumber, "[Open C:\EES\Tablesolve.ees]"
'Paste data
Application.DDEExecute ChannelNumber, "[Paste Parametric 'Table 1' R1 C1]"
'Solve parametrictable
Application.DDEExecute ChannelNumber, "[SOLVETABLE 'TABLE 1' Rows=1..1400]"
'Copy results
Application.DDEExecute ChannelNumber, "[COPY ParametricTable 'Table 1' R1 C7:R1400 C14]"
'Choose separators
Application.DecimalSeparator = ","
Application.ThousandsSeparator = "."
Application.UseSystemSeparators = False
'Paste results from EES into EXCEL
Application.Paste Destination:=Worksheets("Sheet1").Range("H2:O1440")
Application.UseSystemSeparators = True
'Quit EES and Terminate DDE
DDEExecute ChNumber, "QUIT"
Application.DDETerminate ChNumber
Else
MsgBox "Unable to initiate connection to EES", vbExclamation, "EES DDE"
End If
frmEESDDE.Hide
Else
MsgBox "The application, " & myShell & ", was not found", vbExclamation, "EES DDE"
End If如您所见,我尝试将十进制分隔符设置为",“正如这个链接中所建议的:Pasting decimal numbers in excel / comma and point decimal separator,但它也没有工作!
我很感激你的帮助!
发布于 2015-07-16 15:02:49
问题解决了!我还在说葡萄牙语的堆栈溢出语社区发布了这个问题,得到了一个非常有帮助的答案。小小的调整就解决了我的问题!与葡萄牙文解决办法的链接如下:
但对于那些希望使用英文版本的人,我将尝试总结一下为修复代码所做的工作:
1-申报范围变量:
Dim interval As Range 'represent the cells in which info was pasted
Dim Cell As Range 'to allow cell format to be changed2-复制esternal程序的结果并在粘贴之前:
Set interval = Worksheets("Sheet1").Range("H2:O1440") 'set interval to paste the results
interval.NumberFormat = "@" 'set format to text3-粘贴后:
interval.NumberFormat = "General" 'set format to general
For Each Cell In interval
Cell.Value = FormatNumber(CDbl(Cell.Value), 2) 'set only 2 decimal places
Cell.Value = CDbl(Cell.Value) 'set to double
Next其余的代码保持原样。
特别感谢坎托尼,他帮助解决了pt版本的问题。
发布于 2015-07-15 19:16:40
与其用application.paste粘贴,不如只粘贴值。ie:而不是
Application.Paste Destination:=Worksheets("Sheet1").Range("H2:O1440")使用
Range("H2:O1440").PasteSpecial xlPasteValues如果这不起作用,那么将输出解析为字符串。
发布于 2015-07-16 07:17:34
您也可以尝试这样做:
Worksheets("Sheet1").Range("H2").PasteSpecial xlPasteValuesAndNumberFormatshttps://stackoverflow.com/questions/31438318
复制相似问题