DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Copy part of channel data and paste into excel worksheet

Solved!
Go to solution

Hello,

  I want to copy part of channel data and paste into excel worksheet programmatically. I can copy data with "DataBlClpCopy" into clipboard, Does anybody can tell me what command I can use to paste data from clipboard into excel worksheet?

Thank you for your help!

GQ

0 Kudos
Message 1 of 7
(3,387 Views)

Hi GQ,

 

I'm not sure of the exact ActiveX shenanigans you'd need to pull to get the right active sheet, but... this looks like it would work: https://msdn.microsoft.com/en-us/vba/excel-vba/articles/worksheet-paste-method-excel

William R.
0 Kudos
Message 2 of 7
(3,360 Views)

Hi William,

 

It doesn't work.":=" in "Destination:=Worksheets("Sheet1").Range("D1")" is not recognized correctly in script.

Thanks,

GQ

0 Kudos
Message 3 of 7
(3,352 Views)
Solution
Accepted by topic author gq

Hi GQ,

 

Try this.  It really does work.

Set Group = Data.Root.ActiveChannelGroup
Set myExcelApp = CreateObject("Excel.Application")
myExcelApp.Visible = true
myExcelApp.Workbooks.Add()
Set myWorksheet = myExcelApp.Activesheet
Set myRange = myWorksheet.Range("a1")
Set myRange = myRange.Resize(Group.Channels(1).Size, Group.Channels.Count)
myRange.Value = ChnToVariant(Group.Channels, "TransposedMatrix")
Set myExcelApp = Nothing

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 4 of 7
(3,347 Views)

Hi Brad,

it works great. 

Let's go further, How can I copy part of channel data and paste to excel worksheet?

 

Thank you very much!

GQ

 

0 Kudos
Message 5 of 7
(3,336 Views)
Solution
Accepted by topic author gq

Hi GQ,

 

You have two options.  You can fill the 2D array with values yourself, then pass that to the Excel range:

RowSize = 2
ColSize = 3
ReDim Values(RowSize-1, ColSize-1)
Values(0,0) = "0,0"
Values(0,1) = "0,1"
Values(0,2) = "0,2"
Values(1,0) = "1,0"
Values(1,1) = "1,1"
Values(1,2) = "1,2"

Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = true
ExcelApp.Workbooks.Add()
Set Worksheet = ExcelApp.Activesheet
Set Range = Worksheet.Range("a1")
Set Range = Range.Resize(RowSize, ColSize)
Range.Value = Values
Set ExcelApp = Nothing

Or you can create a temporary Group of Channels that contain the desired subset using DataBlCopy():

Dim RawGroup, RawChannels
Set RawGroup = Data.Root.ChannelGroups(2)
Set RawChannels = Data.CreateElementList()
Call RawChannels.Add(RawGroup.Channels("Noise_2"))
Call RawChannels.Add(RawGroup.Channels("Noise_4"))
Call RawChannels.Add(RawGroup.Channels("Noise_5"))
Call ExportGroupBlock(RawGroup, RawChannels, 1000, 101000)


Sub ExportGroupBlock(RawGroup, RawChannels, iMin, iMax)
  Dim SelGroup, SelChannels
  Set SelGroup = Data.Root.ChannelGroups.Add("TempExportGroup")
  Set SelChannels = SelGroup.Channels
  FOR Each Channel In RawChannels
    Call SelChannels.Add(Channel.Name, Channel.DataType)
  NEXT ' Channel
  Call DataBlCopy(RawChannels, iMin, iMax-iMin+1, SelChannels)
  Set myExcelApp = CreateObject("Excel.Application")
  myExcelApp.Visible = TRUE
  myExcelApp.Workbooks.Add()
  Set myWorksheet = myExcelApp.Activesheet
  Set myRange = myWorksheet.Range("a1")
  Set myRange = myRange.Resize(SelGroup.Channels(1).Size, SelGroup.Channels.Count)
  myRange.Value = ChnToVariant(SelGroup.Channels, "TransposedMatrix")
  Set myExcelApp = Nothing
  Call Data.Root.ChannelGroups.Remove(SelGroup.Name)
End Sub ' ExportGroupBlock()

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 6 of 7
(3,329 Views)

Hi Brad,

 

I really appreciate your help.

Thank you very much!

 

GQ 

0 Kudos
Message 7 of 7
(3,324 Views)