2011-06-28 7 views
2

基本的には、実行時に特定のExcelファイルを指すように促すExcelアプリケーションを作成しています。文字列、うまく動作します。どのようにするか分からないのは、アクティブなワークシートの範囲を選択し、各セルの値を1つの文字列に結合することです。Excelワークシートのすべてのセルを文字列(VBA)として受け取ります

これは、これまでの私のコードです:

Option Explicit 

Sub locate_file() 

Dim file As String 
Dim sheet1_95 As String 
Dim theRange As Range 

'prompt user for location of other excel sheet' 
file = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx") 

'test input of location' 
Workbooks("testing input file.xlsx").Sheets("location").Activate 
Range("A1") = file 

'activate the workbook and sheet' 
Workbooks("95%.xlsx").Sheets("DT").Activate 

'Testing retrieving cells as string' 
Set theRange = Range("A2:A4") 

'how do i retrieve values in this range and combine them into 1 string?' 

End Sub 

答えて

2
'Testing retrieving cells as string' 
Set theRange = Range("A2:A4") 

'how do i retrieve values in this range and combine them into 1 string?' 
Dim c as int, x as int 
Dim strValue as string 
c = therange.rows.count 
strValue = vbnullstring 
for x = 1 to c 
strValue = strValue & theRange.cells(x,1).value 
next x 
+1

私はあなたが言ったと思います:.cells(x、1).value – Reafidy

+0

修正しました、ありがとう – lionz

2

私は何をするかどうかはわからないこととは対照的に(Application.InputBox機能を使用することにより、アクティブなワークシートに

を範囲選択しています)VBA.InputBoxへ:

dim r as range 
set r = application.inputbox("Select a range", Type:=8) 

各セルの値を1つの文字列に結合します。細胞をループすることで

dim c as range 
dim s as string 

for each c in r.cells 
    s = s & c.value 
next 
+0

あなたは多数のセルを持っている場合は、代わりにバリアントを使用し処理しなければなりません。たとえば、dim vを変形として使用します。 v = application.inputbox(「範囲の選択」、「タイプ:= 8」)通知なし。次に、2次元配列をループします。 – Jon49

+0

@ Jon49それは 'v = application.inputbox(" Select a range "、Type:= 8).Value'とあいまいなことになります。あなたが言っているように、細胞の数が非常に多いと、あなたはその違いに気付き始めるでしょう。 – GSerg

+0

助けてくれてありがとう、私はそれを試してみましょう。 – jz3

関連する問題