don't know how to paste a string into three different cells in the same row

Get help with using AutoHotkey and its commands and hotkeys
kwfine
Posts: 16
Joined: 26 Nov 2015, 06:46

don't know how to paste a string into three different cells in the same row

30 Jun 2018, 12:43

Hi all,

I am using AHK Version 1.1.25.01 in Windows 7.
I am learning how to use AHK to paste some data into Excel.
Here is the code:

Code: Select all

!h::
path := "C:\test1.xls"   
XL := ComObjCreate("Excel.Application")
XL.Workbooks.Open(path)
XL.Visible := True
XL.Sheets(1).Range("A1:C1") := "1	2	3"

When I pressed the combo key, the excel file was opened but then, what I saw in the cells (A1, B1, C1) were like these:
123 123 123
instead of
1 2 3 (1 in A1, 2 in B1, and 3 in C1) (I want it to be this way)

What should I do ?

Thank you!

Kitty
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: don't know how to paste a string into three different cells in the same row

30 Jun 2018, 13:03

Here's an example script:

Code: Select all

q:: ;excel - populate row
oXl := ComObjCreate("Excel.Application")
oXl.Visible := True
oXl.Workbooks.Add

;version 1 (simpler, for fewer cells)
vText := "1	2	3"
Loop, Parse, vText, `t
	oXl.Cells(1, A_Index) := A_LoopField

Sleep, 3000

;version 2 (faster, for more cells)
vText := "4	5	6"
vRowCount := 1
StrReplace(vText, "`t",, vColCount), vColCount += 1
oSafeArray := ComObjArray(12, vRowCount, vColCount) ;VT_VARIANT := 12
;note: SafeArray uses 0-based indexes
Loop, Parse, vText, `t
	oSafeArray[0, A_Index-1] := A_LoopField
oRange := oXl.Range(oXl.Cells(1, 1), oXl.Cells(vRowCount, vColCount))
oRange.Value := oSafeArray

oXl := ""
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: flyingDman, Google [Bot], JoeWinograd, Siopaulo, teadrinker, vsub and 110 guests